Wiki

Clone wiki

Geometry Class Library / Unit Test Standards

Unit Test Standards

NUnit

We use NUnit as our test framework. Ideally you should be using an IDE or setup that supports running NUnit tests.

Fluent Assertions

We also use Fluent Assertions, which not only allows for easy switching of test frameworks if necessary, but also simple, readable assertions, like this:

#!c#

equilateralTriangle.IsRegularPolygon().Should().BeTrue();

Structure

Test Setup and NUnit Markers

Each unit class has its own test class. Note the general header for a unit test file:

#!c#

namespace GeometryLibraryTests
{
    [TestFixture()]
    public class PolygonTests
    {
        // ...
    }
}
Note the NUnit marker [TestFixture()] That identifies this as a group of related tests.

Each constructor and public method in the class should have a corresponding method testing it in its test class. The naming convention for the method performing the testing is <Unit Class Name>_<Method Name or "Constructors">. For example:

#!c#

[Test()]
public void Plane_Constructor()
{
    // ...
}
Once again note the NUnit marker [Test()] that identifies this as a unit test.

Test Contents

The unit tests should test the full functionality of the method as much as possible. Comprehensive testing prevents problems down the road. The unit test should attempt to delineate the different cases and outcomes possible from the method, and check for each of these. For example:

#!c#

[Test()]
public void Polygon_IsRegularPolygonTest()
{
    List<LineSegment> squareLineSegments = new List<LineSegment>();
    squareLineSegments.Add(new LineSegment(PointGenerator.MakePointWithInches(0, 0, 0), PointGenerator.MakePointWithInches(4, 0, 0)));
    squareLineSegments.Add(new LineSegment(PointGenerator.MakePointWithInches(4, 0, 0), PointGenerator.MakePointWithInches(4, 4, 0)));
    squareLineSegments.Add(new LineSegment(PointGenerator.MakePointWithInches(4, 4, 0), PointGenerator.MakePointWithInches(0, 4, 0)));
    squareLineSegments.Add(new LineSegment(PointGenerator.MakePointWithInches(0, 4, 0), PointGenerator.MakePointWithInches(0, 0, 0)));
    Polygon squarePolygon = new Polygon(squareLineSegments);
    squarePolygon.IsRegularPolygon().Should().BeTrue();

    Polygon equilateralTriangle = Polygon.EquilateralTriangle(new Distance(DistanceType.Inch, 3));
    equilateralTriangle.IsRegularPolygon().Should().BeTrue();
}

In some cases we are testing if an exception is thrown at the proper time. In this case, an exception indicates the test passed. Declare this in NUnit like this:

#!c#

[Test()]
[ExpectedException(typeof(<Exception Name>))]
public void Polygon_ToString()
{
    // ...
}

Updated