Closed
Description
I'm not sure if this is a bug or an expected behavior, but test methods inherited from a parent test class fail if they are staticmethod
s. This does not happen with regular methods.
An example to reproduce:
@pytest.fixture
def a():
return 1
@pytest.fixture
def b():
return 2
class TestBase:
def test_base(self, a, b):
assert a + b == 3
@staticmethod
def test_base_static(a, b):
assert a + b == 3
class TestChild(TestBase):
def test_child(self, a, b):
assert a + b == 3
@staticmethod
def test_child_static(a, b):
assert a + b == 3
Test session output:
$ pytest -v
platform linux -- Python 3.9.0, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
<...>
collected 6 items
TestBase::test_base PASSED [ 16%]
TestBase::test_base_static PASSED [ 33%]
TestChild::test_base PASSED [ 50%]
TestChild::test_base_static FAILED [ 66%]
TestChild::test_child PASSED [ 83%]
TestChild::test_child_static PASSED [100%]
<...>
pyfuncitem = <Function test_base_static>
@hookimpl(trylast=True)
def pytest_pyfunc_call(pyfuncitem: "Function") -> Optional[object]:
testfunction = pyfuncitem.obj
if is_async_function(testfunction):
async_warn_and_skip(pyfuncitem.nodeid)
funcargs = pyfuncitem.funcargs
testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}
> result = testfunction(**testargs)
E TypeError: test_base_static() missing 1 required positional argument: 'a'
.../python3.9/site-packages/_pytest/python.py:184: TypeError
FAILED TestChild::test_base_static - TypeError: test_base_static() missing 1 required positional argument: 'a'
I checked testargs
, and the value is {'a': 1, 'b': 2}
(as expected) for every test item except for TestChild::test_base_static
where it is {'b': 2}
.
I suspected this isinstance
check, but when I replaced cls.__dict__.get(name, None)
with getattr(cls, name, None)
, it got even worse.
$ pip list
Package Version Location
---------- ------- ------------------------
attrs 20.3.0
iniconfig 1.1.1
packaging 20.4
pip 20.1.1
pluggy 0.13.1
py 1.9.0
pyparsing 2.4.7
pytest 6.1.2
setuptools 46.4.0
six 1.15.0
toml 0.10.2
wheel 0.34.2
$ pytest --version
pytest 6.1.2
$ grep -P '^(VERSION|NAME)=' /etc/os-release
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"