Pact testing nullable values

140 Views Asked by At

I want to test a nullable field with pact, so I have one test where

def test_post_1(pact, client):
   (
      pact.given("object doesnt exist")
      .upon_receiving("a new post request with data")
      .with_request(
          "post", "/api/post/new_data",
          headers={"Content-Type": "application/json"},
          body{"key": Like("some data")},
      )
      .will_respond_with(200, body={})
   )
   with pact:
      response = client.post_data(
         data={"key": "some data"}
      )
   pact.verify()

And a second test with

def test_post_2(pact, client):
   (
      pact.given("object doesnt exist")
      .upon_receiving("a new post request")
      .with_request(
          "post", "/api/post/new_data",
          headers={"Content-Type": "application/json"},
          body{"key": None},
      )
      .will_respond_with(200, body={})
   )
   with pact:
      response = client.post_data(
         data={"key": None}
      )
   pact.verify()

When I run each test independently they pass. When run together the second test fails with the error that a string was expected but it received null.

Why is the pact state not updating between tests? The pact here is a fixture shared between both tests and defined in a conftest.py file. I've set the fixture scope from "session" to "function" to try and force it to be regenerated, but this doesn't make any difference.

2

There are 2 best solutions below

0
On

The unique identifiers of a pact test are the description and any provider states. If they don't change between tests, you will get this conflict.

If you could share your test setup that would help us point you in the right direction.

0
On

So this was actually not to do with Pact, but in the setup.cfg for the package pytest was set to use -n auto, so tests were being executed in parallel. Therefore this was playing havoc with the Pact broker mock server, as multiple instances were being spun up at the same time.

Setting pytest to run with -n0 for the contract tests solved the issue I was having.