views:

29

answers:

3

Hello,

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.

+1  A: 

Why don't you execute the script programmatically? You can write an utility method, which takes a file name and a database connection and executes it.

SQLHelper.executeScript("example.sql", DatabaseConnection.get());

It is just one line of code, which could be copied and pasted. Maybe you don't even have to assign a database connection, if it's already established. And your tests will run everywhere.

I use individual scripts for every test suite. So I don't have to keep the SQL scripts in sync. And executing a lot of SQL statements, which aren't needed, would slowdown your tests and thereby your development cycle.

Christian Strempfer
Yes, but I expected that requirement to be a common task and I would like to avoid having to write code to do it. I thought that something would be available from VS or a commandline tool or something like that.
SoMoS
SoMoS: unit tests can be run from build scripts. They don't need visual studio. Everything a tests needs to setup should be implemented in the tests.
Stefan Steinegger
Christian Strempfer
Yes but I have to build the SQLHelper and I expected to not have to do it :)
SoMoS
+1  A: 

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.

Stefan Steinegger
Yes, but I expected that requirement to be a common task and I would like to avoid having to write code to do it. I thought that something would be available from VS or a commandline tool or something like that.
SoMoS
And also take in mind that the script should be executed before any test but only once.
SoMoS
I don't understand your critics on that. Setting up a command line tool is not easier than implementing a method. The `[AssemblyInitialize]` method is only called once (per test assembly). If you have more test assemblies, it would be tricky. I even have separate databases for each test assembly.
Stefan Steinegger
Well, I see that there is no other way so I will use the AssemblyInitialize attribute. As I created automatically the SQL script with SQL Management Studio I expected to found a way of executing the script automatically too.
SoMoS
A: 

Yes you can. You would run it as part of your TestInitialise or Pre-Test.

ChrisBD
Can you extend on that?
SoMoS