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

Using yield_fixture as function causes fixtures to run twice #1481

Closed
Wilfred opened this issue Mar 24, 2016 · 12 comments
Closed

Using yield_fixture as function causes fixtures to run twice #1481

Wilfred opened this issue Mar 24, 2016 · 12 comments
Labels
type: backward compatibility might present some backward compatibility issues which should be carefully noted in the changelog type: bug problem that needs to be addressed

Comments

@Wilfred
Copy link

Wilfred commented Mar 24, 2016

import pytest

def inner_function():
    print "begin inner_function!"
    yield
    print "end inner_function!"

my_fixture = pytest.yield_fixture(scope='session', autouse=True)(inner_function)

def test_foo():
    assert True

Running pytest -s foo.py gives:

foo.py begin inner_function!
begin inner_function!
.end inner_function!
end inner_function!
========= 1 passed in 0.01 seconds ============

It seems that py.test is erroneously thinking that inner_function is a fixture. Sometimes (because I want to use my function in other contexts too) I want to use yield_fixture as a function and pass my target function directly. When this happens, py.test tries to run my fixture twice.

@nicoddemus
Copy link
Member

From #1480 it seems you are using pytest 2.6... could you try upgrading and see if you still have this problem?

@RonnyPfannschmidt
Copy link
Member

the fixture decorator does indeed modify the function in-place by adding a new attribute

@RonnyPfannschmidt RonnyPfannschmidt added type: bug problem that needs to be addressed type: backward compatibility might present some backward compatibility issues which should be carefully noted in the changelog labels Mar 24, 2016
@Wilfred
Copy link
Author

Wilfred commented Mar 24, 2016

I can reproduce this on pytest 2.9.1 too.

@RonnyPfannschmidt
Copy link
Member

the current implementation cannot support usage as non-decorator, since it modifies the function on passtrough

@nicoddemus
Copy link
Member

Should we close this?

@RonnyPfannschmidt
Copy link
Member

No, its still "broken" as far as I can tell

@nicoddemus
Copy link
Member

I understand, I was asking more in the sense of "closing as not supported"

@RonnyPfannschmidt
Copy link
Member

well, we should issue a pytest warning in case more than one active fixture has the same identity

@nicoddemus
Copy link
Member

This is fixed by #2849

@RonnyPfannschmidt
Copy link
Member

@nicoddemus nope, #2849 fixes double use of the decorator

this is about naming the decorator result and the original function differently
in that case pytest find 2 fixtures with the same identity but different name in the globals

@nicoddemus
Copy link
Member

@RonnyPfannschmidt OK, thanks for the clarification

@nicoddemus
Copy link
Member

nicoddemus commented Nov 8, 2018

This has been fixed, inner_function is called only once now:

test-foo.py begin inner_function!
.end inner_function!
                                                                                [100%]

==================================== 1 passed in 0.04 seconds =====================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: backward compatibility might present some backward compatibility issues which should be carefully noted in the changelog type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

3 participants