Repository navigation
Conversation
Register a pytest plugin through the pytest11 entry point with one autouse fixture, pact_test_run_id, which sets the node ID as the Pact test run ID for the test's duration. Log records from the library and from plugins then name the test which caused them without any conftest set-up. An autouse fixture rather than a documented one, because opting in would require every test suite to remember the set-up; disabling it is a single `-p no:pact` instead. Signed-off-by: JP-Ellis <josh@jpellis.me> Assisted-by: Claude Code:claude-opus-5-5
unittest has no plugin entry point and no log capture on failure, so PactTestMixin wraps run() to set the test ID as the Pact test run ID, buffer records on the pact logger, and print them to stderr when the test fails or errors. A logger hands a record to its handlers only when the record clears the logger's effective level, and outside pytest that level is the root logger's WARNING. The mixin therefore lowers the pact logger to the forwarding level while the test runs, when the logger has no explicit level of its own, and restores it afterwards. Signed-off-by: JP-Ellis <josh@jpellis.me> Assisted-by: Claude Code:claude-opus-5
Add the pytest and unittest integrations to the logging page, under
correlating logs with tests, and point the pact.logging module
docstring at them.
Fix cross-reference syntax in src/pact/testing/{__init__,pytest,
unittest}.py: `mkdocs build --strict` reported them unresolved.
mkdocstrings' scoped_crossrefs prepends the package prefix again to a
`pact.`-prefixed target from a module below the package root, so
cross-module references use the path relative to the `pact` package.
Signed-off-by: JP-Ellis <josh@jpellis.me>
Assisted-by: Claude Code:claude-opus-5-5
JP-Ellis
added this pull request to stack #1733
October 16, 2026 00:37
2 of 3 tasks
2 of 3 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 Summary
Attributes Pact log records to the test which caused them, with no set-up in user code.
pytest11entry point, sets each test's node ID as the Pact test run ID for the duration of the test.-p no:pactdisables it.pact.testing.unittestprovides a mixin which does the same forunittesttest cases. It also writes the test's Pact records to stderr when the test fails or errors.Stacked on #1730.
🔥 Motivation
The forwarded records carry
test_run_id, but only when something sets it. An autouse fixture covers every pytest suite by default. An opt-in fixture would need every project to remember the set-up.🔨 Test Plan
pytesterruns a small suite and checks the run ID is set and cleared per test, and that-p no:pactdisables the plugin.unittest.TestResult. Its tests check that output appears only on failure and error, and that the handler is removed afterwards.🔗 Related issues/PRs