unit-testing MakeSut concept


Example

Testcode has the same quality demands, as production code. MakeSut()

  • improves readability
  • can be easily refactored
  • perfectly supports dependency injection.

Here's the concept:

[Test]
public void TestSomething()
{
    var sut = MakeSut();
    
    string result = sut.Do();
    Assert.AreEqual("expected result", result);
}

The simplest MakeSut() just returns the tested class:

private ClassUnderTest MakeSUT()
{
    return new ClassUnderTest();
}

When dependencies are needed, they can be injected here:

private ScriptHandler MakeSut(ICompiler compiler = null, ILogger logger = null, string scriptName="", string[] args = null)
{
    //default dependencies can be created here
    logger = logger ?? MockRepository.GenerateStub<ILogger>();
    ...
}

One might say, that MakeSut is just a simple alternative for setup and teardown methods provided by Testrunner frameworks and might cosider these methods a better place for test specific setup and teardown.

Everybody can decide on her own, which way to use. For me MakeSut() provides better readability and much more flexibility. Last but not least, the concept is independent from any testrunner framework.