Search code examples
c#.net.net-corenunitmoq

Unit Test with input parameter


How to call method as a parameter into TestCase attribute NUnit?

Like we can write: [TestCase(1, 2, 3)]

How to call a function like below?: [TestCase(SomeFunction(), 1, 2, 3)]


Solution

  • You can try TestCaseSource attribute and use static method for your use case.

        public static IEnumerable<EditModel> Generator()
        {
            // You can also call methods here
            yield return new EditModel();
        }
    
        [Test]
        [TestCaseSource(nameof(Generator))]
        public void DoSomething(EditModel model)
        {
            Console.WriteLine($"{model.commentText}");
        }