Shared base fixture

make base case result derived case 1 derived case 2 result result expect happy case expect corner case 1 expect corner case 2 set up verify result exercise modify

A diagram of how different tests share common test setup with a shared base fixture

Q: How do you know you can trust your test set-up?
A: You reuse a well-known test set up in every test.

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↩︎

Comments