Why will we use the Theory attribute?

I found the attributes [Theory] and [Datapoint] in NUnit. I'm not very sure how to use them. I think they can be used to test data, and it interested me. There are not many resources at this level. Can someone explain to me how to use them or point me to resources? Thanks.

+7
unit-testing nunit
source share
2 answers

Have you looked at NUnit online docs? Here is an example that shows you how you can use the [Theory] and [Datapoint] attributes:

http://www.nunit.org/index.php?p=theory&r=2.5.3

+1
source share

A theory is a special type of test used to test a general statement about a system under development. Conventional tests are based on examples. That is, the developer provides one or more examples of inputs and expected results either within the test code or, in the case of parameterized tests, as arguments to the test method. Theory, on the other hand, makes a general statement that all its statements will be valid for all arguments that satisfy certain assumptions.

Theories are implemented in NUnit as methods inside TestFixture , which are annotated using TheoryAttribute ( [Theory] ). theory methods should always have arguments and, therefore, parameterized tests at first glance. However, Theory includes additional data sources for its arguments and processing for assumptions about this data. The key difference, however, is that theories make general statements and are more than just a collection of examples.

Data for theories

The main data source for the theory is the datapoint or datapoints attribute. NUnit will use any fields of the required types that are annotated with one of these attributes to provide data for each Theory Parameter. NUnit collects values ​​for individual arguments combinatorially to provide test cases for theory.

In addition to the Datapoint and Datapoints attributes, it is possible to use any of the approaches for providing data that are normal parameterized tests. We suggest that this feature should not be overused, as it contradicts the distinction between a test based on examples and theory. However, this can be useful to ensure that a specific test case is included.

Assumptions

The theory itself is responsible for ensuring that all the data provided is consistent with its assumptions. This is done using the Assume.That(...) construct, which works the same as Assert.That(...) but does not cause a failure. If the assumption is not satisfied for a particular test case, this case returns the result without exception, and not success or Failure.

The overall result of performing the theory on a set of test cases is determined as follows:

If the assumptions are violated for all test cases, then Theory itself is marked as a failure. If any statement fails, Theory itself fails. If at least some cases convey these assumptions, and there are no rejection statements or exceptions, then Theory passes.

Example:

In the following example, the SquareRootDefinition theory verifies that the square root implementation satisfies the following definition:

"Given a non-negative number, the square root of this number is always non-negative and, when multiplied by itself, gives the original number."

 public class SqrtTests { [Datapoint] public double zero = 0; [Datapoint] public double positive = 1; [Datapoint] public double negative = -1; [Datapoint] public double max = double.MaxValue; [Datapoint] public double infinity = double.PositiveInfinity; [Theory] public void SquareRootDefinition(double num) { Assume.That(num >= 0.0 && num < double.MaxValue); double sqrt = Math.Sqrt(num); Assert.That(sqrt >= 0.0); Assert.That(sqrt * sqrt, Is.EqualTo(num).Within(0.000001)); } } 

Taken from

0
source share

All Articles