开发者

VS.Net Unit Testing -- possible to have project-scoped test setup?

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


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


We use the [AssemblyInitialize] / [AssemblyCleanup] attributes for project level test setup and cleanup code. We do this for two things:

  • creating a test database
  • creating configuration files in a temp directory

It works fine for us, although we have to be careful that each test leaves the database how it found it. Looks a little like this (simplified):

[AssemblyInitialize]
public static void AssemblyInit(TestContext context)
{
    ConnectionString = DatabaseHelper.CreateDatabaseFornitTests();
}

[AssemblyCleanup]
public static void AssemblyCleanup()
{
    DatabaseHelper.DeleteDatabase(ConnectionString);
}

I do not know of a way to do this 'solution' wide (I guess this really means for the test run - across potentially multiple projects)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜