Recently I wrote a suite of unit tests that relied on a large set of test data. The set contained twelve elements and while this does not sound like a lot it was when used with the tests.
Each element required several properties to be set with unique vales. The issue was using this method was that the factory method that created this set of data was huge.
What are the best practices regards this issue? My application actually reads data in via a file but for tests I used mock data from an in memory store.
Any advice?
What do your tests look like?
Are you sure that you are writing unit tests and not higher level tests of multiple components of your code? A pure unit test should only be calling a single method, and that method will hopefully have limited calls to other methods (possibly via mocking).
By focusing on the smallest unit possible, you can write code to test specific edge cases. Whereas, if you are testing at a higher level, you will often have to write all types of permutations of edge-cases. Once you have all the smallests units covered, you can write some higher level integration tests to make sure that all those units are assembled correctly.
For example, if I had an application that reads in a CSV file of stock quotes and averages all the quotes for a given day, I would write several tests:
I apologize if I am making assumptions about your unit tests, but from my experience, I find that often what people call unit tests are not real unit tests and rather integration tests (or whatever you prefer to call them, e.g. functional tests, etc.). I am personally very guilty of writing tests that were too broad, and every time I now write tests I have to force myself to remember to really test a unit at a time.
How many test scenarios does this test data set support?
Ideally, your test data should be broken up so that there are separate test data sets for each scenario. Otherwise your test scenarios are indirectly dependent on each other, which is evil anyway.
In other words, having multiple scenarios share the same data set creates the possibility where modifying the shared data set for one scenario inadvertently makes the data incompatible with another scenario.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With