Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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.

share|improve this answer
TwiP looks good, thanks for pointing it out. – C. Ross May 16 '12 at 19:45
up vote 1 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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