You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don't know whether it is idiomatic or not, but I use the following approach to parametrize pytest tests. Tests are defined as method in a class. Parameters are defined as fixtures in a class. Fixtures are overridden in subclasses to run tests with different parameters.
For example:
importpytestclassTestA:
@pytest.fixturedeffoo(self) ->int:
return42deftest_foo(self, foo: int) ->None:
assertfoo>0@pytest.fixturedefbar() ->str:
return"abc"classTestB(TestA):
@pytest.fixturedeffoo(self, bar: str) ->int: # line 20returnlen(bar)
TestA.test_foo and TestB.test_foo are executed with different values of foo. The problem is that fixtures can depend on other fixtures, and signatures for methods defining fixture can be different. MyPy complains:
t.py:20: error: Signature of "foo" incompatible with supertype "TestA"
t.py:20: note: Superclass:
t.py:20: note: def foo(self) -> int
t.py:20: note: Subclass:
t.py:20: note: def foo(self, bar: str) -> int
It is not real error in the user code that the methods have different signatures. These methods are not called directly. They are called by pytest which provides all arguments.
I can add "# type: ignore", but I do not want to lose static type checking completely. It is useful to check that the type of the parameter matches the returning type of corresponding fixture function.
I don't know whether it is idiomatic or not, but I use the following approach to parametrize
pytest
tests. Tests are defined as method in a class. Parameters are defined as fixtures in a class. Fixtures are overridden in subclasses to run tests with different parameters.For example:
TestA.test_foo
andTestB.test_foo
are executed with different values offoo
. The problem is that fixtures can depend on other fixtures, and signatures for methods defining fixture can be different. MyPy complains:It is not real error in the user code that the methods have different signatures. These methods are not called directly. They are called by pytest which provides all arguments.
I can add "# type: ignore", but I do not want to lose static type checking completely. It is useful to check that the type of the parameter matches the returning type of corresponding fixture function.
mypy 0.961 (compiled: yes)
pytest 7.1.2
Python 3.9.13
The text was updated successfully, but these errors were encountered: