pactpact-python

How to pass arguments from in pact state


We are using pact to create some contract tests. We are quite new to this so we do not really know how state works. Right now we only know that the phare we use in the given comes in the state property of the state setup body call.

Given this consumer test:

headers = {'Authorization': 'Token f921c9568ab910264187332ed17f8c5e3aaa0edf'}
(pact
 .given('User exists and has resources')
 .upon_receiving('Request for user resources')
 .with_request('GET', f'/api/users/{user_id}/resources', headers=headers)
 .will_respond_with(200, body=expected))

And the verifier code on the provider:

class ProviderTestCase(TestCase):
    def test_service_provider_against_broker(self):
        verifier = Verifier(provider="Provider", provider_base_url=PROVIDER_URL)

        success, logs = verifier.verify_with_broker(
            **BROKER_OPTS,
            verbose=True,
            provider_states_setup_url='https://webhook.site/82751898-e89d-4531-a86c-8bf5e1e35f88',
            enable_pending=False,
        )

        self.assertEqual(success, 0)

Then I get the following body on the POST request on the webhook before running that test:

{
  "consumer": "Consumer",
  "state": "User exists and has resources",
  "states": [
    "User exists and has resources"
  ],
  "params": {}
}

And there I see the keyword params. But I do not really know how to populate that. I also do not know why states is an array. The documentation for this module is quite uncompleted I think (or at least I have not found a good one).

Do you know how to send arguments from the consumer? Like the auth token, or some arbitrary variables to set the database before de test is run.


Solution

  • Pact Python doesn't currently support Pact Specification V3, which has support for multiple states with parameters.

    If your consumer is in a language that does support V3, you could use the CLI verifier which supports this.