I often want to have the same unit test applied to data multiple items that are roughly equivalent (two non-empty strings). Currently I implement these in multiple unit test methods, with names like MethodNonEmptyValidString1 and MethodNonEmptyValidString2. This has a bit of a code smell to me.

Is it better to have the multiple data items in one method with multiple asserts? Is there another design pattern that models this more accurately?

2 Answers 2


Perhaps your naming is a bit uncreative and non-descriptive.

Anyway, using Java I would test the same thing with different data using one of two ways:

Both allow feeding different sets of data to the same test method. Your tests will have names alluding to the parameters in your test output.


Well, NUnit has a TestCase attribute for just this purpose, which seems to solve the problem neatly.

    [TestCase(NonMatchingName1, NonMatchingValue1)]
    [TestCase(NonMatchingName2, NonMatchingValue2)]
    public void TestNonMatchingAttribute(string name, string expectedValue)

This type of structure solves the problem fairly well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.