File size: 985 Bytes
8c9ba62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import datetime

import pytest


# Get the result of each test
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    setattr(item, "rep_" + rep.when, rep)


# Real-time print of start and end of test
@pytest.fixture(autouse=True)
def log_test_lifecycle(request):
    node_id = request.node.nodeid
    start_time = datetime.datetime.now().strftime("%H:%M:%S")

    print(f"\n[START] {start_time} - Running: {node_id}")

    yield

    end_time = datetime.datetime.now().strftime("%H:%M:%S")
    # Get the result of each test (setup, call, teardown)
    report = getattr(request.node, "rep_call", None)

    if report:
        if report.passed:
            status = "PASSED"
        elif report.failed:
            status = "FAILED"
        else:
            status = report.outcome.upper()
    else:
        status = "UNKNOWN"

    print(f"\n[END] {end_time} - Result: {status} - {node_id}")