If you are a regular reader of this blog, you may have noticed that for the last couple of months, every test I've posted has shared some similarities; one of which is that I always name my SUT test variable sut. Here's a simple example:
public void DoStuffWillReturnMessage()
// Fixture setup
string expectedResult = "ploeh";
MyClass sut = new MyClass();
// Exercise system
string result = sut.DoStuff(expectedResult);
// Verify outcome
Assert.AreEqual<string>(expectedResult, result, "DoStuff");
Notice how the new instance of MyClass is named sut, and not, say, mc.
No matter the name of the class, I always name the variable sut, since it gives me at least two advantages:
1. It Very Clearly Identifies the SUT
In the above example, it may be very apparent that the SUT is an instance of MyClass no matter the name of the variable. However, as test complexity grows, the sut variable may disappear among other variables, as this example illustrates:
public void ComplexOperationWillSucceed()
string expectedBar = "bar";
Fnaah f = new Fnaah();
Ndøh n = new Ndøh(f);
Foo result = sut.ComplexOperation(n);
Assert.AreEqual<string>(expectedBar, result.Bar, "ComplexOperation");
Had the SUT variable been mc, it would have been much less apparent which of f, n, or mc was the actual SUT, and which variables just belonged to the Test Fixture. Although the rest of the Four-Phase Test structure (including the comments) helps the Test Reader in figuring out which of the variables is the SUT, the name of the variable is definitely a helpful hint.
2. It Is Robust Against Refactoring
Imagine that instead of sut I had named the MyClass variable mc in all my 200 tests, and that I then decided to rename MyClass to ØhmHvadVedJeg (note to self: Use Danish characters in class names more often!). In that case, I would now have 200 tests where instances of ØhmHvadVedJeg were called mc. That's not very nice to the poor Test Reader.
You could of course manually edit all 200 tests to align the variable names again, but that's a bit of work, and you may even forget that you have to do that, since the renaming probably took place somewhere completely different.
When the SUT variable name is sut, you can rename your types as much as you want; the tests will still be communicative and correct.