Default data loading templates for unit testing

Look for some strategies for how you guys load default data when doing unit tests.

+4
source share
5 answers

I am using a builder that contains default values, also: http://elegantcode.com/2008/04/26/test-data-builders-refined/ . Then in the test only the value that he needs is indicated:

Customer customer = new CustomerBuilder() .WithFirstName("this test only cares about a special ' ... first name value"); 

After reading the other answers, I want to clear it of database data. It should create instances / data that you pass to the classes that you test on the module.

This is a matter of convenience / maintenance of simple tests, many times you test a very specific behavior, which depends on 1-3 fields, and you do not care what the rest of the fields.

+1
source

For unit testing, I usually do not upload data in advance - each test is designed to work with a data source that may or may not contain existing records, so each test records all the records necessary to complete the test.

When choosing values ​​to send to the database, I use the GUID (or other random values) when possible, as this ensures that the values ​​in the database are unique (for example, if you create someone named "Mr XY", it’s useful to know that the search "X" should return only 1 result and that there is no chance that you accidentally encountered someone else in the database whose last name would be Y)

Often when testing modules, I test methods that modify data along with methods that read data, and therefore my unit tests use the same API (the one that is being tested) to write to the database. (Well, if each unit test covers a certain area of ​​functionality, but this is not absolutely necessary)

If the API under test does not have methods for writing to the database, I write my own set of auxiliary functions - the exact structure will depend on the data source, but as an example I often use LINQ to SQL.

+1
source

The preferred strategy is transaction data. Spring offers extensive support (for JUnit 3 and 4). With this strategy, your test starts a new transaction every time, and your data is rolled back at the end of the test.

Of course, sometimes this is not enough: either the data set is too extensive, or divided between tests, or several transactions are part of the testing area. In this case, I recommend creating a common test data tray that was created before the test suite was run. There are frameworks for this (dbUnit), but you can also do without them if you are careful and consistent.

UPD: creating data within a transaction does not mean that you do not need test data, you will probably end up creating reusable and general helper classes to maintain test data in all cases.

+1
source

TDD is testing a piece of code in isolation . Create an instance of the class with its dependencies (or their bullying), call the test method and confirm it to check the test result.

Typically, a simple test without data begins with TDD. When data is needed, it is created in the test fixture (the sandbox where the test is run) using the setUp() test method, and then destroyed by the tearDown() method after the test runs. Data is not loaded from the database.

+1
source

I usually use methods like GetCustomer (), which return a common client. If I need to make the returned client package my needs for a specific test, I will simply change its property after it returns.

In other cases, I can pass some configuration information to my GetCustomer () method. For example, GetCustomer (customerType string).

I read the opinions of experts, which say that each test should contain its own unique data to work and not try to make the data generalized. Despite the fact that this can make each test β€œlarger” in size, in comparison with it it will become more clear, because the setting depends on each test and the goals of each test. I like this advice because I have come across many cases when I try to make general settings data, made things very messy very quickly.

0
source

All Articles