Skip to content

Instantly share code, notes, and snippets.

@NickStrupat
Created November 20, 2024 03:32
Show Gist options
  • Save NickStrupat/4c7f1743ff8fc4426457fe1d3c9b7838 to your computer and use it in GitHub Desktop.
Save NickStrupat/4c7f1743ff8fc4426457fe1d3c9b7838 to your computer and use it in GitHub Desktop.
Generic test case attribute for NUnit
[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