Skip to content

Commit da23f2d

Browse files
committed
python(feat): Add report_outcome(result, reason) functionality to test step context managers.
1 parent d54c304 commit da23f2d

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

python/lib/sift_client/_tests/util/test_test_results_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,19 @@ def test_measurement_update(self, report_context):
152152
assert test_step.measurements[2].boolean_value == True
153153
assert test_step.measurements[2].measurement_type == TestMeasurementType.BOOLEAN
154154

155+
def test_report_outcome(self, report_context, step):
156+
# Capture current state of report context's failures so we can keep things passed at a high level if the test's induced failures happen as expected.
157+
current_step_path = step.current_step.step_path
158+
initial_open_step_result = report_context.open_step_results.get(current_step_path, True)
159+
initial_any_failures = report_context.any_failures
160+
assert step.report_outcome("Test Pass Outcome", True, "Test Pass Description") == True
161+
assert step.report_outcome("Test Fail Outcome", False, "Test Failure Description") == False
162+
# If this test was successful, mark that at a high level.
163+
if initial_open_step_result:
164+
report_context.open_step_results[current_step_path] = True
165+
if not initial_any_failures:
166+
report_context.any_failures = False
167+
155168
def test_bad_assert(self, report_context, step):
156169
# Capture current state of report context's failures so we can keep things passed at a high level if the test's induced failures happen as expected.
157170
current_step_path = step.current_step.step_path

python/lib/sift_client/util/test_results/context_manager.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ def create_step(self, name: str, description: str | None = None) -> TestStep:
138138

139139
return step
140140

141-
def report_measurement(self, measurement: TestMeasurement, step: TestStep):
141+
def report_outcome(self, outcome: bool, step: TestStep):
142142
"""Report a failure to the report context."""
143143
# Failures will be propogated when the step exits.
144-
if not measurement.passed:
144+
if not outcome:
145145
self.open_step_results[step.step_path] = False
146146
self.any_failures = True
147147

@@ -303,10 +303,24 @@ def measure(
303303
)
304304
evaluate_measurement_bounds(create, value, bounds)
305305
measurement = self.client.test_results.create_measurement(create)
306-
self.report_context.report_measurement(measurement, self.current_step)
306+
self.report_context.report_outcome(measurement.passed, self.current_step)
307307

308308
return measurement.passed
309309

310+
def report_outcome(self, name: str, result: bool, reason: str | None = None) -> bool:
311+
"""Report an outcome from some action or measurement. Creates a substep that is pass/fail with the optional reason as the description.
312+
313+
Args:
314+
name: The name of the substep.
315+
result: True if the action or measurement passed, False otherwise.
316+
reason: [Optional] The context to include in the description of the substep.
317+
318+
returns: The given result so the function can be used in line.
319+
"""
320+
with self.substep(name=name, description=reason) as substep:
321+
self.report_context.report_outcome(result, substep.current_step)
322+
return result
323+
310324
def substep(self, name: str, description: str | None = None) -> NewStep:
311325
"""Alias to return a new step context manager from the current step. The ReportContext will manage nesting of steps."""
312326
return self.report_context.new_step(name=name, description=description)

0 commit comments

Comments
 (0)