2017-07-19 68 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. 
}