Dov Benyomin Sohacheski
Dov Benyomin Sohacheski

Reputation: 7752

Pytest hooks are causing tests to be run multiple times

I successfully implemented a pytest hook, however, now all tests are being run multiple times.

In my root folder I added the following code to conftest.py in order to activate the hooks:

def pytest_report_teststatus(report):
    if report.passed:
        letter = "."
        longrep = ' \u2714 '
    elif report.skipped:
        letter = "s"
        longrep = ' \u27A5 '
    elif report.failed:
        letter = "F"
        longrep = ' \u2717 '
        if report.when != "call":
            letter = "f"
    return report.outcome, letter, report.outcome.upper() + longrep

When I remove the hook, each test runs once.

How can I cause the tests to run once while using the hook?

Upvotes: 3

Views: 941

Answers (1)

Dov Benyomin Sohacheski
Dov Benyomin Sohacheski

Reputation: 7752

It seems that the pytest_report_teststatus hook is called multiple times during the testing process, (i.e. teardown, call, setup).

I added a conditional statement to the beginning of the block to detect when the hook was called and only returned values once.

def pytest_report_teststatus(report):
    if report.when == 'call': # <-- Added this line
        if report.passed:
            letter = '.'
            longrep = ' \u2714 '
        elif report.skipped:
            letter = 's'
            longrep = ' \u27A5 '
        elif report.failed:
            letter = 'F'
            longrep = ' \u2717 '
        return report.outcome, letter, report.outcome.upper() + longrep

Adding if report.when == 'call' solved the issue.

Upvotes: 3

Related Questions