質問

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

または類似のもの。

役に立ちましたか?

解決

NUnit 2.5のリリースノートからの引用リンクテキスト

  

パラメータ化されたテスト方法は   ジェネリック。 NUnitは正しいと推定します   に基づいて使用する実装   提供されるパラメーターのタイプ。   一般的なテスト方法はでサポートされています   ジェネリッククラスと非ジェネリッククラスの両方。

これによれば、非ジェネリッククラスにジェネリックテストメソッドを含めることができます。方法は?

ジェフのコメントはよくわかりません。 .netジェネリックでは、コンパイル時と実行時の両方があります。リフレクションを使用して、メソッドに関連付けられているテストケース属性を見つけ、ジェネリックパラメーターを見つけ、再度リフレクションを使用してジェネリックメソッドを呼び出すことができます。機能しますか?

更新:OK、わかりました。手遅れではないことを願っています。パラメータリストにあるジェネリック型が必要です。例:

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

他のヒント

カスタム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);
    }
}

プライベートメソッドを作成して呼び出す:

    [Test]
    public void TypeATest()
    {
        MyTest<TypeA>();
    }

    [Test]
    public void TypeBTest()
    {
        MyTest<TypeB>();
    }

    private void MyTest<T>()
    {
        // do test.
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top