Algorithm modules ↔ pytest doctest runner: Edge‑case coverage question
0 reputation · 05 Dec 2023, 20:38 UTC
0 reputation · 05 Dec 2023, 20:38 UTC
Goal: verify that each algorithm module’s doctest examples provide enough regression safety to catch common edge cases while keeping the code readable for learners.
Constraint: the repository relies on pytest --doctest-modules as its primary test harness, so doctests must simultaneously serve as documentation and as the main verification mechanism. Contributors must decide whether to prioritize pedagogically simple examples or to add exhaustive checks for inputs such as empty collections, negative numbers, or overflow conditions, a balance that varies across modules.
Questions: Should maintainers supplement doctests with dedicated unit or property‑based tests for randomized scenarios? Can a minimum edge‑case coverage threshold be enforced through CI without harming the educational focus? How can the project systematically detect and address coverage gaps in existing modules?
31275 reputation · 06 Dec 2023, 07:29 UTC
Yes – keep doctests for clear, pedagogical examples, but add focused unit or property‑based tests for edge cases. Enforce a modest coverage threshold (e.g., 80 %) in CI, and use the coverage report to spot gaps. Ask for the repo’s current doctest line‑coverage to decide if extra tests are urgent.
Doctests are great for illustrating how a function should be used, but they’re limited in expressing exhaustive edge‑case logic. They run only the examples written in the docstring, so an empty list, negative numbers, or overflow conditions will only be caught if the author explicitly added a snippet. In an educational repository, adding many complex doctest cases can clutter the documentation and distract learners.
tests/ directory so contributors can run them locally with pytest without touching the doctest examples.Pytest’s --doctest-modules flag can be combined with pytest‑cov to measure line coverage of the doctest runs. A typical CI step looks like:
pytest --doctest-modules --cov=algorithm_module --cov-report=term-missing
In the workflow file add a failure condition:
- name: Run tests
run: pytest --doctest-modules --cov=algorithm_module --cov-fail-under=80
This keeps the build green as long as doctest coverage stays above 80 %. Adjust the threshold after reviewing historical data to avoid discouraging contributions.
pytest --doctest-modules --cov=algorithm_module --cov-report=html.htmlcov/index.html and look for modules with -- or low percentages.Could you share the current doctest line‑coverage percentage across the algorithm_module package? Knowing that number will help determine whether the threshold should be tightened or if the repository is already on track.
pytest --doctest-modules --cov=algorithm_module --cov-report=term locally to see the current coverage.tests/*.py files are discovered by pytest by adding import pytest at the top of one test file.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.