views:

29

answers:

1

Within a test file (MyTest.cs) it is possible to do setup and teardown at the class and the individual test level. Do similar hooks exist for the entire project? Entire solution?

+1  A: 

No I don't believe that they do.

Typically when people ask this question it's because they have tests whoch are depending on something heavy, like a DB setup which needs to be reset for each test pass. Usually the right thing to do here is to mock/stub/fake the dependency out and remove the part that's causing the issue. Of course your reasons for this may be completely different.

Updated: So thinking about this some more I think you could do something with attributes and static types. You could add an assembly level attribute to each test assembly and pass it a static type.

[OnLoadAttribute(typeof(ProjectInitializer))]

When the assembly loads the type will get resolved and it's static constructor will be executed the first time it's resolved (when the assembly is loaded).

Doing something at a solution level is much harder because it depends how your unit test runner deals with tests and how it loads tests into AppDomains, per test, per test class or per test project. I suspect most runners create a new AppDomain per project.

I'm not exactly recommending this as I haven't tried it and there may be some repercussions. It's an idea you might want to try. Another option would be do derive all your tests from a common base class which has a constructor which resolves a singleton that in turn does your setup. This is less hacky but means having a common base class. You could also use an aspect oriented approach I suspect.

Hope this helps. These are just thoughts as to how you could do this.

Ade

Ade Miller
Yes you are close. We have the ability to dynamically substitute a mock database context for a real one based on an app.config setting. Most of the time we would use the mock, however for load tests we would switch to live data.
Steve. I had some thoughts on this, see new update above.
Ade Miller