hassle.run_tests API documentation

# hassle.run_tests

View Source

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

     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

def run_tests(package_path: pathier.pathier.Pathier) -> bool: View Source

    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    # All tests passed (0) or Pytest couldn't find any tests (5)
    41    return results in [0, 5]

Run tests with coverage and pytest.

Returns True if all tests passed.

def main(args: argparse.Namespace = None): View Source

    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)