Browse DevX
Sign up for e-mail newsletters from DevX


Getting Started with Unit Testing : Page 4

Learn unit testing now and find the problems in your code before things get out of control.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Running the Tests
With the test assembly compiled, it is time to run the tests. NUnit offers two ways to run test assemblies: a windows GUI test runner and a console test runner.

NUnit GUI Test Runner
Most developers prefer to use the graphical GUI test runner to run their unit tests during day-to-day development. It provides a graphical representation of your tests, as well as verbose error information in the case of failing tests.

After you installed NUnit, you should have an NUnit GUI 2.2 icon on your desktop. Double-click the icon to open up the NUnit GUI. When the GUI loads up, from the File menu choose Open (or just press Ctrl-O). This opens a dialog box. Navigate to where your test assembly was compiled. Double-click on the SimpleUnitTest.dll to load the assembly into NUnit.

Your screen should look something like Figure 2.

Figure 2: Looking at SimpleUnitTest from within the NUnit GUI.
On the left side of the NUnit GUI, you should see a tree hierarchy of your test assembly. In the top-level element you should see the path to the physical test assembly. For each sub-element, NUnit will display each class that contains unit tests, which NUnit denotes by the TestFixture attribute in the source code. Underneath each class, every test method, denoted by the Test attribute in the source code, will be displayed. Next to each test method, a small circle represents the result of the test method after that method has run. A red circle means a failed test, a green circle means a successful test, and a yellow circle represents an ignored test.

On the right side of the NUnit GUI, you should note the following three sections. The top contains two buttons that allow you to start and stop tests. You'll also see a status bar that displays the status of all the tests in the assembly. If any test fails, the status bar will turn red.

The middle section is where any output from running the unit tests will appear. Each of the four tabs displays a different piece of information. The tab of the most interest is the one visible by default: Errors and Failures. This is where any error messages that are returned from the test methods are displayed.

You'll use the last section in conjunction with the Errors and Failures tab. It is where a detailed stack trace for any error selected will be displayed.

Finally, the status bar at the bottom of the screen displays the number of test fixtures as well as the number of tests run. Also located in the status bar after a test run is the number of failures as well as the amount of time it took to run the tests.

The NUnit GUI contains several more advanced aspects including multiple configurations, projects, and categories. However, those topics are beyond the scope of this article.

NUnit Console Test Runner
You can also use the NUnit console test runner to run tests. To use the console test runner, make sure the Bin directory of the NUnit installation is in your path. Then open a command prompt and change to the directory that contains your compiled test assembly. Run your tests by typing the following command:

nunit-console SimpleUnitTest.dll

Listing 1 shows what your output should look like.

As you can see, NUnit generates a large amount of output, even for just one test. In the output you can see the same statistics that the GUI runner reported, including the number of failures and the total amount of time it took to run the tests.

The console runner includes several features that make it optimal for running automated tests from build scripts, such as generating detailed test reports and silent mode. However, because of the highly visible nature of the NUnit GUI runner, I will use it for the remainder of the article.

Thanks for your registration, follow us on our social networks to keep up-to-date