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.mark not evaluated with pytest_generate_tests #2401

Closed
spaceone opened this issue May 11, 2017 · 6 comments
Closed

pytest.mark not evaluated with pytest_generate_tests #2401

spaceone opened this issue May 11, 2017 · 6 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@spaceone
Copy link
Contributor

cat > test_foo.py

import pytest


def test_foo(a, b):
        assert a == 'a'
        assert b == 'b'


def pytest_generate_tests(metafunc):
        data = [[pytest.mark.xfail('reason=foo')('A'), 'b']]
        metafunc.parametrize('a,b', data)

results in:

# py.test test_foo.py                                                                                                                                                                                                                                            
============================================================================================================================== test session starts ==============================================================================================================================
platform linux2 -- Python 2.7.9 -- py-1.4.25 -- pytest-2.6.3
collected 1 items 

test_foo.py F

=================================================================================================================================== FAILURES ====================================================================================================================================
________________________________________________________________________________________________________________________________ test_foo[a0-b] _________________________________________________________________________________________________________________________________

a = <MarkDecorator 'xfail' {'args': ('reason=foo', 'A'), 'kwargs': {}}>, b = 'b'

    def test_foo(a, b):
>       assert a == 'a'
E    assert <MarkDecorator 'xfail' {'args': ('reason=foo', 'A'), 'kwargs': {}}> == 'a'

test_foo.py:5: AssertionError
=========================================================================================================================== 1 failed in 0.02 seconds ===========================================================================================================================

Expected result:
test is marked as XFAILED.

@RonnyPfannschmidt
Copy link
Member

That's expected behavior, the mark must wrap around all parameters in the parameter set

In pytest 3.1 we will ship a better mechanism for marking parametersets

@spaceone
Copy link
Contributor Author

spaceone commented May 12, 2017

I build up a workaround:

def xfail(set_):
    fails = [x for x in set_ if isinstance(x, type(pytest.mark.xfail()))]
    if fails:
        data = [x.args[0] if isinstance(x, type(pytest.mark.xfail())) else x for x in set_]
        set_ = pytest.mark.xfail(**fails[0].kwargs)(data)
    return set_

data = [xfail(x) for x in data]

@spaceone
Copy link
Contributor Author

but I can't differentiate a condition from a value :-/ it's both stored in "args".

@RonnyPfannschmidt
Copy link
Member

@spaceone one you need to use actual tuples as arguments to encode the args that get extracted

the method used in pytest < 3.1 is pretty fragile

which is why we introduce https://github.com/pytest-dev/pytest/blob/features/_pytest/mark.py#L14 to support declaring it as pytest.param(my, arg, values, marks=[pytest.mark.mymark(...)

the linked code also contains the extraction logic which should give you some hints on how to write the workaround for current py.test

@RonnyPfannschmidt
Copy link
Member

but bascially, the extraction mechanism presumes that when given a mark, the last element of the args tuple is the parameter list for the parameterize call

and it will recurse on that

@RonnyPfannschmidt RonnyPfannschmidt added the type: question general question, might be closed after 2 weeks of inactivity label May 12, 2017
@spaceone
Copy link
Contributor Author

As you are working on it in further releases, this issue can be closed. I'll find my workaround.

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

No branches or pull requests

2 participants