tags:

views:

317

answers:

1

Question

Is there a way to have a method that will always run anytime that test assembly is run through MSTest?

Similar to how the [TestInitialize] and [ClassInitialize] attributes work, but for the entire assembly. I do not want to have to add code to every test class's [ClassInitialize] method.

Reasoning

Some of my tests interact with the database. They delete data and other things that would be very harmful to a production database. There is only a configuration file that tells my unit test project to run against the non-production database.

I would feel better if there was a method that would run on startup that would say "Okay Database name is not 'production'"

Ideas

Log4Net uses an assembly attribute to configure itself.

using log4net.Config;
[assembly: XmlConfigurator()]

Perhaps I can do something simliar?

[assembly:  CheckDatabaseNameNot("production")]
+5  A: 

Have you tried [AssemblyInitialize]?

SaaS Developer
I thought I looked for such an attribute, but alas yes that does work. I can throw an exception from the method if the database saftey check fails.
quip
I thought you were joking at first when I read your answer -- because I was so sure I looked for it before asking my question!
quip
Lol, yeah we've all been there before.
SaaS Developer