Shared base fixture

Q: How do you know you can trust your test set up?
A: You base it on the set up used in another test, where the behaviour is known.

Example code

def test_simple():
  req = set_up_working_request()
  resp = invoke(req)
  assert resp.status == OK

def test_invalid_page_size():
  req = set_up_working_request()
  req.page_size = -5
  resp = invoke(req)
  assert resp.status == INVALID_ARGUMENT

In the test,

Because the only difference in set up between test_simple and test_invalid_page_size was in the differing page size, we can easily reason that the different response status is resulting from just that.

This works well together with these techniques:


  1. This is called the “Four-Phase test” in the xUnit test pattern book. (Meszaros07, p358)

    [return]
🗨 Comments for this article on HN