Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a SQL script to prepare DB before executing Unit Tests in Visual Studio

I have a lot of unit test that depend on a certain configuration of a database. I would like to execute a script every time I run the unit tests so the database is Ok and the tests do not fail due to wrong data at DB. I currently have a SQL script to put the right data at the DB.

Is there a way of doing that from Visual Studio (2008 would be great)?

Thanks in advance mates.

like image 729
Ignacio Soler Garcia Avatar asked Oct 15 '22 00:10

Ignacio Soler Garcia


1 Answers

you could implement methods with atributes like [ClassInitialize] and [AssemblyInitialize] which are executed once before the first test in the class or assembly. In such a method you run the script.

You could also write a method which is called in every test ([TestInitialize]) which knows if it had already been executed. This way you could run the script before every test that needs it, but not when you only run tests which do not need it.

like image 187
Stefan Steinegger Avatar answered Nov 01 '22 09:11

Stefan Steinegger