Skip to content

--cov as final element in addopts creates confusing behaviour #701

Open

Description

@ego-thales

Hi there,

I'm not sure this is a pytest bug but here it goes.

I have the following pyproject.toml configuration

[tool.pytest.ini_options]
testpaths = "test"
addopts = [
    "--import-mode=importlib",  # Recommended by pytest
    "--cov",
]

so I can easily use

pytest

to start my tests.

Now when I want to test only a specific file, doing

pytest specific/file.py

ignores the argument, while in the mean time, passing another optional argument such as the following

pytest -s specific/file.py

works perfectly as intended.

Am I missing a piece or is this a pytest-side problem?

Thanks in advance!

All the best.
Élie

Activity

  1. RonnyPfannschmidt commented on Jun 20, 2025

    @RonnyPfannschmidt
    Member

    I believe a argument to the cov option is missing

  2. RonnyPfannschmidt commented on Jun 20, 2025

    @RonnyPfannschmidt
    Member

    Indeed the cov option expects a argument

    Provide that or remove it from addopts

  3. eliegoudout commented on Jun 20, 2025

    @eliegoudout

    Thanks for your answer.

    I think then it might be a problem of pytest-cov since they recommend not giving it when using source configuration:

    If you have multiple sources it might be easier to set those in .coveragerc and always use --cov (without a value) instead of having a long command line with --cov=pkg1 --cov=pkg2 --cov=pkg3 ....

    Do you +1 or am I missing something else? If appropriate, I will maybe ask for help or open an issue directly at pytest-cov.

  4. RonnyPfannschmidt commented on Jun 20, 2025

    @RonnyPfannschmidt
    Member

    I believe the examples put options instead of filename after it so it works

    This definitely needs better docs

  5. RonnyPfannschmidt commented on Jun 20, 2025

    @RonnyPfannschmidt
    Member

    I checked the docs they use the --cov=somename variant

    We probably need a warning

  6. eliegoudout commented on Jun 21, 2025

    @eliegoudout

    I think a warning could be a first step indeed.

    Also, I think my case can easily be solved simply by swapping the order of my options in my config file so that --cov is already followed by another option? I cannot test this week-end.

    Finally, maybe they could use a special character to mean "use from source" (e.g. --cov=!).
    It could even be automatically added if configuration file ends with --cov, but this may not be a desirable behaviour.

  7. changed the title [-]Test only specific files[/-] [+]--cov as final emelent in addopts xcreates confusing behaviour[/+] on Jun 21, 2025
  8. RonnyPfannschmidt commented on Jun 21, 2025

    @RonnyPfannschmidt
    Member
    • determine if we can add a warning
    • document the error
  9. ionelmc commented on Jun 27, 2025

    @ionelmc
    Member

    Would using "--cov=" (just that) achieve the desired result? It would not eat up following arguments.

  10. ionelmc commented on Jun 27, 2025

    @ionelmc
    Member

    If not maybe we could implement a special value, eg: --cov=* or --cov=?

  11. changed the title [-]--cov as final emelent in addopts xcreates confusing behaviour[/-] [+]--cov as final element in addopts creates confusing behaviour[/+] on Jul 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions