Repeating a [TestCase] 100 times in NUnit

nunit, repeat, unit-testing, webtest

Solution

From the online NUnit documentation for Repeat, it would appear that this behaviour is deliberate.

Notes:

It is not currently possible to use RepeatAttribute on a TestFixture or any other type of test suite. Only single tests may be repeated. Since a parameterized test method represents a suite, RepeatAttribute is ignored when it appears on such a method.

With the assumption that `TestCase` is obviously regarded as a parameterized test. The loop looks inevitable, without some additional customization?

What you could do instead is use use a test case source and feed it an enumerable of 100 repeated items:

private static readonly IEnumerable<Tuple<string, string>> _oneHundredCases = 
  Enumerable.Range(0, 100)
   .Select(_ => Tuple.Create("arg1", "arg2"));

[TestCaseSource(nameof(_oneHundredCases))]
public void testing(Tuple<string, string> theArgs)
{
    ...
    AssertStuff(theArgs.Item1, theArgs.Item2);
}

But for the same price, you may as well vary the arguments a bit?, e.g.

_oneHundredCases = 
  Enumerable.Range(0, 100)
   .Select(n => Tuple.Create(n.ToString(), $"arg{n}"));

Problem

I'm converting a webtest that loops 100 times (fires the request and validates the response 100 times) to NUnit. It seems that the `[Repeat]` attribute only works for `[Test]`, not `[TestCase]` or any other decoration/attribute. What is the cleanest way to do this? I want to fire that one test case with those parameters 100 times, and I'd like to do it without adding a `loopCount` parameter to my test case and nesting my act and assert sections in a for loop. The below code only runs once. ``` [TestCase("arg1", "arg2"), Repeat(100)] public void testing(string arg1, string arg2) { //Arrange //Act var response = RequestSender.SendGetRequest(); //Assert AssertStuff(arg1, arg2); } ```

Original source