Dataset for unit testing

Hi

I am writing some unit tests for a class which I have written. Many of the methods of the class are to retrieve data held in the class in various ways. I would like to unit test these methods to ensure that they return the correct data.

Having read a little about unit testing, I think the idea is to make functions which test a specific piece of functionality of the method using the AAA (arrange act assert) pattern. There may be multiple test functions needed in respect of each method being tested. Descriptive names for each test function should be given.

What I am a little unsure of is when/where to put my test data. Should I (a) create one set of fixed data in a function and then get that data for each test function or (b) declare custom data at the beginning of each test function.

I should think that it is possible to create generic data which provides sufficient variety to allow the functionality of each method to be properly tested. This would save a considerable amount of code so I think this is preferable but would be grateful for any thoughts.

Thanks
Last edited on
I would go for approach a since it will give you more flexibility. If you change the data you need to change it only in one place.
Another benefit is that you can have different functions that create different data - like random data, invalid data.

Topic archived. No new replies allowed.