Is it discouraged to import conftest.py within test-modules?
Asked Answered
S

2

6

I am creating an object within conftest.py and use it in some fixtures. I also need to use this object within my test-modules. Currently I am importing conftest.py inside my test-modules and make use of that 'helper' object. I am pretty sure that this is not the recommended way. I am looking forward to your suggestions.

Thank you :)

Following is the dummy-coded version of my question:

conftest.py

import pytest

class Helper():

    def __init__(self, img_path:str):
        self.img_path = img_path

    def grayscale(self):
        pass

    def foo(self):
        pass

helper = Helper("sample.png")

@pytest.fixture()
def sample():
    return helper.grayscale()

test_module.py

import conftest

helper = conftest.helper

def test_method1(sample):
    helper.foo()
    ...
Seibert answered 11/8, 2021 at 7:54 Comment(5)
I would recommend moving your helpers to a helpers module or to fixtures -- conftest isn't really meant to be importedNotornis
You could also think about making helper a session-scoped fixture.Mohican
Thank you for your comments :) I applied your recommendation.Seibert
@anthonysottile I'm coming in a couple of years late... but can you expand a bit on your answer? From the preferred answer here #34466527 I understood that conftest is the correct place for defining shared fixtures. But if you want to have type annotated test functions then you need to import the types from somewhere. So would the best practice be to create a helper module purely to define these types?Biparty
ime typing tests (beyond check-untyped-defs) is a waste of time -- they're already going to fail during test execution so you're not really helping yourself. that said yes if you need to return complex enough types then define them outside of conftestNotornis
R
1

As already commented, I also handled such scenarios by fixtures before if I have a helper class in tests.

conftest.py

import pytest


class Helper():
    def __init__(self, img_path: str):
        self.img_path = img_path

    def grayscale(self):
        pass

    def foo(self):
        pass


@pytest.fixture(scope="session")
def helper():
    return Helper("sample.png")


@pytest.fixture()
def sample(helper):
    return helper.grayscale()

test_module.py

def test_method1(helper, sample):
    helper.foo()
Rideout answered 12/8, 2021 at 3:28 Comment(2)
How to implement type hints? Helper() is defined inside conftestSubaqueous
@Subaqueous did you find a solution for type hints?Travax
A
0

One small addition to @Niel's response;

If you want to have type hints, you can import it from conftest module.

test_module.py

# If test module is next to conftest
# from .conftest import Helpers 

# If test module is under a subpackage, which is my case
from ..conftest import Helpers

def test_method1(helper: Helpers, sample: str):
    helper.foo()
Abe answered 18/9, 2023 at 11:37 Comment(2)
Directories are not equivalent to Python modules.Conceptualize
@Conceptualize sorry, my bad. I meant packages.Abe

© 2022 - 2024 — McMap. All rights reserved.