Skip to content

Latest commit

 

History

History
230 lines (156 loc) · 5.46 KB

File metadata and controls

230 lines (156 loc) · 5.46 KB

Basics of Testing and Coverage

Run All Unit Tests

python -m unittest discover -v

Options:

  • -v — Verbose output (shows each test name and result)
  • -s tests — Specify the test directory if not automatically discovered
  • -p '*_test.py' — Pattern for test filenames

Example:

python -m unittest discover -s tests -p '*_test.py' -v

Run a Specific Test Class or Method

python -m unittest tests.ClassTest
python -m unittest tests.ClassTest.test_method

(Tip: Use the full import path relative to your repository root.)


Run Tests Inside a Jupyter Notebook

import unittest
from tests import ClassTest

suite = unittest.TestLoader().loadTestsFromTestCase(ClassTest)
unittest.TextTestRunner(verbosity=2).run(suite)

Measure Code Coverage

Install the coverage library (if not already installed):

pip install coverage

Run coverage on the entire project:

coverage run -m unittest discover -v

Generate a terminal report:

coverage report -m

Or generate an HTML report:

coverage html

Open the resulting report (Linux):

xdg-open htmlcov/index.html

(On Windows, use start htmlcov/index.html; on macOS, use open htmlcov/index.html.)


Limit Coverage to a Specific Module

To focus coverage analysis on a specific file or package:

coverage run --source=module/file.py -m unittest discover
coverage report -m

Or from Python:

import warnings
from coverage import Coverage
from coverage.exceptions import CoverageWarning

# Suppress include/source overlap warnings
warnings.filterwarnings("ignore", category=CoverageWarning)

cov = Coverage(source=['caemate/calcs/modalanalysis/twinwrappers/viadottoparchi.py'])
cov.start()

# Run your tests here
unittest.TextTestRunner(verbosity=2).run(suite)

cov.stop()
cov.save()
cov.report(show_missing=True)

Combine Coverage from Multiple Runs

When tests are distributed across different environments (e.g., unit and integration layers):

coverage combine
coverage report -m

This merges all .coverage.* data files into a single combined result.