Ray Suelzer
Ray Suelzer

Reputation: 4107

Unit Testing Suggestions

I have a moderately to highly complicated application that is backed by a large database of about 50-60 tables. I'm trying to get as much unit test coverage as possible on the code, but I am really struggling with mocking a dataset and some key concepts. I actually have anxiety at the thought of trying to implement full unit test coverage because I'm not sure how to do the following:

1)Test every possible scenario and data combination for each function. I.E. One of our functions returns a value based upon about 20 inputs with 3 different possible values for each input. How could one possibly test all of the values for something like that? And if I could test all those combinations, I would have to write the exact same logical code in the test to determine if it should have passed or not (isn't this redundant?).

2) Data and results change with time! For example, if I run a query for the number of employees who rented a car in the last week, I will always be returning a different result as time moves forward. How can I write a unit test that knows how many results to expect if the results will vary from one day to the next?

People say that if you are struggling with unit testing that you are doing it wrong, so please enlighten me on how to best handle these situations.

Upvotes: 1

Views: 193

Answers (1)

George Mauer
George Mauer

Reputation: 122042

My comment above notwithstanding, here are some thoughts:

  • In unit tests you test "units". A large part of the finesse in unit testing is determining what exactly a "unit" is. So what are your units? Hint: They almost never include involving the database.
  • Determining proper units is what prevents the problem of results and data changing. If a core bit of functionality changes then sure, a unit test needs to be modified or even deleted, but otherwise it should be minimal maintenance.
  • Combinations and permutations - Test things that you actually need to test. You can write some helper code for this. However, you don't always need to test absolutely everything, test to the point that is useful and no further.
  • I find high levels of testing useful, you however might not. Your experience and abilities at unit testing factor into how useful tests might be.

Upvotes: 1

Related Questions