Skip to content
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

Pytest fails on marking of input parameter if it is a method of a class #3048

Closed
rabodaber opened this issue Dec 18, 2017 · 3 comments
Closed
Labels
topic: parametrize related to @pytest.mark.parametrize type: question general question, might be closed after 2 weeks of inactivity

Comments

@rabodaber
Copy link

rabodaber commented Dec 18, 2017

Hello!

Python 2.7.10
Windows 10 1703, pytest 3.3.1
Pip list: https://pastebin.com/WKpwf3eW

Small bug. When I'm trying to skip method passed as parameter to a test funny things are going on.

  1. OK Example
import pytest


@pytest.mark.parametrize('param', [
    pytest.mark.skip(reason=u'reason')(lambda a, b: (a, b)),
    lambda a, b: (a, b)
])
def test_test(param):
    print param(123, 321)

OK here. Output:

(123, 321)
.

Everything is fine here

  1. Ignore skip example
import pytest

def foo(param1, param2):
    return param1, param2


def baz(param1, param2):
    return param1, param2


@pytest.mark.parametrize('param', [
    pytest.mark.skip(reason=u'reason')(foo),
    baz
])
def test_test(param):
    print param(123, 321)

Both tests were run. Output:

(123, 321)
.(123, 321)
.

  1. Fail example
import pytest


class TestClass(object):
    def foo(self, param1, param2):
        return param1, param2

    def baz(self, param1, param2):
        return param1, param2


@pytest.mark.parametrize('param', [
    pytest.mark.skip(reason=u'reason')(TestClass().foo),
    TestClass().baz
])
def test_test(param):
    print param(123, 321)

Fails. Output:

test.py:16: in
pytest.mark.skip(reason=u'reason')(TestClass().foo),
C:\Python27\lib\site-packages_pytest\mark.py:401: in call
store_legacy_markinfo(func, self.mark)
C:\Python27\lib\site-packages_pytest\mark.py:439: in store_legacy_markinfo
setattr(func, mark.name, holder)
E AttributeError: 'instancemethod' object has no attribute 'skip'

Works fine with:
lambda a,b : TestClass().foo(a,b)

Thanks in advance =)

@pytestbot pytestbot added the topic: parametrize related to @pytest.mark.parametrize label Dec 18, 2017
@pytestbot
Copy link
Contributor

GitMate.io thinks the contributor most likely able to help you is @RonnyPfannschmidt.

@RonnyPfannschmidt
Copy link
Member

this is a known issue rooted in how python works - marks cannot tell apart if a normal function is supposed to be decorated or not if its the only thing being passed

for that reason we introduced 2 different mechanisms to rectify the problem

a) pytest.param(somefunction, marks=pytest.mark.mymark) - for parametrize
b) pytest.mark.foo(1).with_args(some_function) - for extending a mark

@RonnyPfannschmidt RonnyPfannschmidt added the type: question general question, might be closed after 2 weeks of inactivity label Dec 18, 2017
@rabodaber
Copy link
Author

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: parametrize related to @pytest.mark.parametrize type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants