March 2, 2017

NUnit Parameterised Tests

NUnit can have class level parameterised tests:


[ TestFixture( MachineEnum.None ) ]
[ TestFixture( MachineEnum.Lathe ) ]
[ TestFixture( MachineEnum.Miller ) ]
public class SomeTests
{
  private readonly MachineEnum _machineEnum = MachineEnum.None;

  public SomeTests(
  MachineEnum machineEnum )
  {
    _machineEnum = machineEnum;
  }
  ...
}

Also the tests can be paramterised at the test level. Make sure the TestCase parameter match the order and type of the test method.

  [Test]
  [TestCase("some value", null,         ExpectedResultEnum.Paramater1Used)]
  [TestCase("some value", "over ride",  ExpectedResultEnum.Paramater2Used)]
  [TestCase("some value", "over ride",  ExpectedResultEnum.Paramater1Used)]
  [TestCase(null,         null,         ExpectedResultEnum.Paramater1Used)]
  public async Task ParamterisedTest(
    string param1,
    string param2,
    ExpectedResultEnum expectedResult)
{
    // Arrange

    // Act

    // Assert
    Assert.Multiple(() =>
    {
      Assert.That(var1 == null, Is.False);
      Assert.That(var2.Id, Is.EqualTo(expectedResult));
    });
    ...
}

No comments: