hubesal
hubesal

Reputation: 179

NUnit creates more test instances than parameters added

I have a problem with what seems like NUnit creating more test instances than parameters added.

SortingState enum:

public enum SortingState
{
    None,
    Ascending,
    Descending,
}

test code:

    [Theory]
    [TestCase(SortingState.Ascending)]
    [TestCase(SortingState.Descending)]
    public void SortReportCodeList(SortingState sortingState)
    {}

According to the code above, there should be 2 test cases created for SortReportCodeList, but there's 3 of them instead, with the 3rd one using None enum value. enter image description here

I tried run tests once again, rebuild solution, restart VS, run the tests from Windows console, all of these with no effect. What would be the cause? Is it NUnit related, or maybe I should look for a problem in VS?

Upvotes: 0

Views: 118

Answers (1)

Charlie
Charlie

Reputation: 13746

You have made this test method an NUnit Theory and I'm wondering if that's what you actually intend.

If you remove [Theory] you will get only the two cases you have specified. Theories look for their test cases in various places and may even invent some cases for you, so don't use them unless that's what you want.

For example, you might have a declaration like this in your class...

SortingState NoSortingAtAll = SortingState.None;

If you did, the Theory would use that as an example. Depending on the NUnit framework version you are using, it may also pick up any array of SortingStates.

If you are not filtering test cases using Assume.That, you probably didn't want a Theory in the first place. Theories should only be used when you actually have a "theory" to prove, i.e. an hypothesis.

Here's an example of a "theory", which might be implemented using [Theory]:

"Given a non-negative real number, the square root of that number multiplied by itself gives the original number."

To implement, you could use random numbers or let Theory find real numbers to use. You would then use Assume.That to eliminate negative numbers and implement the test itself as an Assert.

However, I strongly suspect you don't want a Theory at all here, so try removing the attribute. :-)

Upvotes: 1

Related Questions