在上一级目录中调用测试时,发现PyTest装置

问题描述 投票:1回答:1

我有一个基于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)

如上所述,当我从'core'目录执行pytest时,此'core'部分工作正常。

当我尝试将基类与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 /”绝对应该仍然独立存在。

我有一个基于PyTest的测试项目,该项目使用带有夹具的基类。该“核心”项目正在按预期运行。我也想在其他...中具有其他“侧面”测试项目...

python pytest fixtures
1个回答
0
投票

我找到了与此结构一起运行的修复程序,并将结果发布在另一个论坛上:https://github.com/pytest-dev/pytest/issues/5858

© www.soinside.com 2019 - 2024. All rights reserved.