Instead, you can include Type as an input parameter. For instance:.
[Theory] [MemberData(SomeTypeScenario)] public void TestMethod(Type type) { Assert.Equal(typeof(double), type); } public static IEnumerable<object[]> SomeScenario() { yield return new object[] { typeof(double) }; }
No need to go with generics in xunit.
Edit (if you really need shared files)
1) You need to subclass ITestMethod to save general information about the method, you also need to implement IXunitSerializable
// assuming namespace Contosco public class GenericTestMethod : MarshalByRefObject, ITestMethod, IXunitSerializable { public IMethodInfo Method { get; set; } public ITestClass TestClass { get; set; } public ITypeInfo GenericArgument { get; set; } /// <summary /> [Obsolete("Called by the de-serializer; should only be called by deriving classes for de-serialization purposes")] public GenericTestMethod() { } public GenericTestMethod(ITestClass @class, IMethodInfo method, ITypeInfo genericArgument) { this.Method = method; this.TestClass = @class; this.GenericArgument = genericArgument; } public void Serialize(IXunitSerializationInfo info) { info.AddValue("MethodName", (object) this.Method.Name, (Type) null); info.AddValue("TestClass", (object) this.TestClass, (Type) null); info.AddValue("GenericArgumentAssemblyName", GenericArgument.Assembly.Name); info.AddValue("GenericArgumentTypeName", GenericArgument.Name); } public static Type GetType(string assemblyName, string typeName) {
2) You need to write your own discoverer for common methods, it must be a subclass of IXunitTestCaseDiscoverer
// assuming namespace Contosco public class GenericMethodDiscoverer : IXunitTestCaseDiscoverer { public GenericMethodDiscoverer(IMessageSink diagnosticMessageSink) { DiagnosticMessageSink = diagnosticMessageSink; } protected IMessageSink DiagnosticMessageSink { get; } public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute) { var result = new List<IXunitTestCase>(); var types = factAttribute.GetNamedArgument<Type[]>("Types"); foreach (var type in types) { var typeInfo = new ReflectionTypeInfo(type); var genericMethodInfo = testMethod.Method.MakeGenericMethod(typeInfo); var genericTestMethod = new GenericTestMethod(testMethod.TestClass, genericMethodInfo, typeInfo); result.Add( new XunitTestCase(DiagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), genericTestMethod)); } return result; } }
3) Finally, you can make your attribute for common methods and bind it to your custom discoverer with the XunitTestCaseDiscoverer attribute
// assuming namespace Contosco [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] [XunitTestCaseDiscoverer("Contosco.GenericMethodDiscoverer", "Contosco")] public sealed class GenericMethodAttribute : FactAttribute { public Type[] Types { get; private set; } public GenericMethodAttribute(Type[] types) { Types = types; } }
Using:
[GenericMethod(new Type[] { typeof(double), typeof(int) })] public void TestGeneric<T>() { Assert.Equal(typeof(T), typeof(double)); }