Manprit Singh Sahota
Manprit Singh Sahota

Reputation: 1339

Unit testing for Combination of inputs

I am working for an application that has set of filter options to filter the data.

I want to test each combination of input for which my method is going to work. For example do I have to write Unit test for each combination like below:

[TestClass]
public class Data_Should_Filter
{
    [TestMethod]
    public void _For_Category()
    {

    }

    [TestMethod]
    public void _For_Product_And_Category()
    {

    }

    [TestMethod]
    public void _For_Product_CreationDate()
    {

    }
}

Is there any way to test each combination of data with single test. I review the blog for NUnit test. What are the possible way to achieve this kind of testing and which are the frameworks that support combination testing.

Upvotes: 0

Views: 2980

Answers (4)

Charlie
Charlie

Reputation: 13736

You haven't given any examples of what you want to automatically combine, so I have had to invent it for this answer.

NUnit has several ways to specify data as a argument corresponding to a single parameter of the test method as well as several ways of combining those arguments.

Specify arguments: * ValuesAttribute * ValueSourceAttribute * RandomAttribute * RangeAttribute

Generate combinations of the above values: * CombinatorialAttribute (this one is the default if you don't use anything) * PairwiseAtribute * SequentialAttribute

Example...

[Test]
public void TestProcuctAndCategory(
    [Values("ProductA", ProductB")] string productName,
    [Values("Cat1", "Cat2", "Cat3")] string category)
{
    // Test will be executed six times, using all combinations
    // of the values provided for the two arguments.
}

Upvotes: 2

Manprit Singh Sahota
Manprit Singh Sahota

Reputation: 1339

Found this library which can be used for Random combinations testing: FsCheck

Upvotes: -1

Janus Pienaar
Janus Pienaar

Reputation: 1103

It's certainly possible with Nunit:

[TestFixture]
    public class Data_Should_Filter
    {
        [Test]
        [TestCase(new Product(1), new Category(2), DateTime.UtcNow)]
        [TestCase(new Product(2), new Category(2), DateTime.UtcNow)]
        public void TestFilter(Product product, Category category, DateTime creationDate)
        {

        }
    }

Upvotes: 1

Stephen Wilson
Stephen Wilson

Reputation: 1514

Yes it's possible with NUnit 2.5 and above

[TestCase(12,3,4)]
[TestCase(12,2,6)]
[TestCase(12,4,3)]
public void DivideTest(int n, int d, int q)
{
    Assert.AreEqual( q, n / d );
}

Some more info here

Upvotes: 2

Related Questions