1

I have a PyTest based test project which utilizes base-classes with fixtures. This "core" project is functioning as expected.

I would also like to have other "side" test projects in a different folder structure utilize these base-classes and fixtures. The use-case is that we have one core development project and many other projects which utilize this core project. We want "core" tests available to everyone, but we don't want the other project-specific tests cluttering the core.

Here is a skeleton tree structure:

directory/
├── core/
|   ├── conftest.py
|   └── folder1/
|       └── base_class.py
|       └── conftest.py
|       └── tests/
|           └── test_core.py
├── ancillary/
|   └── test_ancillary.py

Here are the contents of the python files:

# 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)

As mentioned above, this 'core' part works perfectly fine when I execute pytest from the 'core' directory.

The complication comes in when I try to use my base-class with test_ancillary.py. Here are its contents:

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)

In my original setup I was running PyTest directly from the 'core/' directory and pointing it at 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
...

So it cannot find my fixtures ("fixture 'true_fixture' not found").

Right now I am just trying to get the 'ancillary' tests to run with this base class. What is the best way to do that?

I would also like users to be able to execute tests in 'core' and in 'ancillary' at the same time. I am trying to find a solution which will provide this.

The closest match I found to my question was here: pytest fixtures in a separate directory

I tried this solution with directly executing out of the 'directory', but I still received the same issue with not being able to find fixtures. That said, I am not convinced that I did it correctly.

Any ideas? I am not even too sure which should be my executing directory in this case. My main restraint is that 'core/' should definitely still stand alone.

1 Answers1

0

I found a fix to run with this structure and posted the results in another forum: https://github.com/pytest-dev/pytest/issues/5858

There was a conftest conflict which I could only resolve by moving common fixtures to a separate file and importing them separately into each required conftest.