【问题标题】:Unit testing for Combination of inputs输入组合的单元测试
【发布时间】:2017-07-19 09:57:22
【问题描述】:

我正在为具有一组过滤器选项来过滤数据的应用程序工作。

我想测试我的方法适用的每种输入组合。例如,我是否必须为每个组合编写单元测试,如下所示:

[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。有哪些可能的方式来实现这种测试,哪些框架支持组合测试。

【问题讨论】:

    标签: c# unit-testing nunit xunit


    【解决方案1】:

    是的,使用 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

    【讨论】:

    • 感谢您提供详细信息,但正如您所见,我们必须在此处提供 TestCase。我想知道是否有任何框架可以自动执行此操作。我研究了一个发现 FsCheck 可以工作但没有经验。
    • 所以你想要一些东西来阅读你的代码并自动为它创建单元测试?我认为这被称为程序员或开发人员等......
    • 在这种情况下,您可能需要查看github.com/nunit/docs/wiki/Theory-Attribute
    【解决方案2】:

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

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

    指定参数: * 值属性 * 值源属性 * 随机属性 * 范围属性

    生成上述值的组合: * CombinatorialAttribute (如果你不使用任何东西,这个是默认的) * 成对属性 * 顺序属性

    示例...

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

    【讨论】:

      【解决方案3】:

      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)
              {
      
              }
          }
      

      【讨论】:

        【解决方案4】:

        找到了这个可用于随机组合测试的库: FsCheck

        【讨论】:

          猜你喜欢
          • 2014-06-01
          • 2023-03-15
          • 1970-01-01
          • 1970-01-01
          • 2016-11-07
          • 2020-12-27
          • 1970-01-01
          • 2017-04-25
          • 2018-12-27
          相关资源
          最近更新 更多