Skip to content

logging: pytest should allow users to know why a test was deselected #13617

Description

@ssbarnea

Especially when using markers and complex testing scenarios, one would endup being surprised that some tests were deselected without a reason.

Very long time ago (3.x), pytest has some code that attempted to provide reason about this but this code was buggy and was removed.

While we might not want to pollute the default console logging, we would want to ensure that when running with -v (or more), decisions about marking tests as deselected are also logged, so users could figure out the likely wrong condition.

@The-Compiler I would like to work on this, but before starting to write any code, it would better to ensure that I am not trying to fix the wrong issue ;)

Activity

  1. added theissue type on Jul 24, 2025
  2. added
    topic: selectionrelated to test selection from the command line
    topic: marksrelated to marks, either the general marks or builtin
    plugin: loggingrelated to the logging builtin plugin
    pythonpull requests that update Python code
    on Jul 24, 2025
  3. self-assigned this
    on Jul 24, 2025
  4. The-Compiler commented on Jul 24, 2025

    @The-Compiler
    Member

    Not sure why me - sounds reasonable all in all, but I don't have a strong opinion on this (and I don't remember any such code being around, might have been before I got into pytest stuff).

  5. nicoddemus commented on Jul 24, 2025

    @nicoddemus
    Member

    decisions about marking tests as deselected are also logged, so users could figure out the likely wrong condition.

    To be clear, you mean deselecting tests via pytest_deselected?

  6. RonnyPfannschmidt commented on Jul 24, 2025

    @RonnyPfannschmidt
    Member

    The hook never supported passing the reason and something random else was printed
    .thats why i took it away

  7. ssbarnea commented on Jul 25, 2025

    @ssbarnea
    MemberAuthor

    To rephrase, is anyone is not against bringing back support for tracking deselection reason, I will attempt to make a change that is backwards compatible.

    @nicoddemus I am also referring to markers, I want to track reasons when they are used too, not only pytest_deselected hook.

  8. nicoddemus commented on Jul 25, 2025

    @nicoddemus
    Member

    To rephrase, is anyone is not against bringing back support for tracking deselection reason, I will attempt to make a change that is backwards compatible.

    I'm OK with it. Adding a reason parameter to pytest_deselected is simple and backward compatible. Other than that, you will need to update terminal.py to capture that reason and display it later somehow.

    @nicoddemus I am also referring to markers, I want to track reasons when they are used too, not only pytest_deselected hook.

    pytest.mark.skip, pytest.mark.skipif, and pytest.mark.xfail all have reason arguments already. When given, they show up in the test terminal summary. Do you mean something else?

  9. added a commit that references this issue on Jul 25, 2025
    caa85e3
  10. RonnyPfannschmidt commented on Jul 25, 2025

    @RonnyPfannschmidt
    Member

    We cannot add the parameter without call control in pluggy

    Its external called without a entrypoint we control

  11. RonnyPfannschmidt commented on Jul 25, 2025

    @RonnyPfannschmidt
    Member

    On second thought we can actually use the same hack that was used for implying psth vs fspath in hooks

  12. webknjaz commented on Jul 26, 2025

    @webknjaz
    Member

    Here's a radical though: why not redesign the deselection mechanics? Perhaps, have a new hook instead. Currently, making use of it is rather involved as it requires interfacing two tightly coupled things: https://github.com/aio-libs/multidict/blob/bc86d23/tests/conftest.py#L189-L210.

  13. RonnyPfannschmidt commented on Jul 26, 2025

    @RonnyPfannschmidt
    Member

    The idea of such a api was on the table back in 2016 there just was no followup

    In particular since doing this right requires retirement of the modifyitems hook

  14. Flamefire commented on Feb 20, 2026

    @Flamefire

    I just ran into this: Running a test suite, got no tests executed:

    collected 4 items / 4 deselected / 0 selected

    I haven't passed any deselection args on the cmdline so why tests were deselected is unknown to me. Even having a list of tests that were deselected would also be good.

    But having some (possibly verbose-only) output like "deselected xxx (not matching yy)" would be very helpful

  15. added 2 commits that reference this issue on Sep 16, 2026
    392c316
    3a5eb32
  16. added 3 commits that reference this issue on Oct 4, 2026
    99f3f90
    499828f
    c141776
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

plugin: loggingrelated to the logging builtin pluginpythonpull requests that update Python codetopic: collectionrelated to the collection phasetopic: marksrelated to marks, either the general marks or builtintopic: selectionrelated to test selection from the command line

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions