Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add mutation testing with Cosmic Ray #636

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/python-mutation-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Python mutation tests

on:
workflow_dispatch:
schedule:
- cron: '30 3 * * *'
# Todo; remove the push, they take to long
push:
branches:
- add-mutation-testing-with-cosmic-ray

jobs:
tests:
name: Python mutation tests
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
python-version: ["3.12"]

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Display Python version
run: python --version

- name: Install dependencies
run: |
pip install poetry
poetry install

- name: Run mutation tests
run: |
# Todo: Can we make it faster in GitHub Actions
# Todo: Drop the --force, it just here for easy local copy and paste
poetry run cosmic-ray init setup.cfg cosmic-ray.sqlite --force
echo "Initialised"
poetry run cosmic-ray --verbosity INFO exec setup.cfg cosmic-ray.sqlite

- name: Report results
# Todo: Only show failures https://github.com/sixty-north/cosmic-ray/issues/561
run: poetry run cr-report cosmic-ray.sqlite --show-diff
env:
PYTHON_VERSION: ${{ matrix.python-version }}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Unit tests
name: Python unit tests

on: [push]

Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ __py_cache__/

.pytest_cache/

# Mutation testing
cosmic-ray.sqlite
cosmic-ray-worker*
mutants

staticfiles/
node_modules/

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,17 @@ To allow pdb to work correctly, disable multiple processes using the `--numproce

`poetry run pytest --numprocesses 0`

##### Monitoring the quality of our Python unit tests

We use [Codecov](https://app.codecov.io/github/uktrade/platform-tools) to monitor the comprehensiveness and performance of our unit tests.

We use [Cosmic Ray](https://cosmic-ray.readthedocs.io/en/latest/index.html) to run [Python mutation tests in GitHub Actions](https://github.com/uktrade/platform-tools/actions/workflows/python-mutation-tests.yml) on a schedule to help identify areas of our code which are not covered by our tests.

You can run the mutation tests locally, but:

- they take a very long time to run as they run the whole unit test suite for each mutation.
- they actually your source files while they run, so you cannot work on the code until it's finished.

#### Manual testing

You may want to test any CLI changes locally.
Expand Down
2,405 changes: 1,030 additions & 1,375 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ requests = "^2.31.0"
prettytable = "^3.9.0"
semver = "^3.0.2"
tomlkit = "^0.12.2"
checkov = "^3.1.67"
slack-sdk = "^3.27.1"
jsonschema = "~=4.17.0"
cfn-lint = "^1.4.2"
Expand All @@ -53,6 +52,8 @@ freezegun = "^1.2.2"
parameterized = "^0.9.0"
pytest-xdist = "^3.5.0"
pytest-cov = "^6.0.0"
cosmic-ray = "^8.4.1"
mutmut = "^3.2.1"

[build-system]
requires = ["poetry-core"]
Expand Down
22 changes: 22 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[cosmic-ray]
module-path = "utils"
timeout = 10.0
excluded-modules = ["**/node_modules/**/*"]
test-command = "poetry run pytest tests/utils"

[cosmic-ray.distributor]
name = "local"
# name = "http"

# [cosmic-ray.distributor.http]
# worker-urls = [
# "http://localhost:9876",
# "http://localhost:9877",
# "http://localhost:9878",
# "http://localhost:9879"
# ]

[mutmut]
paths_to_mutate = "utils/,dbt_platform_helper"
runner = "poetry run pytest tests/utils"
tests_dir = "tests/"
1 change: 1 addition & 0 deletions tests/platform_helper/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self):
@pytest.fixture
def fakefs(fs):
"""Mock file system fixture with the templates and schemas dirs retained."""
print(f"BASE_DIR: {BASE_DIR}")
fs.add_real_directory(BASE_DIR / "dbt_platform_helper/templates", lazy_read=True)
fs.add_real_directory(FIXTURES_DIR, lazy_read=True)
fs.add_real_file(BASE_DIR / "dbt_platform_helper/addon-plans.yml")
Expand Down
Loading