hassle.run_tests

 1import argparse
 2import os
 3
 4import coverage
 5import pytest
 6from pathier import Pathier
 7
 8
 9def get_args() -> argparse.Namespace:
10    parser = argparse.ArgumentParser()
11
12    parser.add_argument(
13        "package_name",
14        type=str,
15        default=".",
16        nargs="?",
17        help=""" The name of the package or project to run tests for,
18        assuming it's a subfolder of your current working directory.
19        Can also be a full path to the package. If nothing is given,
20        the current working directory will be used.""",
21    )
22
23    args = parser.parse_args()
24
25    return args
26
27
28def run_tests(package_path: Pathier) -> bool:
29    """Run tests with coverage and pytest.
30
31    Returns True if all tests passed."""
32    startdir = Pathier().cwd()
33    package_path.mkcwd()
34    cover = coverage.Coverage()
35    cover.start()
36    results = pytest.main(["-s"])
37    cover.stop()
38    cover.report()
39    startdir.mkcwd()
40    return results == 0
41
42
43def main(args: argparse.Namespace = None):
44    if not args:
45        args = get_args()
46    package_path = Pathier(args.package_name).resolve()
47    run_tests(package_path)
48
49
50if __name__ == "__main__":
51    main(get_args())
def get_args() -> argparse.Namespace:
10def get_args() -> argparse.Namespace:
11    parser = argparse.ArgumentParser()
12
13    parser.add_argument(
14        "package_name",
15        type=str,
16        default=".",
17        nargs="?",
18        help=""" The name of the package or project to run tests for,
19        assuming it's a subfolder of your current working directory.
20        Can also be a full path to the package. If nothing is given,
21        the current working directory will be used.""",
22    )
23
24    args = parser.parse_args()
25
26    return args
def run_tests(package_path: pathier.pathier.Pathier) -> bool:
29def run_tests(package_path: Pathier) -> bool:
30    """Run tests with coverage and pytest.
31
32    Returns True if all tests passed."""
33    startdir = Pathier().cwd()
34    package_path.mkcwd()
35    cover = coverage.Coverage()
36    cover.start()
37    results = pytest.main(["-s"])
38    cover.stop()
39    cover.report()
40    startdir.mkcwd()
41    return results == 0

Run tests with coverage and pytest.

Returns True if all tests passed.

def main(args: argparse.Namespace = None):
44def main(args: argparse.Namespace = None):
45    if not args:
46        args = get_args()
47    package_path = Pathier(args.package_name).resolve()
48    run_tests(package_path)