Testing VS.Net blocks - is it possible to set up a test setup for a project?

In the test file (MyTest.cs), you can configure and disable the class and at a separate testing level. Are there similar hooks for the whole project? The whole solution?

+4
source share
2 answers

No, I do not believe that they do it.

Usually, when people ask this question because they have whoch tests, depending on something difficult, for example, setting the database, which should be reset for each test. Usually the right thing here is to mock / drown / fake the addiction and remove the part that causes the problem. Of course, your reasons for this can be completely different.

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

[OnLoadAttribute(typeof(ProjectInitializer))] 

When the assembly is loaded, the type will be allowed, and the static constructor will be executed at the first resolution (when the assembly is loaded).

Doing something at the solution level is a lot harder because it depends on 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 that most runners create a new AppDomain for each project.

I do not recommend this because I have not tried it, and there may be some consequences. This is an idea that you might want to try. Another option is to infer all your tests from a common base class that has a constructor that solves a singleton, which in turn does your setup. This is less dangerous, but has a common base class. You can also use the aspect-oriented approach that I suspect.

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

Ade

+1
source

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

  • test database creation
  • creating configuration files in the temp directory

This works great for us, although we have to be careful that each test leaves the database as it finds it. It looks a bit (simplified):

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

I donโ€™t know a way to make this โ€œsolutionโ€ wide (I think it really means for a test run - through potentially several projects)

0
source

Source: https://habr.com/ru/post/1308050/


All Articles