This project showcases a modern, robust, and efficient configuration for automating tests, ensuring code quality, and performing advanced static analysis in Python. It includes test coverage, complexity analysis, security checks, spell checking, and Mutation Testing.
UnitTest_demo/
├── .pre-commit-config.yaml # Pre-commit configuration for automated checks
├── pyproject.toml # Centralized configuration for Python tools
├── requirements.txt # Project dependencies
├── scripts/
│ └── mutmut_check.py # Script to run and validate Mutation Testing (mutmut)
├── src/
│ └── pokemon.py # Project source code
├── tests/ # Automated tests with pytest
├── Logs/
│ └── mutmut_survivors.md # Detailed report of surviving mutations
└── htmlcov/ # Visual report of code coverage
- ruff: A fast linter and formatter that combines features from
flake8,black, andisort, ensuring high code quality and style compliance. - pytest: A framework for running unit, integration, and functional tests in Python.
- pytest-cov: A
pytestplugin that measures code coverage and generates detailed reports. - hypothesis: A library that enables property-based and randomized testing.
- mutmut: A Mutation Testing tool that automatically modifies source code to detect weaknesses in test suites.
- codespell: Detects and automatically corrects common spelling errors in source code.
- bandit: A tool for identifying security issues in Python code via static analysis.
- xenon: Analyzes the cyclomatic complexity of Python code and ensures functions remain maintainable.
python3 -m venv .venv
source .venv/bin/activatepip install -r requirements.txtchmod +x scripts/mutmut_check.pypre-commit installpytestpytest --cov=src --cov-report=term-missing --cov-report=htmlmutmut run
mutmut results > Logs/mutmut_survivors.mdpython scripts/mutmut_check.pybandit -c bandit.yaml -r src/codespell src/xenon --max-absolute B --max-modules B --max-average A src/Pre-commit automatically enforces code quality before each commit by running:
- ruff: Checks style, formats code, and fixes import order.
- pytest: Runs automated tests.
- pytest-cov: Validates a minimum coverage threshold of 80%.
- codespell: Reviews source code for spelling errors.
- bandit: Executes security analysis.
- xenon: Enforces complexity limits.
- mutmut_check.py: Executes mutation tests and ensures a minimum detection threshold of 80%.
To manually trigger all hooks:
pre-commit run --all-filesgraphql
CopiarEditar
ruff............................................................ Passed
pytest.......................................................... Passed
coverage_check.................................................. Passed
codespell....................................................... Passed
bandit security check........................................... Passed
xenon complexity check.......................................... Passed
mutation testing (mutmut)....................................... Passed
- Logs/mutmut_survivors.md: Contains surviving mutations, indicating potential weaknesses in the test suite.
- htmlcov/: Interactive visual report of code coverage generated by
pytest-cov.
All tools and dependencies are centrally configured in the pyproject.toml file, making maintenance and readability much easier.
Developed by Ismael Sanromán 🧑💻