我有一个基于PyTest的测试项目,该项目使用具有夹具的基类。这个“核心”项目正在按预期运行。
我还希望在其他文件夹结构中的其他“侧面”测试项目利用这些基类和固定装置。用例是我们有一个核心开发项目,还有许多其他利用该核心项目的项目。我们希望每个人都可以使用“核心”测试,但是我们不希望其他特定于项目的测试使核心混乱。
这是一个骨架树结构:
directory/
├── core/
| ├── conftest.py
| └── folder1/
| └── base_class.py
| └── conftest.py
| └── tests/
| └── test_core.py
├── ancillary/
| └── test_ancillary.py
以下是python文件的内容:
# contests of core.conftest.py
import pytest
@pytest.fixture(scope='session')
def base_true_fixture():
return True
@pytest.fixture(scope='session')
def base_false_fixture():
return False
--
# contents of core.folder1.conftest.py
import pytest
@pytest.fixture(scope='session')
def true_fixture(base_true_fixture):
return base_true_fixture
@pytest.fixture(scope='session')
def false_fixture(base_false_fixture):
return base_false_fixture
--
# contents of core.folder1.base_class.py
import pytest
class base:
true = None
false = None
@pytest.fixture(scope='class', autouse=True)
def base_setup(self, request, true_fixture, false_fixture):
request.cls.true = true_fixture
request.cls.false = false_fixture
yield
--
# contents of contents of core.folder1.tests.test_core.py
from folder1.base_class import base
import pytest
class TestCore(base):
@pytest.fixture(scope='class', autouse=True)
def console_setup(self, request, base_setup):
# Required to ensure other autouse fixtures are instantiated.
yield
def test_class_pass(self):
assert self.true, "Passes-{}".format(self.true)
def test_class_fail(self):
assert self.false, "Fails-{}".format(self.false)
如上所述,当我从“核心”目录执行pytest时,此“核心”部分可以正常工作。
当我尝试将基类与test_ancillary.py一起使用时,就会出现复杂问题。这里是它的内容:
from folder1.base_class import base
import pytest
class TestAncillary(base):
@pytest.fixture(scope='class', autouse=True)
def console_setup(self, request, base_setup):
# Required to ensure other autouse fixtures are instantiated.
yield
def test_class_pass(self):
assert self.true, "Passes-{}".format(self.true)
def test_class_fail(self):
assert self.false, "Fails-{}".format(self.false)
在我的原始设置中,我直接从“ core /”目录运行PyTest,并将其指向辅助/:
python3 -m pytest -vv -n=1 ancillary/
...
[gw0] linux -- Python 3.6.8 /usr/bin/python3
file /home/me/adopter_project_layer/ancillary/test_ancillary.py, line 18
def test_class_pass(self):
file /home/me/adopter_project_layer/core_module/folder1/base_class.py, line 8
@pytest.fixture(scope='class', autouse=True)
def base_setup(self, request, true_fixture, false_fixture):
E fixture 'true_fixture' not found
> available fixtures: __pytest_repeat_step_number, base_setup, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, console_setup, doctest_namespace, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory, worker_id
> use 'pytest --fixtures [testpath]' for help on them.
/home/me/adopter_project_layer/core_module/folder1/base_class.py:8
...
因此找不到我的装置(“ 未找到装置'true_fixture'”)。
现在,我只是想让“辅助”测试与此基类一起运行。最好的方法是什么?
我还希望用户能够在“核心”和“辅助”中同时执行测试。我正在尝试找到一种可以提供此功能的解决方案。
我找到的最接近问题的匹配项是: pytest fixtures in a separate directory
我尝试直接在“目录”之外执行此解决方案,但仍然遇到相同的问题,无法找到固定装置。就是说,我不相信自己做得正确。
有什么想法吗?我什至不太确定在这种情况下哪个应该是我的执行目录。我的主要约束是,“ core /”绝对应该仍然独立存在。
答案 0 :(得分:0)
我找到了使用此结构运行的修复程序,并将结果发布在另一个论坛中: https://github.com/pytest-dev/pytest/issues/5858
有一个conftest冲突,我只能通过将通用灯具移到一个单独的文件并将它们分别导入到每个必需的conftest中来解决。