如何将服务器作为py.test的夹具运行 [英] How to run server as fixture for py.test

查看:87
本文介绍了如何将服务器作为py.test的夹具运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用服务器作为夹具编写Selenium测试:

I want to write Selenium tests with server as fixture:

import pytest

@pytest.fixture()
def driver(request):
    from selenium import webdriver
    d = webdriver.Firefox()
    request.addfinalizer(d.close)
    return d

@pytest.fixture()
def server():
    from server import run
    run(host="localhost", port=8080)

def test_can_see_echo(driver,server):
    page = TestPage(driver)
    page.fill_text_in_input("test")
    page.click_send()
    print page.get_returnet_value()

服务器夹具中运行的功能是瓶运行功能。问题是,当我调用run()程序时会进入无限循环,并且不会执行测试主体。我应该在同一线程中调用run吗?我的设计还好吗?将来我想使用服务器固定装置集成到服务器状态。例如,使用Selenium进行测试添加注释,最后使用服务器固定装置询问服务器是否确实发生了此操作。

Function run in server fixture is bottle run function. The problem is that, when I call run() programs go into infinite loop and body of test is not executed. Should I call run in same thread? Is my design fine? In future I want use server fixture to integrate to server state. For example make test "add comment" using Selenium and in the end use server fixture to ask server if this action really happened.

推荐答案

测试挂起,因为您的 run(host = localhost,port = 8080)启动了​​一个永远等待的服务器。您应该在其他线程/进程中启动该服务器。

The tests hang because your run(host="localhost", port=8080) starts a server which waits forever. You should start that server in a different thread/process.

查看类似 pytest-xprocess 用于为测试运行外部服务器进程。

Look into something like pytest-xprocess for running external server processes for your tests.

这篇关于如何将服务器作为py.test的夹具运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆