Pass lambda to parameterized NUnit test
Asked Answered
D

2

16

I have a class with a bunch of overloaded operators:

public static double[,] operator +(Matrix matrix, double[,] array)
public static double[,] operator -(Matrix matrix, double[,] array)
public static double[,] operator *(Matrix matrix, double[,] array)

For all of them I'd like to test operands for null. I have an NUnit test for that:

public void MatrixOperatorOperandIsNullThrows(Func<Matrix, double[,], double[,]> op)
{
    Matrix m = null;
    var right = new double[,] {{1, 1}, {1, 1}};
    Assert.Throws<ArgumentException>(() => op(m, right));
}

How can I pass a lambda for each operator like (l,r) => l + r ?

Duplicate answered 30/5, 2012 at 19:33 Comment(0)
H
1

You can pass exactly that:

MatrixOperatorOperandIsNullThrows((l,r) => l + r);
Hadrian answered 30/5, 2012 at 19:36 Comment(2)
Where I'd better to call that? In constructor of TextFixture? I also use a Resharper to run tests. Can it handle them this way?Duplicate
@AndreyErmakov: In a separate test method per operator.Hadrian
M
32

You cannot immediately apply the TestCase attribute containing a lambda expression, i.e. the following test would be invalid:

[TestCase((a, b) => a + b)]
public void WillNotCompileTest(Func<double, double, double> func)
{
    Assert.GreaterOrEqual(func(1.0, 1.0), 1.0);
}

What you can do, however, is to use the TestCaseSource attribute together with an IEnumerable of your lambda expressions, like this:

[TestFixture]
public class TestClass
{
    private IEnumerable<Func<double, double, double>> testCases
    {
        get
        {
            yield return (a, b) => a + b;
            yield return (a, b) => a * b;
            yield return (a, b) => a / b;
        }
    }

    [TestCaseSource(nameof(testCases))]
    public void Test(Func<double, double, double> func)
    {
        Assert.GreaterOrEqual(func(1.0, 1.0), 1.0);
    }
}
Mycah answered 30/5, 2012 at 20:38 Comment(4)
Thanks for this quite useful approach. I chose what @SLaks provided because it also helped me to group tests for each operator.Duplicate
What if I want to pass other arguments, not just a function?Assize
You can create a class which property is a Func<double, double, double> then create other properties which are expectations. Finally you can pass an IEnumerable of your new class.Admonish
From what I can observe you need to make the testCases enumerable static. Until I did that I got an error: "The sourceName specified on a TestCaseSourceAttribute must refer to a static field, property or method."Delphina
H
1

You can pass exactly that:

MatrixOperatorOperandIsNullThrows((l,r) => l + r);
Hadrian answered 30/5, 2012 at 19:36 Comment(2)
Where I'd better to call that? In constructor of TextFixture? I also use a Resharper to run tests. Can it handle them this way?Duplicate
@AndreyErmakov: In a separate test method per operator.Hadrian

© 2022 - 2024 — McMap. All rights reserved.