14.1 Cyclomatic complexity of GCD
If possible, find test cases that allow you to test each independent path of the gcd function.
Independent paths are defined here in course material.
How many test cases do you actually need to cover all source lines of the gcd function?
The cyclomatic complexity of the gcd function is four.
In general, the cyclomatic complexity is an upper limit of the number of necessary test cases. In the gcd function, it is possible to cover all source lines with two test cases.
Notice in gcd that the control of the branches of the two if control structures are coupled. This is actually poor programming! This observation may imply that it is impossible to find input data that exercises all independent paths.
14.2 Install Nunit
More specifically, for Windows users, goto the NUnit download page and download the most recent stable version of NUNIT for .NET 2.0. As of February 2010 this is the NUnit-18.104.22.16845.msi file (a Windows installer file). You can also get this file directly from here.
Mono users on Linux probably already have an installation of NUnit. According to the NUnit documentation, Mono 1.0 through Mono 1.9 include NUnit 2.2. Try calling the NUnit console runner named nunit-console from your shell. Please be aware of the version you are running. The newest stable version of NUnit is 2.5.3 (as of February 2010). Mono users are recommended to use version 2.4.8, however. See also the release notes.
14.3 Give Nunit a Try
This exercise is guided tour in using NUnit (version 2.5.3) together with Visual C# 2008 Express on Windows. The exercise will help set up the BankAccount test, which we have seen on an earlier slide page in this material. If you use C# via another IDE, or on a non-Windows platform, you should not follow this guide.
We will assume that you already have installed Nunit and that you use Visual C# 2008 Express.
Start Visual C# 2008 Express. Use File > New Project... and make a Class Library project.
In the Solution Explorer (usually at the far right of you screen) right click References, and do Add Reference.... Select and add nunit.framework (version 2.5.3) from the list. (The list may be long, so be careful to select the right entry).
Use Project > Add Class... to add the BankAccount class. (Do copy and paste from the BankAccount class of this material).
Paste the BankAccountTest class instead of Class1, which was made automatically by Visual C# 2008 Express.
Now build your library application: Use F6 as usual. "Build succeeded" is expected.
Do File > Save All. Notice the Location of you project. On a piece of paper (or in a text editor) notice the full file path to your Visual C# 2008 project. You can also get information about your project locations via the C# 2008 Express menu entry Tools > Options..., 'Projects and Solutions'.
Start NUnit 2.5.3 - for instance via the Icon on your desktop or via the Windows start menu.
In NUnit use Tools -> Settings... and check the 'Enable Visual Studio Support' in the window which appears. You find the check box in IDE Support > Visual Studio. (This is most likely the default setting in NUNIT 2.5.3).
In NUnit use File > Open Project, and select the dll named after your project. The file is most likely located in the Bin/Debug branch of your project directory (as you should have noticed above).
Activate the Run button in NUnit. You should see 'green light'.
Introduce an run-time error in BankAccount class. Is the error revealed in unit test?
If you managed to get to here you should be able to use NUnit on your own stuff. Congratulations.
14.4 Test of class Set
In continuation of class SetTest, perform unit tests of the intersection, union, and set difference operations.
The natural starting point is your solution to the previous exercise. You can also chose to test my solution.
14.5 Unit test of struct Interval
You are given a version of struct Interval, which we have worked with in an earlier exercise. You are also given a partial test suite of struct Interval. In the given Interval test suite all tests should be successful. (You may have to adjust the full qualified names of exceptions in the ExpectedException attributes, however). Please check for yourself that all tests pass successfully.
Add a test of the indexer and a test of the OverlapWith method. Both are members of type Interval, as given above.
In the version of struct Interval from above there is at least one error in either the indexer or the OverlapWith method. You should be able to reveal the error from your tests!
Please correct the error when you have found it. Carry out a regression test.
There are several overloaded operators in struct Interval. I have only made a test of the + operator. If time allows, add some additional tests of the non-tested Interval operators.
Finally, discuss the expected coverage of my Interval test cases together with your additional Interval test cases.
There is an error in the OverlapWith method of struct Interval. The two cases other inside this and this inside other (see the trailing comment lines) have been switched.
The following version of Interval-test.cs reveals the error:
In the systematic tests shown above the test methods OverlapTest_This_ContainedIn_Other and OverlapTest_Other_ContainedIn_This both fail.
The above version of Interval-test.cs also shows the test of the indexer.
Here is the correct version of the method OverlapWith:
With this version of OverlapWith all test are executed successfully.
Generated: Monday February 7, 2011, 12:23:19