Skip to content

fix: measure tavern stage timings via beta request/response hooks#493

Merged
gibiw merged 3 commits into
mainfrom
fix/tavern-step-timings
May 25, 2026
Merged

fix: measure tavern stage timings via beta request/response hooks#493
gibiw merged 3 commits into
mainfrom
fix/tavern-step-timings

Conversation

@gibiw

@gibiw gibiw commented May 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • _set_steps created all Step() objects up-front in start_pytest_item with StepExecution() auto-stamping start_time. Every step then went through one pytest_runtest_makereport loop calling execution.complete(). Result: every stage of a multi-stage Tavern test shared the same start_time, the same end_time, and a duration equal to the full test length — the TestOps timeline could not show stage ordering or per-stage runtime.
  • Hook into Tavern's pluggy hooks:
    • pytest_tavern_beta_before_every_request stamps start_time on the step matching the current stage index.
    • pytest_tavern_beta_after_every_response calls execution.complete() on that step and advances the cursor.
  • Replace unconditional execution.complete() calls in pytest_runtest_makereport with _ensure_step_closed() — respects timings already set by the hooks; falls back to a zero-duration placeholder for steps that never ran (skip-after-failure, no-hook).
  • Bumps qase-tavern to 3.1.1.

Test plan

  • Added TestTavernBetaHooksTiming covering distinct timings across 3 stages, single-pair advance of the cursor, extra-hook-after-last-stage guard, and _ensure_step_closed behaviour for already-closed and skipped steps.
  • pytest qase-tavern/tests/test_plugin.py -v → 25 passed locally (Python 3.12).
  • Added examples/single/tavern/test_multistage.tavern.yaml (3-stage tests with delay_before so the gap between stages is visible).
  • End-to-end verified against TestOps project DEVX:
    • Before fix (run #880): all 3 stages of Multistage GET sequence share start_time≈…570…, end_time≈…692…, duration=1122 ms each.
    • After fix (run #881): same test shows Stage 1=735 ms, Stage 2=307 ms, Stage 3=461 ms with monotonically increasing start times; the second test's delay_before gaps are visible (~300 ms between stage 1 and stage 2).
  • CI green on all supported Python versions.

gibiw added 3 commits May 25, 2026 19:48
Steps were created up-front in start_pytest_item with StepExecution()
auto-stamping start_time, and then all closed in a single
pytest_runtest_makereport loop, so every stage of a multi-stage
Tavern test ended up with identical start_time, end_time and a
duration equal to the full test length. The TestOps timeline could
not show stage ordering or per-stage runtime.

Hook into Tavern's pluggy hooks:
- pytest_tavern_beta_before_every_request stamps start_time on the
  step that matches the current stage index.
- pytest_tavern_beta_after_every_response calls execution.complete()
  on that step and advances the cursor.

Replace the unconditional execution.complete() calls in
pytest_runtest_makereport with _ensure_step_closed(), which respects
timings already set by the hooks and falls back to a zero-duration
placeholder for steps that never ran (skip-after-failure, etc.).

Add a test_multistage.tavern.yaml example demonstrating sequential
stages with delay_before between them, and unit tests covering hook
ordering, the extra-hook-after-last-stage guard, and _ensure_step_closed.
Adding pytest_tavern_beta_before_every_request and
pytest_tavern_beta_after_every_response as plain methods on
QasePytestPlugin makes pluggy refuse to load the plugin when the
Tavern package isn't installed, because the matching hookspecs
are only registered when tavern itself is imported. The tox
testing environment installs only qase-tavern + pytest, so CI
fails with:

    PluginValidationError: unknown hook
    'pytest_tavern_beta_after_every_response' in plugin
    <qase.tavern.plugin.QasePytestPlugin object at ...>

Decorate both hooks with @pytest.hookimpl(optionalhook=True) so
pluggy skips the unknown-hook check; the hooks still fire when
Tavern is present at runtime.
@gibiw gibiw merged commit 77c1a6e into main May 25, 2026
37 checks passed
@gibiw gibiw deleted the fix/tavern-step-timings branch May 25, 2026 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant