【问题标题】:Testing with empty string using DataRow in MsTestV2在 MsTestV2 中使用 DataRow 使用空字符串进行测试
【发布时间】:2021-10-11 12:47:55
【问题描述】:

我正在尝试构建一个数据驱动的测试,该测试将在接收到空字符串或空值时验证方法的行为。我所做的是添加到 DataRow 属性,但除非我添加显示名称,否则它们被视为单行。使用显示名称,我的测试被调用了 2 次,但两次调用都将值设置为 null。是否可以使用 DataTestMethod 测试空字符串?

这是我的代码

[DataTestMethod]
[
    DataRow("", DisplayName ="Empty"),
    DataRow(null, DisplayName ="Null")
]
public void MyTest(string value)
{
    //Arrange
    //Act
    var result= new MyObject(value);

    //Assert
    Assert.IsNull(result);
}

编辑 我也用 DynamicData 测试过,结果是一样的

private static IEnumerable<Object[]> EmptyOrNullStrings { get; } = new Object[][] { 
        new Object[] { string.Empty }, 
        new Object[] { null } };
[DataTestMethod]
[DynamicData(nameof(EmptyOrNullStrings), DynamicDataSourceType.Property)]
public void ZoneComplementaireVide_IdEnveloppeNull(string zoneComplementaire){...}

【问题讨论】:

    标签: c# c#-4.0 mstest


    【解决方案1】:

    另一种解决方法是将测试项目中的包MSTest.TestAdapter 降级到版本2.2.3。我在github上创建了一个问题:https://github.com/microsoft/testfx/issues/1028

    【讨论】:

      猜你喜欢
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多