New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

staticmethod functions are not collected for testing #2528

Closed
waisbrot opened this Issue Jun 25, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@waisbrot
Contributor

waisbrot commented Jun 25, 2017

When using classes to group functions, I'm interested in this pattern:

"test_http_requests.py"

class TestGet:
  @staticmethod
  def test_simple_get():
    pass

  @staticmethod
  def test_query_string():
    pass

class TestPut:
  @staticmethod
  def test_no_data():
     pass

  @staticmethod
  def test_binary_data():
    pass

Using the @staticmethod decorator makes it clear that I'm not using the class for anything but grouping.

Unfortunately, pytest fails to collect any method so decorated. If I remove the decorator and add the self argument in the above example, pytest collects as expected.

Pytest 3.1.2, Python 2.7.13, no virtualenv, MacOS 10.12.5

@ceridwen

This comment has been minimized.

Contributor

ceridwen commented Aug 17, 2017

I was attempting to use this, and it doesn't seem to be working quite right with fixtures. Running the following code,

import pytest

@pytest.fixture
def foo():
    return 'foo'

def test_foo(foo):
    assert foo == 'foo'

class TestFoo:
    @staticmethod
    def test_foo(foo):
        assert foo == 'foo'

Produces the following error:

pyfuncitem = <Function 'test_foo'>

    @hookimpl(trylast=True)
    def pytest_pyfunc_call(pyfuncitem):
        testfunction = pyfuncitem.obj
        if pyfuncitem._isyieldedfunction():
            testfunction(*pyfuncitem._args)
        else:
            funcargs = pyfuncitem.funcargs
            testargs = {}
            for arg in pyfuncitem._fixtureinfo.argnames:
                testargs[arg] = funcargs[arg]
>           testfunction(**testargs)
E           TypeError: test_foo() missing 1 required positional argument: 'foo'

../../Library/Python/3.6/lib/python/site-packages/_pytest/python.py:143: TypeError
======================= 1 failed, 1 passed in 0.32 seconds =======================

The first test_foo() function passes correctly.

@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Aug 17, 2017

Thanks @ceridwen for the report. Moving this discussion to #2699 as this is a new issue.

Closing this as #2531 was merged some time ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment