Replies: 1 comment
|
Hi @xor-xor
So if you write: @pytest.fixture(autouse=True)
def my_fixture():
return 123then inside ordinary module code, To receive def test_something(my_fixture):
assert isinstance(my_fixture, int)Using If you actually need the fixture value, keeping it as an explicit test/fixture argument is the idiomatic approach and also works best with type annotations and IDEs. Please mark this answer as accepted if it helped, thank you. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Let's say that I need to test some function, which explicitly checks the type of its input, and I have no control over this behavior. Also, let's say, that I need a fixture for such test:
So far, so good - everything works as expected and I get
<class 'int'>in the output.BUT!
If I change this fixture to
autouse=True(because e.g. I have a lots of tests like the one above and I just don't want to repeat the same declaration over and over):...then everything breaks with
AssertionError: sorry, integers only!(well, obviously), becausemy_fixtureis now<class 'function'>.Assuming that this is a feature, not a bug, my question is: what is the "canonical" way of handling such situations?
EDIT: actually, I've just realized that this type assertion here is irrelevant - this
autouse=Truecase will just "explode" onreturn x + 1for exactly the same reason.All reactions