Software Development

Oversharing on Set Up

Consider the following fictitious test fixture code:

beforeEach(() => {
   databaseConnection = openDatabase();
   inputFile = loadBigFile();
   userList = loadUserList();
   imageData = loadImageBytes();
});

If I see something like that, run before EACH test case, I have a visceral reaction. Surely we can’t need to go through this effort for each and every test?

There are a few possible root causes for the above:

  • The test fixture is performing too diverse a series of tests and needs to be broken down into multiple fixtures
  • There are some resources that are needed, but could be loaded once only for the fixture, rather than for each test
  • There are some resources that should only be loaded for the tests that use them, not the others
  • No.. this is exactly what each test needs

The last case is usually rare.

The obverse, of doing the setup in each and every test – The First and Last Rites – can be just as messy.

The focus, therefore, should be on understanding the degrees of test resource and how best to set up resources of each stratum.

Where possible, divide tests into fixtures that don’t need too large a diversity of setups. Similarly, prefer framework code to boilerplate for setting up and tidyup up test resources.

Published on Java Code Geeks with permission by Ashley Frieze, partner at our JCG program. See the original article here: Oversharing on Set Up

Opinions expressed by Java Code Geeks contributors are their own.

Ashley Frieze

Software developer, stand-up comedian, musician, writer, jolly big cheer-monkey, skeptical thinker, Doctor Who fan, lover of fine sounds
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Herbert
3 years ago

Ah, i see. Well tht’as not too tricky at all!

Back to top button