5

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
  • I'm surprised this question has so few replies. Has nothing changed in 10 years? Commented Apr 8, 2020 at 16:25

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
And also take in mind that the script should be executed before any test but only once.
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.
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.
2

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.

3 Comments

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: 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.
Yes but I have to build the SQLHelper and I expected to not have to do it :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.