Skip to content

Fix: Configurable behavior when no tests match#687

Open
OmChillure wants to merge 6 commits intokarva-dev:mainfrom
OmChillure:fix-configurable-behaviour-when-no-tests-match-no-test
Open

Fix: Configurable behavior when no tests match#687
OmChillure wants to merge 6 commits intokarva-dev:mainfrom
OmChillure:fix-configurable-behaviour-when-no-tests-match-no-test

Conversation

@OmChillure
Copy link
Copy Markdown
Contributor

Fixes #553

Summary

This PR implements configurable behavior when no tests are collected, letting users control the exit status via --no-tests:

  • --no-tests=fail — exit non-zero with an error message (default)
  • --no-tests=pass — exit 0 silently, useful in CI where filters may legitimately match nothing
  • --no-tests=warn — exit 0 and print a warning

A NoTestsMode enum (pass | warn | fail) was added to karva_metadata and wired through the CLI, options, and settings. The no_tests_matched_filters helper checks whether zero tests ran and dispatches to the correct mode. The default is fail, preserving existing behavior.

The option can also be set persistently in pyproject.toml:

[tool.karva.test]
no-tests = "warn"

NoTests follows the same pattern as RunIgnored and TerminalColor with an as_str method and value_name = "WHEN" on the clap argument for cleaner --help output.

Test Plan

Added filterset_test_no_matches, filterset_test_no_matches_pass, and filterset_test_no_matches_warn integration tests covering all three modes against a file with no test functions. Updated the filterset_tag_no_matches snapshot to reflect the new default fail behavior.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 17, 2026

Merging this PR will improve performance by 10.96%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime karva_benchmark 24.7 s 22.3 s +10.96%

Comparing OmChillure:fix-configurable-behaviour-when-no-tests-match-no-test (3e62a94) with main (faef1bf)

Open in CodSpeed

@OmChillure OmChillure marked this pull request as draft April 17, 2026 18:05
@OmChillure OmChillure marked this pull request as ready for review April 17, 2026 18:33
@MatthewMckee4
Copy link
Copy Markdown
Member

Thank you

I've just found out that neatest has some nice functionality

      --no-tests <ACTION>
          Behavior if there are no tests to run [default: auto]

          Possible values:
          - auto: Automatically determine behavior, defaulting to fail
          - pass: Silently exit with code 0
          - warn: Produce a warning and exit with code 0
          - fail: Produce an error message and exit with code 4

          [env: NEXTEST_NO_TESTS=]

And auto essentially means

  • No filters: treat "no tests ran" as fail.
  • Filters given: treat it as pass.

So if we could add the env var KARVA_NO_TESTS, and update both enums to support this auto variant that checks based on filters, just if we have a filters flag set should work. That would be great. Also if we could match the docs in the cli too.

Weirdly neatest doesn't support this in configuration file, but I like that you've added that, we should definitely support config.

@OmChillure
Copy link
Copy Markdown
Contributor Author

Thank you

I've just found out that neatest has some nice functionality

      --no-tests <ACTION>
          Behavior if there are no tests to run [default: auto]

          Possible values:
          - auto: Automatically determine behavior, defaulting to fail
          - pass: Silently exit with code 0
          - warn: Produce a warning and exit with code 0
          - fail: Produce an error message and exit with code 4

          [env: NEXTEST_NO_TESTS=]

And auto essentially means

  • No filters: treat "no tests ran" as fail.
  • Filters given: treat it as pass.

So if we could add the env var KARVA_NO_TESTS, and update both enums to support this auto variant that checks based on filters, just if we have a filters flag set should work. That would be great. Also if we could match the docs in the cli too.

Weirdly neatest doesn't support this in configuration file, but I like that you've added that, we should definitely support config.

yup added this

@OmChillure
Copy link
Copy Markdown
Contributor Author

OmChillure commented Apr 18, 2026

Hey @MatthewMckee4 the "pre commit" ci is failing, I guess there is a bug in that test or smtg? Can you take a look please !!

Thank you

@MatthewMckee4
Copy link
Copy Markdown
Member

It's just failing due to formatting

@OmChillure
Copy link
Copy Markdown
Contributor Author

OmChillure commented Apr 18, 2026

It's just failing due to formatting

ahh its was just cargo fmt, i thought it was something else, my fault :(

fixed if you can check ! thanks

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

Successfully merging this pull request may close these issues.

Configurable behavior when no tests match (--no-tests)

2 participants