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

本文是小编为大家收集整理的关于如何将服务器作为py.test的夹具来运行?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想用服务器编写硒测试作为固定装置:

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()程序时,程序进入无限循环,并且未执行测试主体.我应该在同一线程中调用运行吗?我的设计很好吗?将来我希望使用服务器固定装置集成到服务器状态.例如,使用Selenium进行测试"添加评论",并最终使用服务器夹具询问服务器是否确实发生了.

推荐答案

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

查看 pytest-xprocess 用于运行测试的外部服务器进程.

本文地址:https://www.itbaoku.cn/post/1975367.html

问题描述

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()

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.

推荐答案

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.

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