How to name and organize unit tests that test a method with several parameters?

Given this method that should be tested:

// Search the given value using the provided search options
// Return true if the searchValue was found; false otherwise 
bool Search(string searchValue, bool useExactSearch, bool useIndexing)

I have 6 important searchValues ​​(one with ponctuation, one with accented characters, one with line breaks, etc.) that I need to check with every possible combination of useExactSearch and useIndexing. This means 54 test cases.

How do you do this? Are you really writing 54 unit tests? If so, what do you call them? Do you only write tests for the most significant cases? Do you write unit tests that sort through a table of parameter values ​​and expected results? If I do a single unit test, it is harder to find which case is aborted when continuous integration reports an error.

+5
source share
3 answers

If you are using NUnit (.Net), you can do this:

[TestCase("John*",true, false, false)]
[TestCase("*user*",false, true, true)]
[TestCase(".",true, false, true)]
public void SearchTest(string param1, bool param2, bool param3, bool expectedResult)
{
    YourClass clazz = new YourClass();
    bool result = clazz.Search(param1, param2, param3);
    Assert.AreEqual(expectedResult, result);
}

The NUnit Test Runner will do this 3 times. And, as you see here , the report shows all the test cases that help determine who broke the assembly. This is probably available for most xUnit frameworks.

+4
source

In his book, The Art of Unit Testing, Roy Osherow recommends using this naming convention:

MethodUnderTest_ConditionUnderTest_ExpectedBehaviour()

Which seems reasonable. As for how to do several tests of the same function with different parameters, I think that separate tests will simplify what went wrong, and you can use refactoring to minimize duplication between tests.

, , NUnit , . " " 54 " , ".

+3

You need to write tests, at least for meaningful test cases, and tests can be called according to a test case, such as TestSeachForPunctuation, TestSeachForAccentedChars, TestSeachForLineBreaks.

0
source

All Articles