0

I need to do something like this:

[TestCase(1, 0, TestName = "Small values")]
[TestCase(int.MaxValue, 0, TestName = "Big precision value")]
[TestCase(int.MaxValue, int.MaxValue - 1, TestName = "Big precision and scale values")]
public void Constructor_ThrowNoExceptions_OnCorrectArguments(int precision, int scale,
   [Values] bool onlyPositive)
{
   Action action = () => new NumberValidator(precision, scale, onlyPositive);
   action.Should().NotThrow();
}

I want to have TestName and two values from testcase and run them for values onlyPositive: true and false.

I have no idea how to do this because code above doesn't work.

I get Method requires 3 arguments but TestCaseAttribute only supplied 2 System.Reflection.TargetParameterCountException.

If I put true or false in TestCase, ValuesAttribute just ignored and stops working.

Jonas Nyrup
  • 2,376
  • 18
  • 25
Defalt
  • 1

1 Answers1

0

When you apply a ValuesAttribute on a paramenter, you must apply one on every parameter.
From the documenation:

The ValuesAttribute is used to specify a set of values to be provided for an individual parameter of a parameterized test method. Since NUnit combines the data provided for each parameter into a set of test cases, data must be provided for all parameters if it is provided for any of them.


What you want can be achieved using a TestCaseSourceAttribute.
That one allows to pass arguments in order to make variations of the test cases being returned.

The 3 number combination test cases will be run 2 times; one time with onlyPositive true, a second time with false.

[Test]
[TestCaseSource(nameof(GetTestCases), new object[] { true })]
[TestCaseSource(nameof(GetTestCases), new object[] { false })]
public void ConstructorThrowNoExceptionsOnCorrectArguments(
    int precision, int scale, bool onlyPositive
    )
{
    Action action = () => new NumberValidator(precision, scale, onlyPositive);
    action.Should().NotThrow();
}

public static IEnumerable<TestCaseData> GetTestCases(bool onlyPositive)
{
    yield return new TestCaseData(1, 0, onlyPositive)
        .SetName("Small values");

    yield return new TestCaseData(int.MaxValue, 0, onlyPositive)
        .SetName("Big precision value");

    yield return new TestCaseData(int.MaxValue, int.MaxValue - 1, onlyPositive)
        .SetName("Big precision and scale values"); 
}
pfx
  • 20,323
  • 43
  • 37
  • 57