Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I use NUnit integration tests. I am trying to test to make sure that user can't create account with existing email. ([email protected])

I need to have test data in the database (account with [email protected] email).

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

Where is the better place to create this test data?

like image 781
Anatoliy Avatar asked Feb 26 '23 19:02

Anatoliy


2 Answers

Neither option is wrong but there are a number of ways to extend and solidify your strategy:

  • Mocking which goes hand-in-hand with TDD
  • Generation of db test data with tools like RedGate's Sql Data Gen
  • Creation of pluggable data providers (IoC/DI) where you can swap between test and production

None of these solutions are mutually exclusive. I would recommend the last item especially (pluggable provider) and then a choice between object mocking or faux but quality db test data.

like image 158
Paul Sasik Avatar answered Mar 04 '23 09:03

Paul Sasik


Your best bet is to look into Dependency Injection and Mocking frameworks. This way you can swap out data providers with mocked data providers and use the data that fits your needs for the specific test.

If you're using NHibernate or similar, you can always recreate your db schema before each test(fixture).

like image 35
RoelF Avatar answered Mar 04 '23 09:03

RoelF