Where is the best place to create test data in TDD?

I am using NUnit integration tests. I am trying to verify that the user cannot create an account with existing email. ( Test@example.com )

I need to have test data in the database (account with email address test@example.com ).

I can create this account in a test function or in an sql script (and run it before the integration tests).

Where is the best place to create this test data?

+4
source share
3 answers

None of the options are wrong, but there are many ways to expand and strengthen your strategy:

None of these solutions are mutually exclusive. I would recommend the last element especially (the plug-in provider), and then the choice between deceptive or artificial, but quality db test data.

+5
source

It’s best to look at Dependency Injection and Bullying. In this way, you can exchange data providers with the help of ridiculed data providers and use the data that suits your needs for a particular test.

If you use NHibernate or the like, you can always recreate your db scheme before each fixture.

+2
source

In such a situation, as you describe, I would prefer to create an account in a test function.

A unit test should be as autonomous as possible. In addition, it helps to understand what you are testing, if you can see all the data needed for the test in one place.

Here is a complete example that should illustrate:

[Test] public void Test_CannotCreateDuplicateEmail() { // Arrange CreateAccount(" test@example.com "); // OK // Act try { CreateAccount(" test@example.com "); // If control arrives here, then the test has failed. Assert.Fail(); } // Assert catch(AccountException ex) { // Assert that the correct exception has been thrown. Assert.AreEqual("Failed", ex.Message); } } 
+1
source

All Articles