Skip to content

Commit 9b6f067

Browse files
zulinx86roypat
authored andcommitted
test: Always report test metrics
Previously it reported CW metrics only when tests fail in 'call' phase. However, we do some custom cleanups in 'teardown' phase and they may fail. To reflect such failures on CW metrics, reports metrics for any phase ('setup', 'call' and 'teardown') and adds per-phase dimension. Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent 641b375 commit 9b6f067

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

tests/conftest.py

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -116,36 +116,52 @@ def record_props(request, record_property):
116116

117117
def pytest_runtest_logreport(report):
118118
"""Send general test metrics to CloudWatch"""
119-
if report.when == "call":
120-
METRICS.set_dimensions(
121-
{
122-
"test": report.nodeid,
123-
"instance": global_props.instance,
124-
"cpu_model": global_props.cpu_model,
125-
"host_kernel": "linux-" + global_props.host_linux_version,
126-
},
127-
# per host kernel
128-
{"host_kernel": "linux-" + global_props.host_linux_version},
129-
# per CPU
130-
{"cpu_model": global_props.cpu_model},
131-
# and global
132-
{},
133-
)
134-
METRICS.set_property("result", report.outcome)
135-
METRICS.set_property("location", report.location)
136-
for prop_name, prop_val in report.user_properties:
137-
METRICS.set_property(prop_name, prop_val)
138-
METRICS.put_metric(
139-
"duration",
140-
report.duration,
141-
unit="Seconds",
142-
)
143-
METRICS.put_metric(
144-
"failed",
145-
1 if report.outcome == "failed" else 0,
146-
unit="Count",
147-
)
148-
METRICS.flush()
119+
120+
# The pytest's test protocol has three phases for each test item: setup,
121+
# call and teardown. At the end of each phase, pytest_runtest_logreport()
122+
# is called.
123+
# https://github.com/pytest-dev/pytest/blob/d489247505a953885a156e61d4473497cbc167ea/src/_pytest/hookspec.py#L643
124+
# https://github.com/pytest-dev/pytest/blob/d489247505a953885a156e61d4473497cbc167ea/src/_pytest/hookspec.py#L800
125+
METRICS.set_dimensions(
126+
# fine-grained
127+
{
128+
"test": report.nodeid,
129+
"instance": global_props.instance,
130+
"cpu_model": global_props.cpu_model,
131+
"host_kernel": "linux-" + global_props.host_linux_version,
132+
"phase": report.when,
133+
},
134+
# per test
135+
{
136+
"test": report.nodeid,
137+
"instance": global_props.instance,
138+
"cpu_model": global_props.cpu_model,
139+
"host_kernel": "linux-" + global_props.host_linux_version,
140+
},
141+
# per phase
142+
{"phase": report.when},
143+
# per host kernel
144+
{"host_kernel": "linux-" + global_props.host_linux_version},
145+
# per CPU
146+
{"cpu_model": global_props.cpu_model},
147+
# and global
148+
{},
149+
)
150+
METRICS.set_property("result", report.outcome)
151+
METRICS.set_property("location", report.location)
152+
for prop_name, prop_val in report.user_properties:
153+
METRICS.set_property(prop_name, prop_val)
154+
METRICS.put_metric(
155+
"duration",
156+
report.duration,
157+
unit="Seconds",
158+
)
159+
METRICS.put_metric(
160+
"failed",
161+
1 if report.outcome == "failed" else 0,
162+
unit="Count",
163+
)
164+
METRICS.flush()
149165

150166

151167
@pytest.fixture()

0 commit comments

Comments
 (0)