utkbansal
utkbansal

Reputation: 2817

Pytest not collecting staticmethod

I have a test class with some static methods along with normal methods. The problem is that pytest is not collecting static methods. I couldn't find anything in the documentation regarding this. How can I make it collect staticmethods too?

class TestFoo(object):
    @staticmethod
    def test_bar():
        assert 1 == 1

    def test_bar2(self):
        assert 1 == 1

In the above class, only test_bar2 is collected and test_bar() isn't.

I am running Python 2.7.13, pytest-3.1.2, py-1.4.34, pluggy-0.4.0

Plugins are xdist-1.17.1, leaks-0.2.2, cov-2.5.1

Upvotes: 3

Views: 3652

Answers (2)

Maxpm
Maxpm

Reputation: 25592

Pytest will collect and run test functions that are @staticmethods since v3.2.0 (2017-07-30).

class TestFoo:
    @staticmethod
    def test_bar():
        assert 1 == 1

Upvotes: 0

Julien Palard
Julien Palard

Reputation: 11596

When collecting test functions, pytest ensure each functions are callable.

But a staticmethod is not callable, from https://docs.python.org/3/reference/datamodel.html:

Static method objects are not themselves callable, although the objects they wrap usually are.

see:

>>> class TestFoo(object):
...     @staticmethod
...     def test_bar():
...         assert 1 == 1
... 
>>> hasattr(TestFoo.__dict__['test_bar'], '__call__')
False

For this to work, pytest itself should be modified to accept static method, I don't know if that's what they want, you can open an issue on their issue tracker on github if you think you really need it.

Why do you think static methods are a solution? For which problem exactly?

Upvotes: 5

Related Questions