【问题标题】:Parametric test with generic methods使用通用方法进行参数测试
【发布时间】:2009-04-29 06:43:33
【问题描述】:

在 NUnit 2.5 中,您可以这样做:

[TestCase(1,5,7)]
public void TestRowTest(int i, int j, int k)
{
  Assert.AreEqual(13, i+j+k);
}

你可以做参数测试。

但我想知道您是否可以这样做,使用通用测试方法进行参数测试?即:

[TestCase <int>("Message")]
public void TestRowTestGeneric<T>(string msg)
{
  Assert.AreEqual(5, ConvertStrToGenericParameter<T>(msg));
}

或类似的东西。

【问题讨论】:

    标签: unit-testing nunit


    【解决方案1】:

    这是来自 NUnit 2.5 link text发行说明的引用

    参数化的测试方法可以是 通用的。 NUnit 会推断出正确的 实现使用基于 提供的参数类型。 支持通用测试方法 泛型和非泛型类。

    据此,非泛型类中可以有泛型测试方法。怎么样?

    我不太明白 Jeff 的评论。在 .net 中,泛型既是编译时又是运行时。我们可以通过反射找出方法关联的测试用例属性,找出泛型参数,然后再次使用反射调用泛型方法。它会起作用的,不是吗?

    更新:好的,我现在知道怎么做,希望还不算太晚。您需要将泛型类型放在参数列表中。例如:

    [TestCase((int)5, "5")]
    [TestCase((double)2.3, "2.3")]
    public void TestRowTestGeneric<T>(T value, string msg)
    {
      Assert.AreEqual(value, ConvertStrToGenericParameter<T>(msg));
    }
    

    【讨论】:

      【解决方案2】:

      您可以自定义 GenericTestCaseAttribute

          [Test]
          [GenericTestCase(typeof(MyClass) ,"Some response", TestName = "Test1")]
          [GenericTestCase(typeof(MyClass1) ,"Some response", TestName = "Test2")]
          public void MapWithInitTest<T>(string expectedResponse)
          {
              // Arrange
      
              // Act
              var response = MyClassUnderTest.MyMethod<T>();
      
              // Assert
              Assert.AreEqual(expectedResponse, response);
          }
      

      这里是 GenericTestCaseAttribute 的实现

      [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
      public class GenericTestCaseAttribute : TestCaseAttribute, ITestBuilder
      {
          private readonly Type _type;
          public GenericTestCaseAttribute(Type type, params object[] arguments) : base(arguments)
          {
              _type = type;
          }
      
          IEnumerable<TestMethod> ITestBuilder.BuildFrom(IMethodInfo method, Test suite)
          {
              if (method.IsGenericMethodDefinition && _type != null)
              {
                  var gm = method.MakeGenericMethod(_type);
                  return BuildFrom(gm, suite);
              }
              return BuildFrom(method, suite);
          }
      }
      

      【讨论】:

      • 谢谢你,我一直在苦苦挣扎好几天,你的回答帮助我终于做对了!
      • 这太棒了。我等不及要通过拉取请求审查了!
      【解决方案3】:

      创建一个私有方法并调用它:

          [Test]
          public void TypeATest()
          {
              MyTest<TypeA>();
          }
      
          [Test]
          public void TypeBTest()
          {
              MyTest<TypeB>();
          }
      
          private void MyTest<T>()
          {
              // do test.
          }
      

      【讨论】:

      • 我不得不为我的测试使用这个方法,看起来像这样:[TestCase(new float[] { 1, 2, 3, 4, 5 }, new float[] { 1, 2, 3, 4, 5 }, true)] public void AbleToCompareEqualArrays(T[] ar1, T[] ar2, bool expectedValue)
      • 不错的答案。这比创建自定义属性要好得多(恕我直言)
      猜你喜欢
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多