visual unitarias unit test studio pruebas hacer ejemplo como c# unit-testing generics nunit testcase

c# - unitarias - NUnit TestCase con genéricos



pruebas unitarias visual studio (7)

¿Hay alguna manera de pasar los tipos genéricos usando un TestCase a una prueba en NUnit?

Esto es lo que me gustaría hacer, pero la sintaxis no es correcta ...

[Test] [TestCase<IMyInterface, MyConcreteClass>] public void MyMethod_GenericCall_MakesGenericCall<TInterface, TConcreteClass>() { // Arrange // Act var response = MyClassUnderTest.MyMethod<TInterface>(); // Assert Assert.IsInstanceOf<TConcreteClass>(response); }

O si no, ¿cuál es la mejor manera de lograr la misma funcionalidad (obviamente tendré múltiples TestCases en el código real)?

Actualiza con otro ejemplo ...

Aquí hay otro ejemplo con un solo tipo genérico pasado ...

[Test] [TestCase<MyClass>("Some response")] public void MyMethod_GenericCall_MakesGenericCall<T>(string expectedResponse) { // Arrange // Act var response = MyClassUnderTest.MyMethod<T>(); // Assert Assert.AreEqual(expectedResponse, response); }


Comience primero con la prueba, incluso cuando realice la prueba. ¿Qué quieres hacer? Probablemente algo como esto:

[Test] public void Test_GenericCalls() { MyMethod_GenericCall_MakesGenericCall<int>("an int response"); MyMethod_GenericCall_MakesGenericCall<string>("a string response"); : }

Entonces puede simplemente hacer que su prueba sea una vieja prueba de función. Sin marcador [Test]

public void MyMethod_GenericCall_MakesGenericCall<T>(string expectedResponse) { // Arrange // Act var response = MyClassUnderTest.MyMethod<T>(); // Assert Assert.AreEqual(expectedResponse, response); }


Como podría estar probando con funciones genéricas que devuelven objetos ?. Ejemplo:

public Empleado TestObjetoEmpleado(Empleado objEmpleado) { return objEmpleado; }

Gracias


Hice algo similar la semana pasada. Esto es lo que terminé con:

internal interface ITestRunner { void RunTest(object _param, object _expectedValue); } internal class TestRunner<T> : ITestRunner { public void RunTest(object _param, T _expectedValue) { T result = MakeGenericCall<T>(); Assert.AreEqual(_expectedValue, result); } public void RunTest(object _param, object _expectedValue) { RunTest(_param, (T)_expectedValue); } }

Y luego la prueba en sí:

[Test] [TestCase(typeof(int), "my param", 20)] [TestCase(typeof(double), "my param", 123.456789)] public void TestParse(Type _type, object _param, object _expectedValue) { Type runnerType = typeof(TestRunner<>); var runner = Activator.CreateInstance(runnerType.MakeGenericType(_type)); ((ITestRunner)runner).RunTest(_param, _expectedValue); }


Los atributos en C # no pueden ser genéricos, por lo que no podrá hacer las cosas exactamente como lo desee. Quizás lo más fácil sería poner los atributos TestCase en un método auxiliar que utiliza la reflexión para llamar al método real. Algo como esto podría funcionar (nota, no probado):

[TestCase(typeof(MyClass), "SomeResponse")] public void TestWrapper(Type t, string s) { typeof(MyClassUnderTest).GetMethod("MyMethod_GenericCall_MakesGenericCall").MakeGenericMethod(t).Invoke(null, new [] { s }); }


Los métodos de prueba de NUnit en realidad pueden ser genéricos siempre que los argumentos de tipo genérico se puedan inferir a partir de los parámetros:

[TestCase(42)] [TestCase("string")] [TestCase(double.Epsilon)] public void GenericTest<T>(T instance) { Console.WriteLine(instance); }

Si los argumentos genéricos no se pueden inferir, el corredor de prueba no tendrá una pista sobre cómo resolver los argumentos de tipo:

[TestCase(42)] [TestCase("string")] [TestCase(double.Epsilon)] public void GenericTest<T>(object instance) { Console.WriteLine(instance); }

Pero en este caso puede implementar un atributo personalizado:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public class TestCaseGenericAttribute : TestCaseAttribute, ITestBuilder { public TestCaseGenericAttribute(params object[] arguments) : base(arguments) { } public Type[] TypeArguments { get; set; } IEnumerable<TestMethod> ITestBuilder.BuildFrom(IMethodInfo method, Test suite) { if (!method.IsGenericMethodDefinition) return base.BuildFrom(method, suite); if (TypeArguments == null || TypeArguments.Length != method.GetGenericArguments().Length) { var parms = new TestCaseParameters { RunState = RunState.NotRunnable }; parms.Properties.Set("_SKIPREASON", $"{nameof(TypeArguments)} should have {method.GetGenericArguments().Length} elements"); return new[] { new NUnitTestCaseBuilder().BuildTestMethod(method, suite, parms) }; } var genMethod = method.MakeGenericMethod(TypeArguments); return base.BuildFrom(genMethod, suite); } }

Uso:

[TestCaseGeneric("Some response", TypeArguments = new[] { typeof(IMyInterface), typeof(MyConcreteClass) }] public void MyMethod_GenericCall_MakesGenericCall<T1, T2>(string expectedResponse) { // whatever }

Y una personalización similar para TestCaseSourceAttribute :

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public class TestCaseSourceGenericAttribute : TestCaseSourceAttribute, ITestBuilder { public TestCaseSourceGenericAttribute(string sourceName) : base(sourceName) { } public Type[] TypeArguments { get; set; } IEnumerable<TestMethod> ITestBuilder.BuildFrom(IMethodInfo method, Test suite) { if (!method.IsGenericMethodDefinition) return base.BuildFrom(method, suite); if (TypeArguments == null || TypeArguments.Length != method.GetGenericArguments().Length) { var parms = new TestCaseParameters { RunState = RunState.NotRunnable }; parms.Properties.Set("_SKIPREASON", $"{nameof(TypeArguments)} should have {method.GetGenericArguments().Length} elements"); return new[] { new NUnitTestCaseBuilder().BuildTestMethod(method, suite, parms) }; } var genMethod = method.MakeGenericMethod(TypeArguments); return base.BuildFrom(genMethod, suite); } }

Uso:

[TestCaseSourceGeneric(nameof(mySource)), TypeArguments = new[] { typeof(IMyInterface), typeof(MyConcreteClass) }]


Puede hacer CustomGenerTestCaseAttribute

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

Aquí está la implementación de 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); } }


Tuve la oportunidad de hacer algo similar hoy, y no estaba contento con el uso de la reflexión.

Decidí aprovechar [TestCaseSource] en lugar de ello delegando la lógica de prueba como contexto de prueba en una clase de prueba genérica, anclada en una interfaz no genérica, y llamé a la interfaz desde pruebas individuales (mis pruebas reales tienen muchos más métodos en la interfaz, y use AutoFixture para configurar el contexto):

class Sut<T> { public string ReverseName() { return new string(typeof(T).Name.Reverse().ToArray()); } } [TestFixture] class TestingGenerics { public IEnumerable<ITester> TestCases() { yield return new Tester<string> { Expectation = "gnirtS"}; yield return new Tester<int> { Expectation = "23tnI" }; yield return new Tester<List<string>> { Expectation = "1`tsiL" }; } [TestCaseSource("TestCases")] public void TestReverse(ITester tester) { tester.TestReverse(); } public interface ITester { void TestReverse(); } public class Tester<T> : ITester { private Sut<T> _sut; public string Expectation { get; set; } public Tester() { _sut=new Sut<T>(); } public void TestReverse() { Assert.AreEqual(Expectation,_sut.ReverseName()); } } }