2017-07-19 69 views
-1

我正在为具有一组过滤器选项来过滤数据的应用程序工作。输入组合的单元测试

我想测试我的方法将要工作的输入的每个组合。比如我必须写单元测试每个组合如下图所示:

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

    } 

    [TestMethod] 
    public void _For_Product_And_Category() 
    { 

    } 

    [TestMethod] 
    public void _For_Product_CreationDate() 
    { 

    } 
} 

有什么方法来测试与单一的测试数据的每个组合。我回顾了NUnit测试的blog。实现这种测试的可能方式是什么?哪些是支持组合测试的框架。

回答

2

是它可能与NUnit的2.5及以上

[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); 
} 

一些更多的信息here

+0

谢谢你的细节,但你可以看到我们必须在这里提供TestCase。我想知道是否有任何框架可以自动执行此操作。我研究发现FsCheck可以工作,但没有经验。 –

+1

所以你想要读取你的代码并自动为它创建单元测试?我认为这就是所谓的程序员或开发人员等...... –

+0

在这种情况下,您可能需要查看https://github.com/nunit/docs/wiki/Theory-Attribute。 –

0

这当然是可能的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) 
     { 

     } 
    } 
-1

发现这个库,可用于随机组合测试: FsCheck

0

你还没有给出任何你想要自动合并的例子,所以我不得不为这个答案创造它。

NUnit有几种方法可以将数据指定为与测试方法的单个参数相对应的参数以及组合这些参数的多种方法。

指定参数: * ValuesAttribute * ValueSourceAttribute * RandomAttribute * RangeAttribute

产生上述值的组合: * CombinatorialAttribute(这个是默认的,如果你不使用任何东西) * PairwiseAtribute * SequentialAttribute

例...

[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. 
}