-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Using fixture decorator as a function #2001
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
Comments
Hi, sorry for the delay. I think the simplest solution is to decorate your class with the @pytest.mark.usefixtures('unique_bed')
class TestConfig(unittest.TestCase):
... |
But I don't have a class declaration. It's generated using |
Oh my bad, completely missed this line: return mark.usefixtures(test_case_class, "unique_bed") If you add this to your test case: print(ConfigTestCase) You will see that it is not really the class being returned, but a
What's happening is: A decorator is a syntax sugar, so the two are equivalent: @dec
class T:
pass
# Equivalent to:
T = dec(T) A decorator with an argument is a little different in that it should return a callable which is then applied to the class: @dec('param')
class T:
pass
# Equivalent to:
T = dec('param')(T) So you have to change your example to: return mark.usefixtures("unique_bed")(test_case_class) |
This was very helpful. Thanks! |
I have an autogenerated unit test class that I'd like to decorate with a fixture for some injected functionality. Knowing that decorators are "just functions" led me to something like this...
However this leads to pytest being unable to find the "unittest.TestCase" base classes.
Perhaps my python-fu is just not strong enough to see the obvious answer, but what's the solution here? How can I keep my existing autogenerated test classes and add a fixture decorator programmatically?
(Editted by @nicoddemus: updated syntax highlight)
The text was updated successfully, but these errors were encountered: