-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Override a fixture on a test module level:with "name" key_word_arg, its may not Override #12952
Comments
We should error on such conflicts |
I'd note that doing this might conceivably make sense if the one requests the other, e.g.: import pytest
@pytest.fixture(name="fix")
def fix1():
print('fix1')
@pytest.fixture(name="fix")
def fix2(fix):
print('fix2')
def test(fix): pass I'm not necessarily saying we shouldn't make this an error, but it's something to consider.
Correct, this is because we use |
At first glance, it might seem meaningful, but it feels counterintuitive. In scenarios where the name import pytest
@pytest.fixture()
def fix1():
print('fix1')
@pytest.fixture()
def fix2():
print('fix2')
@pytest.fixture()
def fix(fix1, fix2):
pass
def test(fix):
pass On the other hand, the following implementation seems overly contrived (especially when both fixtures are in the same file): @pytest.fixture(name="fix")
def fix1():
...
@pytest.fixture(name="fix")
def fix2(fix):
... According to pytest's current implementation, the unique advantage of this approach is that it creates a fixture that cannot be directly request by any test, It can only be request by another fixture with the same name but declared later. Just my thoughts:
|
I definitely agree it feels odd in isolation. However, if we consider that pytest supports to concept of overriding fixtures, the question becomes, should we disallow overriding within a single file (module)? My feeling on this is that we shouldn't disallow it, mostly because I dislike special cases and I like orthogonality. My feeling would be different if this were a common issue causing confusion, but at least for me, this is the first time I see it come up. And actually the original reporter (@steadyfirmness) seems to dislike only the sorting behavior, not that it's allowed. WDYT? BTW @dongfangtianyu, if you feel like it, a PR to remove the sorting behavior of |
Same name in Same file should be conflict unless someone demonstrates a valid use case that's not a thedaylywtf submission or esoteric code golfing |
Well, there isn't really a reason for anyone to write this Python file: def func():
pass
def func():
pass yet I still don't think Python should disallow it. The downsides of disallowing is added complexity, and preventing any creative usage of it. The upside is to prevent mistakes, but I don't think it's a common mistake. |
Yes, as he(@bluetech ) said, treating test code as a project and solving scalability issues by adding code is the best approach, as shown below.It can better reflect the process of operation:
BTW: |
Pytest should warn or error on this as it's always a user error Linters catch function override I consider python allows redefiniton a strawman as it's either used for a valid pattern like overrides/properties or it's incorrect code As far as I'm concerned in same module name conflicts we know something is wrong and we must make it known to the user |
@bluetech cool, your description hits the nail on the head. As described in the documentation, coverage targets different levels. So the question becomes: should we prohibit repeated coverage at the same level? If such a requirement does exist, it seems more appropriate to implement it using Python code (if after
Functions can be used completely before being overridden, same-level fixtures don't seem to be like this, they can only be used by the fixture that overwrites them。 |
description
I have some fixtures in the same test file, and I need to reload one of them as following:
what i want
When i call “f1” its return 0 because its actually executed “f0” as following:
pytest result
But this is fail for test
env
os:mac 15.0.1
python:3.12.4
pytest:8.3.2
more detail
When I change name ‘f0’ -> 'f2', as following, it works well:
So I summarized that the overloading of fixtures in the same file is not in the order of bytecode compilation, but in the lexicographic order of functions, resulting in the phenomenon where f0 cannot reload f1 while f2 can reload f1.
I'm not sure if this is a feature or a bug
The text was updated successfully, but these errors were encountered: