NUnit TestCase with Generics

后端 未结 9 830
梦毁少年i
梦毁少年i 2021-01-31 13:53

Is there any way to pass generic types using a TestCase to a test in NUnit?

This is what I would like to do but the syntax is not correct...

<         


        
相关标签:
9条回答
  • 2021-01-31 14:49

    As might be testing with generic functions that return objects?. Example:

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

    Thanks

    0 讨论(0)
  • 2021-01-31 14:50

    I slightly modified the TestCaseGenericAttribute somebody posted here:

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class GenericTestCaseAttribute : TestCaseAttribute, ITestBuilder
    {
        public GenericTestCaseAttribute(params object[] arguments)
            : base(arguments)
        {
        }
    
        IEnumerable<TestMethod> ITestBuilder.BuildFrom(IMethodInfo method, Test suite)
        {
            if (!method.IsGenericMethodDefinition) return base.BuildFrom(method, suite);
            var numberOfGenericArguments = method.GetGenericArguments().Length;
            var typeArguments = Arguments.Take(numberOfGenericArguments).OfType<Type>().ToArray();
    
            if (typeArguments.Length != numberOfGenericArguments)
            {
                var parms = new TestCaseParameters { RunState = RunState.NotRunnable };
                parms.Properties.Set("_SKIPREASON", $"Arguments should have {typeArguments} type elements");
                return new[] { new NUnitTestCaseBuilder().BuildTestMethod(method, suite, parms) };
            }
    
            var genMethod = method.MakeGenericMethod(typeArguments);
            return new TestCaseAttribute(Arguments.Skip(numberOfGenericArguments).ToArray()).BuildFrom(genMethod, suite);
        }
    }
    

    This version expects one list of all parameters, starting with the type parameters, starting with the type paramters. Usage:

        [Test]
        [GenericTestCase(typeof(IMailService), typeof(MailService))]
        [GenericTestCase(typeof(ILogger), typeof(Logger))]
        public void ValidateResolution<TQuery>(Type type)
        {
            // arrange
            var sut = new AutoFacMapper();
    
            // act
            sut.RegisterMappings();
            var container = sut.Build();
    
            // assert
            var item = sut.Container.Resolve<TQuery>();
            Assert.AreEqual(type, item.GetType());
        }
    
    0 讨论(0)
  • 2021-01-31 14:55

    You can make custom GenericTestCaseAttribute

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

    Here is implementation of 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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题