Esempio n. 1
0
def pytest_terminal_summary(terminalreporter: TerminalReporter,
                            exitstatus: int, config: Config) -> None:
    """Show factoryboy random state when there are test failures or errors.

    Factoryboy uses randomness in order to generate its values which is great for
    fuzzing but makes it really hard to reproduce tests which fail due to a fuzzed
    value. This hook outputs the random state used by factory-boy (and faker) when
    there are test failures (or errors).

    The outputted state is an ascii, base64 encoded pickle dump.

    Args:
        terminalreporter (TerminalReporter): Add output to the pytest output.
        exitstatus (int): The exit status of pytest (unused)
        config (Config): The pytest config (unused)

    """
    show_state = config.getoption("show_state") or (
        os.environ.get("SHOW_FACTORYBOY_STATE") == "True")
    failures: list[BaseReport] = terminalreporter.getreports("failed")
    errors: list[BaseReport] = terminalreporter.getreports("error")
    if show_state and (failures or errors):
        terminalreporter.write_sep("=", "factory-boy random state")
        encoded_state = base64.b64encode(
            pickle.dumps(factory.random.get_random_state()))
        terminalreporter.write_line(encoded_state.decode("ascii"))
Esempio n. 2
0
def retrieve_stat_count_map(reporter: TerminalReporter) -> Dict[str, int]:
    stat_count_map = {}

    for name in ["failed", "passed", "skipped", "error", "xfailed", "xpassed"]:
        count = len(reporter.getreports(name))
        stat_count_map[name] = count

    return stat_count_map