【问题标题】:Issues finding PyTest fixtures when calling tests in upper level directory在上级目录中调用测试时发现 PyTest 固定装置的问题
【发布时间】:2020-01-14 17:49:21
【问题描述】:

我有一个基于 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,并将其指向 ancillary/:

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
...

所以它找不到我的灯具(“fixture 'true_fixture' not found”)。

现在我只是想让“辅助”测试与这个基类一起运行。最好的方法是什么?

我还希望用户能够同时在“核心”和“辅助”中执行测试。我正在努力寻找能够提供此功能的解决方案。

我找到的与我的问题最接近的匹配项在这里: pytest fixtures in a separate directory

我尝试了直接从“目录”执行此解决方案,但我仍然遇到了同样的问题,无法找到固定装置。也就是说,我不相信我做对了。

有什么想法吗?在这种情况下,我什至不太确定哪个应该是我的执行目录。我的主要限制是 'core/' 绝对应该是独立的。

【问题讨论】:

    标签: python pytest fixtures


    【解决方案1】:

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

    有一个 conftest 冲突,我只能通过将公共固定装置移动到一个单独的文件并将它们分别导入到每个所需的 conftest 中来解决。

    【讨论】:

      猜你喜欢
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      相关资源
      最近更新 更多