Skip to content

ismaelsanroman/unitTests-quality-plan

Repository files navigation

🧪 UnitTest_demo

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.


📂 Project Structure

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

🔍 What does each tool do?

  • ruff: A fast linter and formatter that combines features from flake8, black, and isort, ensuring high code quality and style compliance.
  • pytest: A framework for running unit, integration, and functional tests in Python.
  • pytest-cov: A pytest plugin 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.

🚀 Step-by-Step Initial Setup

1. Prepare the virtual environment

python3 -m venv .venv
source .venv/bin/activate

2. Install dependencies

pip install -r requirements.txt

3. Make the custom script executable

chmod +x scripts/mutmut_check.py

4. Install pre-commit and hooks

pre-commit install

🛠 Manual Tool Usage

Run tests with pytest

pytest

Generate coverage report

pytest --cov=src --cov-report=term-missing --cov-report=html

Run Mutation Testing (mutmut)

mutmut run
mutmut results > Logs/mutmut_survivors.md

Validate mutations with the custom script

python scripts/mutmut_check.py

Security analysis with Bandit

bandit -c bandit.yaml -r src/

Spell check with codespell

codespell src/

Cyclomatic complexity check with Xenon

xenon --max-absolute B --max-modules B --max-average A src/

🛠 Automation with pre-commit

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-files

✅ Expected pre-commit Output

graphql
CopiarEditar
ruff............................................................ Passed
pytest.......................................................... Passed
coverage_check.................................................. Passed
codespell....................................................... Passed
bandit security check........................................... Passed
xenon complexity check.......................................... Passed
mutation testing (mutmut)....................................... Passed

📜 Generated Reports and Logs

  • 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.

⚙️ Centralized Configuration (pyproject.toml)

All tools and dependencies are centrally configured in the pyproject.toml file, making maintenance and readability much easier.


✨ Author

Developed by Ismael Sanromán 🧑‍💻

About

A modern testing and quality assurance toolkit for Python projects. Includes pre-commit checks, mutation testing, complexity analysis, and security scanning.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors