[英]PyTest - Specify cleanup tests in conftest.py
I am testing a service that requires starting and shutting down a gRPC server via a client's request.
我正在测试需要通过客户端请求启动和关闭 gRPC 服务器的服务。 In my set of integration tests, I need to specify a set of pre-test and post-test actions that should happen before any given test is run within the set.
在我的一组集成测试中,我需要指定一组应该在集合中运行任何给定测试之前发生的测试前和测试后操作。 Ideally, I would like to keep these pre/post-test methods in conftest.py or organize them into their own class within separate module.
理想情况下,我想将这些前/后测试方法保留在 conftest.py 中,或者将它们组织到单独模块中自己的 class 中。
I can specify the first test that should run (test that starts the server) by doing the following within conftest.py:
我可以通过在 conftest.py 中执行以下操作来指定应该运行的第一个测试(启动服务器的测试):
@pytest.fixture(scope="session", autouse=True)
def test_start_server():
# code to start server
The problem is that when I execute another test module only the test_start_server function is executed and not the subsequent test_shutdown_request function further down in the file:
问题是,当我执行另一个测试模块时,只执行test_start_server function 而不是文件中后续的test_shutdown_request function :
def test_shutdown_request():
# code to shutdown server
Is there any way to specify the last test (post-test action) to be run?
有没有办法指定要运行的最后一个测试(测试后操作)?
If possible, I don't want to include any 3rd party dependencies or plugins, as my project already has enough.
如果可能的话,我不想包含任何第三方依赖项或插件,因为我的项目已经足够了。
I think you should use yield in your fixtures https://docs.pytest.org/en/6.2.x/fixture.html#yield-fixtures-recommended
我认为你应该在你的灯具中使用yield https://docs.pytest.org/en/6.2.x/fixture.html#yield-fixtures-recommended
@pytest.fixture(scope="session", autouse=True)
def server():
# code to start server
yield
# code to stop server
def test_some():
# some tests
def test_more():
# some tests
This code create server once for session and shutdown your server after all tests.
此代码为 session 创建服务器一次,并在所有测试后关闭服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.