Home > Software design >  How to add objects to unit test?
How to add objects to unit test?

Time:02-25

I have a class called Matrix2D which essentially holds a 2-dimensional integer array. Then I have another class that multiplies 2 matrices. How can I put a unit test together for testing the multiplication function?

[TestClass]
public class UnitTest
{
    [TestMethod]
    [DataRow(new Matrix2D(new int[,] { { 2, 1, 3 }, { 1, 3, 2 } }), new Matrix2D(new int[,] { { 35, 30 }, { 50, 40 }, { 70, 75 } }), new Matrix2D(new int[,] { { 330, 325 }, { 325, 300 } }))]
    public void TestMethod(Matrix2D a, Matrix2D b, Matrix2D c)
    {
        var program = new MatrixMultiplier();
        
        Assert.AreEqual(c, MatrixMultiplier.Multiplies(a, b));
    }
}

It complains about line 5 regardless of what data I put in. I also tried passing in a regular 2d integer array and creating those matrices inside the function, but still keeps complaining. Couldn't find a solution anywhere, so thank you in advance.

CodePudding user response:

If you have the option to use NUnit, you can define a TestCaseSource attribute that could be either a static object[] where each element represents the parameters of a test case, or you could also use an enumerator (A method returning IEnumerable<object[]>).

To use NUnit you have to download the NUnit and the NUnit3TestAdapter NuGet packages.

using NUnit.Framework;

namespace UnitTestProject1
{
    [TestFixture]
    public class UnitTest
    {
        [TestCaseSource(nameof(MatrixSource))]
        public void TestMethod(Matrix2D a, Matrix2D b, Matrix2D c)
        {
            // Arrange
            var program = new MatrixMultiplier();

            // Act
            var result = program.Multiplies(a, b);

            // Assert
            Assert.Equals(c, result);
        }



        static object[] MatrixSource =
        {
            new object[]
            {
                new Matrix2D(new int[,] { { 2, 1, 3 }, { 1, 3, 2 } }),
                new Matrix2D(new int[,] { { 35, 30 }, { 50, 40 }, { 70, 75 } }),
                new Matrix2D(new int[,] { { 330, 325 }, { 325, 300 } })
            }
        };
    } 
}

CodePudding user response:

You can do it using the DynamicData attribute:

[TestClass]
public class UnitTest
{
    public static IEnumerable<object[]> TestData()
    {
        yield return new object[]
        {
                new Matrix2D(new int[,] {{2, 1, 3}, {1, 3, 2}}),
                new Matrix2D(new int[,] {{35, 30}, {50, 40}, {70, 75}}),
                new Matrix2D(new int[,] {{330, 325}, {325, 300}})
        };
        // yield return additional test cases here if you want
    }


    [DataTestMethod]
    [DynamicData(nameof(TestData), DynamicDataSourceType.Method)]
    public void Test(Matrix2D a, Matrix2D b, Matrix2D c)
    {
        \\ test
    }
}

this blog entry has a more thorough description of DynamicData along with some warnings about using it. If you use NUnit or XUnit there are more robust features for doing this.

  • Related