jeudi 26 mai 2016

Using parameterized tests with "ExpectedResult" and following Osherove's 3-part naming convention

I really like Roy Osherove's 3-part naming convention for naming unit tests:

[UnitOfWork_StateUnderTest_ExpectedBehavior]

Which to me means something like this (even when using parameterized inputs):

[TestCase("user1")]
[TestCase("user2")]
public void SayHello_OnExistingUser_ReturnsHelloMessage(string username) 
{
    Assert.AreEqual("Hello " + username, SayHello(username));
}

[TestCase("badusername")]
[TestCase("")]
public void SayHello_OnNonExistingUser_ReturnsNull(string username) 
{
    Assert.IsNull(SayHello(username));
}

However, this is a bit tricky when using parameterized unit tests with specified expected result per test case, such as:

[TestCase("user1", ExpectedResult = "Hello user1")]
[TestCase("user2", ExpectedResult = "Hello user2")]
[TestCase("badusername", ExpectedResult = null)]
[TestCase("", ExpectedResult = null)]
public void TESTNAMEHERE(string username)
{
    return SayHello(username);
}

With parameters for both the StateUnderTest and ExpectedBehavior it is less obvious what the test name should be. I have heard suggestions to NOT use the "expected result" in TestCase for this reason, but it seems like a potentially great functionality that could simplify the test code.

I'd like to hear some suggestions for how to do this. Abandon ExpectedResult for the sake of unit test naming clarity? Or trade some clarity for test code brevity?

Aucun commentaire:

Enregistrer un commentaire