Raychaser
Raychaser

Reputation: 370

Shared Context between .NET Unit test Classes

I understand that ideal unit tests should not share any context between them but I have a dilemma: I am trying to unit test against a piece of software that only has a single license that can be instantiated at a time.

.NET unit tests seem to run in parallel so when I click "Run all tests" the classes are all run simultaneously and most fail because they can't all have a license.

So I see two questions that are mutually exclusive:

Clarification: The licensed software is not what I'm trying to test, it's just the tool I need to DO the test

Upvotes: 1

Views: 2134

Answers (2)

Carlos Torrecillas
Carlos Torrecillas

Reputation: 5756

I'm not sure if this is what you might be looking for but would that work if you run all tests at the same time however each of them runs on a separate AppDomain?

For reference I used the cross domain delegates where you could pass your actual test: https://msdn.microsoft.com/en-us/library/system.appdomain.docallback(v=vs.110).aspx

Let me know if that's works!

Upvotes: 0

Bruno Garcia
Bruno Garcia

Reputation: 6408

Normally I'd consider Singleton an anti-pattern since it makes unit testing impossible. But this a good use case to have a Singleton. A real singleton, with a private constructor and a static constructor, will run only once and will be thread-safe.

This way you can keep your tests running in parallel.

Upvotes: 1

Related Questions