Created
November 20, 2024 03:32
-
-
Save NickStrupat/4c7f1743ff8fc4426457fe1d3c9b7838 to your computer and use it in GitHub Desktop.
Generic test case attribute for NUnit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | |
public class TestCaseAttribute<T>(params Object?[]? arguments) : TestCaseAttribute(arguments), ITestBuilder | |
{ | |
IEnumerable<TestMethod> ITestBuilder.BuildFrom(IMethodInfo method, Test? suite) | |
{ | |
if (!method.IsGenericMethodDefinition) | |
return base.BuildFrom(method, suite); | |
var length = method.GetGenericArguments().Length; | |
if (length != 1) | |
{ | |
var @params = new TestCaseParameters { RunState = RunState.NotRunnable }; | |
@params.Properties.Set(PropertyNames.SkipReason, $"Method has {length} type parameters, but only 1 type argument was provided."); | |
return [new NUnitTestCaseBuilder().BuildTestMethod(method, suite, @params)]; | |
} | |
var genMethod = method.MakeGenericMethod(typeof(T)); | |
return base.BuildFrom(genMethod, suite); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment