Search code examples
c#unit-testingtestingnunit

Add Result to TestCaseSource


I have simple method which calculates a given calculation from a list. I would like to write some tests for this method.

I am using NUnit. I am using TestCaseSource because i am trying to give a list as parameter. I have the solution from this question. My tests looks like this:

[TestFixture]
    public class CalcViewModelTests : CalcViewModel
    {
        private static readonly object[] _data =
            {
                new object[] { new List<string> { "3", "+", "3" } },
                new object[] { new List<string> { "5", "+", "10" } }
            };

        [Test, TestCaseSource(nameof(_data))]
        public void Test(List<string> calculation)
        {
            var result = SolveCalculation(calculation);

            Assert.That(result, Is.EqualTo("6"));
        }
    }

I would like to test multiple calculations like with testCases.

TestCases have the Result parameter. How could I add a Result to TestCaseSource so i can test multiple calculations?


Solution

  • Looks like this should work:

    private static readonly object[] _data =
        {
            new object[] { new List<string> { "3", "+", "3" }, "6" },
            new object[] { new List<string> { "5", "+", "10" }, "15" }
        };
    
    [Test, TestCaseSource(nameof(_data))]
    public void Test(List<string> calculation, string expectedResult)
    {
        var result = SolveCalculation(calculation);
    
        Assert.That(result, Is.EqualTo(expectedResult));
    }