Python 在上层目录中调用测试时查找PyTest fixture的问题
我有一个基于PyTest的测试项目,它利用基类和fixture。这个“核心”项目正在按预期运行 我还希望在不同的文件夹结构中有其他“side”测试项目使用这些基类和fixture。用例是我们有一个核心开发项目和许多其他利用这个核心项目的项目。我们希望每个人都可以使用“核心”测试,但我们不希望其他特定于项目的测试使核心变得杂乱无章 下面是一个骨架树结构:Python 在上层目录中调用测试时查找PyTest fixture的问题,python,pytest,fixtures,Python,Pytest,Fixtures,我有一个基于PyTest的测试项目,它利用基类和fixture。这个“核心”项目正在按预期运行 我还希望在不同的文件夹结构中有其他“side”测试项目使用这些基类和fixture。用例是我们有一个核心开发项目和许多其他利用这个核心项目的项目。我们希望每个人都可以使用“核心”测试,但我们不希望其他特定于项目的测试使核心变得杂乱无章 下面是一个骨架树结构: directory/ ├── core/ | ├── conftest.py | └── folder1/ | └── ba
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)
如上所述,当我从'core'目录执行pytest时,这个'core'部分工作得非常好
当我尝试将我的基类与test_.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\u装置”未找到”)
现在,我只是想让“辅助”测试用这个基类运行。最好的方法是什么
我还希望用户能够同时在“核心”和“辅助”中执行测试。我正试图找到一个解决办法,这将提供这一点
我发现与我的问题最接近的匹配是:
我尝试了直接在“目录”外执行此解决方案,但由于无法找到装置,我仍然收到了相同的问题。也就是说,我不相信我做得对
有什么想法吗?在这种情况下,我甚至不太确定哪个应该是我的执行目录。我的主要限制是“core/”肯定仍然是独立的。我找到了一个使用此结构运行的修复程序,并在另一个论坛上发布了结果:
有一个conftest冲突,我只能通过将常见装置移动到一个单独的文件并将它们分别导入每个必需的conftest来解决。我找到了一个使用此结构运行的修复程序,并将结果发布在另一个论坛上: 有一个conftest冲突,我只能通过将公共装置移动到一个单独的文件并将它们分别导入每个必需的conftest来解决