Skip to content

Improve inference of type arguments in generic test methods #150

@michaelamaura

Description

@michaelamaura

Currently the deduction of type arguments in NUnit is somewhat limited.

For example the following will work:

public static ITestCaseData[] TestCasesForEqualitiy =
{
    new TestCaseData(1, 1).Returns(true), 
    new TestCaseData(1, 2).Returns(false), 
};

[Test]
[TestCaseSource("TestCasesForEqualitiy")]
public bool Equality<TElement>(TElement left, TElement right)
{
    return left.Equals(right);
}

While the following won't work:

public static ITestCaseData[] TestCasesForEqualitiy =
{
    new TestCaseData(1, 1).Returns(true), 
    new TestCaseData(1, 2).Returns(false), 
};

[Test]
[TestCaseSource("TestCasesForEqualitiy")]
public bool Equality<TElement>(TElement? left, TElement? right)
{
    return left.Equals(right);
}

As far as I can see the modification would be local to NUnit.Framework.Internal.Builders.NUnitTestCaseBuilder.GetTypeArgumentsForMethod(...).

I have coded a somewhat complex type inference logic for another project. I'll check into it and will happily supply it to NUnit project.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions