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

document how a fixture can depend on another fixture behaviour, but not on its return value #12923

Open
kalfa opened this issue Oct 28, 2024 · 2 comments

Comments

@kalfa
Copy link

kalfa commented Oct 28, 2024

I am looking at #1014 and related bugs, there are few on the subject.

It is still unclear to me what do to when a fixture uses another fixture, but it does not refer to it in the code.

From 2013 (the older but on the subject I found), when the ticket was raised, things have changed and now we have really strong type checking and linters.

An example of the code which would raise linters concerns is:

@fixture
def bar(monkeypatch):
  with monkeypatch() as mp:
     mp.setenv('MYVAR', 1):
     yield

@fixture
def foo(monkeypatch, bar):
    do_something_depening_on_MYVAR()

foo does not know what bar do, and does not care what bar return value is. the code is unaware.

foo depends on the behaviours of bar, but does not refer to it. so any modern linter would raise it as a problem: unused-arguement

This is solved in test cases with usefixtures() which injects them without putting them in the scope fo the test code.

how is it supposed to work with fixtures?

a valid solution to this ticket to me would be:

  • document it in the fixture page
  • and/or add a programmatic way to solve the problem, equivalent to usefixtures()
@The-Compiler
Copy link
Member

The-Compiler commented Oct 28, 2024

IMHO the most obvious option is to configure your linter to not complain about unused arguments in your tests/ folder - it looks like e.g. ruff supports per-file-ignores (so does flake8), and per-directory config files.

Also see astral-sh/ruff#4673

@kalfa
Copy link
Author

kalfa commented Oct 28, 2024

I already disable it, but with I am becoming slowly against disabling checks, as the number of things to disable grows.

The problem is that it is not a solution, but a workaround for a lack of something else in pytest, hence this bug report (which could become a feature request as well).

Disabling as workaround potentially hides other problems.

It is not a devastating lack, but nevertheless a lack of the ability to describe a dependency that way, and the solution relies on external abilities to cover by the local lack.

either I disable entirely unused args detection, or I have to disable 1:1 each fixture, being very verbose.

I currently use the latter, to minimise hiding other problems, but increasing verbosity and non python code.

pytest.fixture(..., uses_fixtures: list[str], ...) would be an easy (I guess?) solution to the problem, with the same verbosity of the workaround above, but actually solving the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants