Coverage for pycommons / dev / building / run_tests.py: 94%
36 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-11 03:04 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-11 03:04 +0000
1"""Run the pytest tests."""
3from argparse import ArgumentParser
4from itertools import chain
5from typing import Final
7from pycommons.dev.building.build_info import (
8 BuildInfo,
9 parse_project_arguments,
10)
11from pycommons.io.arguments import pycommons_argparser
12from pycommons.io.console import logger
13from pycommons.types import type_error
16def run_tests(info: BuildInfo) -> None:
17 """
18 Perform the unit testing of the project.
20 This code cannot really be unit tested, as it would run the itself
21 recursively.
23 :param info: the build information
25 >>> try:
26 ... run_tests(None)
27 ... except TypeError as te:
28 ... print(str(te)[:50])
29 info should be an instance of pycommons.dev.buildi
31 >>> try:
32 ... run_tests(1)
33 ... except TypeError as te:
34 ... print(str(te)[:50])
35 info should be an instance of pycommons.dev.buildi
36 """
37 if not isinstance(info, BuildInfo):
38 raise type_error(info, "info", BuildInfo)
40 logger(
41 f"Performing unit tests for {info}. First erasing old coverage data.")
43 info.command(("coverage", "erase")).execute()
45 logger("Now running doctests.")
46 ignores: Final[list] = []
47 if info.doc_dest_dir is not None:
48 ignores.append(f"--ignore={info.doc_dest_dir}")
49 if info.doc_source_dir is not None:
50 ignores.append(f"--ignore={info.doc_source_dir}")
51 if info.dist_dir is not None:
52 ignores.append(f"--ignore={info.dist_dir}")
53 if info.tests_dir is not None:
54 ignores.append(f"--ignore={info.tests_dir}")
56 timeout: Final[str] = f"--timeout={max(10, int(0.95 * info.timeout) - 1)}"
57 info.command(chain((
58 "coverage", "run", "-a", f"--include={info.package_name}/*",
59 "-m", "pytest", timeout, "--strict-config",
60 "--doctest-modules"), ignores)).execute()
62 if info.tests_dir is None:
63 logger("No unit tests found.")
64 else:
65 logger("Now running unit tests.")
66 if info.examples_dir is not None:
67 del ignores[-1]
68 if info.examples_dir is not None:
69 ignores.append(f"--ignore={info.examples_dir}")
70 info.command(chain((
71 "coverage", "run", "-a", f"--include={info.package_name}/*",
72 "-m", "pytest", timeout, "--strict-config",
73 info.tests_dir), ignores)).execute()
75 logger(f"Finished doing unit tests for {info}.")
78# Run conversion if executed as script
79if __name__ == "__main__":
80 parser: Final[ArgumentParser] = pycommons_argparser(
81 __file__,
82 "Run the unit tests for a Python Project.",
83 "This utility runs the unit tests and computes the coverage data "
84 "in a unified way.")
85 run_tests(parse_project_arguments(parser))