I have a class that I want to use in various test cases/files to test certain conditions. Something like the following:
class MyTester:
@staticmethod
def does_string_apply(my_string: str) -> bool:
return my_string == 'Hello World'
I want to use that class in various test cases among various test files, which is why I defined it in conftest and provided a fixture to access that class.
@pytest.fixture(name=my_tester)
def fixture_my_tester() -> type[MyTester]:
return MyTester
That fixture is used in the test cases, but the class name is not imported. Therefore, I do not know how to type hint the fixture:
def test_something(my_tester: type['What goes here?']) -> None:
assert my_tester.does_string_apply("Hello")
So just a bit of background on Pytest for future readers - in a test you can specify an argument name, that can connect to a fixture in a different file, so the class may not necessarily need to be imported (outside of typing).
The short answer is, simply:
from .conftest import MyTester
It may be that conftest isn't accessible in this way, in which case you could put MyTester
into a utils section of the package.
If circular imports are a concern with the testing system, you can also import it like this:
if typing.TYPE_CHECKING:
from .conftest import MyTester
I get the feeling that pytest was not originally designed with typing in mind, but it does expose enough options that it's possible to make it work. Hope this helps!