Abstract Context
I have a static stateful class (constraint: that I cannot change) that would like to cover 100%. It is a static class with a static state field. I'd like to test different inputs to a static method in the scenario where state is not already "set". This object does not have a publicly accessible mechanism for altering the piece of state of interest to the test.
Yes, I recognize this is a very good example of static-ness making testing painful.
My question here is to see how clever I can be without refactoring the unit under test.
I have not been able to find a framework way to accomplish this; I am aware that I could spin up a custom app domain for each test to get additional "copies" of my static object, but would like to avoid the heavy overhead (especially if I would be duplicating something the framework can accomplish).
Concrete Example
Consider the following class
public class Class1 { private static string _state; public static string DoStaticStateThing(string thing) { if(_state == null) { _state = thing; } return _state; } } and the following test fixture(s)
using NUnit.Framework; [TestFixtureSource(nameof(SourceEnumeration))] public class Class1Tests { private string _testInput; static object[] SourceEnumeration = { new object[] { "foo" }, new object[] { "bar" } }; public Class1Tests(string input) { _testInput = input; } [Test] public void TestStatefulThing() { string result = Class1.DoStaticThing(_testInput); Assert.AreEqual(_testInput, result); } } This results in the following test results
I was expecting both tests to start from the same state, where _thing is null, but this observation makes it clear that the object I am testing is surviving in memory between test executions.
I've poked around the documentation, found an interesting looking commit calling out MultipleDomainTestRunner that appears to no longer exist, and tried using the /domain=Multiple flag, all with no luck.
My Question
Can I alter the
Class1TestsTestFixturein some way to accomplish what I want: each test executing over the same "starting state" of a stateful object?
