How to reuse a pact.v3.Pact in consumer testing #959
-
Hi, I am having some difficulties with implementing version 3 of pact-python. Specifically, on how the Consumer Testing can be done. In the end, I wonder why I cannot set the scope of the pytest fixture to @pytest.fixture(scope="session")
def pact() -> Generator[Pact, None, None]:
"""
Set up the Pact fixture.
This fixture configures the Pact instance for the consumer test. It defines
where the pact file will be written and the consumer and provider names.
This fixture also sets the Pact specification to `V4` (the latest version).
The use of `yield` allows this function to return the Pact instance to be
used in the test cases, and then for this function to continue running after
the test cases have completed. This is useful for writing the pact file
after the test cases have run.
Yields:
The Pact instance for the consumer tests.
"""
pact_dir = Path(Path(__file__).parent.parent.parent / "pacts")
pact = Pact("v3_http_consumer", "v3_http_provider")
yield pact.with_specification("V4")
pact.write_file(pact_dir) This returns a My use case is that I would like to attach the Pact to an object yielded by a session-scoped fixture. Your help is much appreciated. Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
For the sake of completeness, I'll copy my answer from the issue here in case anyone stumbles on this (and apologies for not answering you earlier). When the underlying server is started for the verification process, the underlying server expects all of the configured interactions to be replayed and will result in an error if any of the interactions are not replayed. For example, if I configure the Pact to have a As a result, it is not exactly possible to have a session-scoped fixture like you have here assuming that each test has the combination of:
Having said all that though, Pact by default merges interactions within the output file if it already exists. So simply changing your fixture to be function-scoped (the default) should work fine. Hopefully this helps! And apologies for net answering you earlier. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your reply. Your explanation supports my assumption that this might be a design problem. A pact file contains multiple interactions that can and, in practical terms, are probably usually generated by multiple tests. But the pact-python framework does not support that scheme that multiple tests feed interactions to the same Pact object (while they end up in the same file and Pact on the broker). I cannot really see the actual causal connection between In our test suite we reuse multiple interactions with the same description in multiple tests. But in order to avoid overwriting of interactions with the same name (description) when saving to file, we came up with additional code to handle the interaction descriptions and its properties. It effectively creates a new description name if a given set of interaction properties (request_body, request_query_params, return_code, response_body) is not defined in an interaction already used in this pact. I personally would like to see this as part of pact-python and that could be achieved by making the pact object session-scoped and allow to add new interactions and replays after a server was shut down. |
Beta Was this translation helpful? Give feedback.
The description of the interaction is an essential aspect of Pact. It's key to the BDD and "specification by example" elements of the test. Pact is not a general purpose API testing automation solution, it's a targeted contract testing solution - the descriptions (along with the states or "pre-conditions") are the basis for explaining the scenario being tested.
That is to say that the description is for human comprehe…