dimanche 4 septembre 2016

What's the difference between [Test] (optional [Values]) versus [TestCase] (optional ExpectedResult)?

The actual question is at the bottom. I've tried to make the post as short as possible already and I hope that I've managed to straighten most of the unclear parts of the issue.

According to the docs, we have in nUnit (among other attributes)

I've been reading in the docs and using all all the above since long time so I'm well familiar with how they behave. However, one thought has always bugged me and by now, I'm embarrassed to ask.

public int Output(int input) { return input + 1; } 

The way we could set up test for a simple method like the one above, I can think of four approaches. I've seen most of them pretty much everywhere but I can't really tell if those are different flavors leading to the same under-the-hood functionality or if there's a technical difference that I'm simply not aware of.

For instance - perhaps #2 can be parallellized whereas the other can't (it's just a dummy example to show that there might be magic that we're not aware of).

Test attribute only

[Test]
public void OutputIsWorking()
{
  List<int> inputs = new List<int>{ 1, 2, 3 };
  for(int i = 0; i < inputs.Count; i++)
    Assert.That(inputs[i] + 1, Is.EqualTo(Output(input)));
}

Test and Values attributes combined

[Test]
public void OutputIsGreat([Values(1,2,3)] int input)
{
  int output = Output(input);
  Assert.That(input + 1, Is.EqualTo(output));
}

TestCase attribute only

[TestCase(1, 2)]
[TestCase(2, 3)]
[TestCase(3, 4)]
public void DivideTest(int input, int expectation)
{
  int output = Output(input);
  Assert.That(output, Is.EqualTo(expectation));
}

TestCase and ExpectedResult attributes combined

[TestCase(1, ExpectedResult = 2)]
[TestCase(2, ExpectedResult = 3)]
[TestCase(3, ExpectedResult = 4)]
public void DivideTest(int input)
{
  return Output(input);
}

So, the question is if those approaches differ on a technical level or if it's just whatever the code is most fond of at the moment. We prefer to lower the number of choices a developer needs to make and providing such a wide range of equivalents seems redundant and confusing.

If they differ, then I'd like to know how. I haven't found that in the docs. If they don't differ, then I'm curious why. Are we talking about "let people decide what they like" or rather "this is a legacy thing that can't be removed"?

Aucun commentaire:

Enregistrer un commentaire