views:

40

answers:

3

Hi everyone,

I have a Unit Test project with 20+ .cs files. I want to run some setup code before each individual test. Kinda like how the [TestInitialize] attribute works. However, I'd need to put that attribute on all 20+ of my .cs files.

Is there a way to centralize the initializing code in one place for every test in my entire project?

Thanks!

-Mike

A: 

I am not very familiar with unit testing in C# but you could write an abstract test class and put the initializing code there and derive each individual test class from this abstract one.

mpistrich
A: 

Mike the only bootstrapping hooks are [ClassInitialize] and [TestInitialize] and their teardown counterparts. In cases like these I just externalized the common logic into its own class, essentially follow normal DRY and SoC practices. Typically I have several services and providers defined within my test assemblies and the xInitialize methods just have 1 or 2 lines of code to call the approperiate provider. That being said mpistrich's answer is perfectly acceptable as well, I perfer layering over inheritence.

Tedford
A: 

I don't know whether this is still an issue. But I found one more solution do achieve this: When opening the .testsettings file, go to the "Setup and Cleanup scripts" tab, you can specify scripts to run before and after the tests run.

Peter van Kekem