我最近发现了pytest
.看起来很棒.但是,我觉得文档可能更好.
我正在尝试了解哪些conftest.py
文件的用途.
在我(当前很小的)测试套件中,我conftest.py
在项目根目录中有一个文件.我用它来定义我注入测试的灯具.
我有两个问题:
conftest.py
吗?它有其他用途吗?conftest.py
文件吗?我什么时候想这样做?将举例说明.更一般地说,您如何conftest.py
在py.test测试套件中定义文件的目的和正确使用?
我想使用fixtures作为pytest.mark.parametrize的参数或具有相同结果的东西.
例如:
import pytest
import my_package
@pytest.fixture
def dir1_fixture():
return '/dir1'
@pytest.fixture
def dir2_fixture():
return '/dir2'
@pytest.parametrize('dirname, expected', [(dir1_fixture, 'expected1'), (dir2_fixture, 'expected2')]
def test_directory_command(dirname, expected):
result = my_package.directory_command(dirname)
assert result == expected
Run Code Online (Sandbox Code Playgroud)
夹具参数的问题在于夹具的每个参数都会在每次使用时运行,但我不希望这样.我希望能够根据测试选择使用哪种灯具.
我正在 pycharm 中使用 pytest 编写测试。测试分为不同的类别。
我想指定某些类必须在其他类之前运行。
我在 stackoverflow 上看到了各种问题(例如指定从文件运行 pytest 测试以及如何在所有其他测试之前运行方法)。
这些和其他各种问题想要选择特定的函数按顺序运行。fixtures
据我了解,这可以使用或 with来完成pytest ordering
。
我不关心每个类的哪些函数首先运行。我所关心的是课程按照我指定的顺序运行。这可能吗?