如何将服务器作为 py.test 的夹具运行

Bar*_*cki 4 python fixtures pytest bottle

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

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 Code Online (Sandbox Code Playgroud)

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

Oin*_*Oin 5

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

查看pytest-xprocess 之的东西,用于为您的测试运行外部服务器进程。