Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Manning.The.Art.of.Unit.Testing.with.Examples.in.dot.NET.Jun.2009.pdf
Скачиваний:
18
Добавлен:
21.03.2016
Размер:
9.67 Mб
Скачать

26

CHAPTER 2

A first unit test

In this book, we’ll write tests that verify LogAn’s parsing, event-recog- nition, and notification abilities. Before we get started testing our project, though, we’ll look at how to write a unit test with NUnit. The first step is installing it.

2.3 First steps with NUnit

As with any new tool, you’ll need to install it first. Because NUnit is open source and freely downloadable, this task should be rather simple. Then we’ll see how to start writing a test with NUnit, use the various built-in attributes that NUnit ships with, and run our test and get some real results.

2.3.1Installing NUnit

You can download NUnit from www.NUnit.org or www.NUnit.com. NUnit is free to use and is an open source product, so you can get the source code for NUnit, compile it yourself, and use the source freely within the limits of the open source license. (See the license.txt file in the program directory for license details.)

NOTE At the time of writing, the latest version of NUnit is 2.2.8. The examples in this book should be compatible with most future versions of the framework.

To install NUnit, run the setup program you downloaded. The installer will place a shortcut to the GUI part of the NUnit runner on your desktop, but the main program files should reside in a directory named something like c:\Program Files\NUnit-Net-2.0 2.2.8. If you doubleclick the NUnit desktop icon, you’ll see the unit test runner shown in figure 2.2.

We’ll be using this GUI to run our tests shortly.

2.3.2Loading up the solution

If you have the book’s code on your machine, load up the ArtOfUnitTesting.sln solution from the Code folder inside Visual Studio 2008.

First steps with NUnit

27

Figure 2.2 The NUnit GUI is divided into three main parts: the tree listing the tests on the left, messages and errors at the top right, and stack trace information at the bottom right.

NOTE The C# Express Edition of Visual Studio 2008 (or above) is fine for use with this book.

We’ll begin by testing the following simple class with one method (the unit we’re testing) inside it:

public class LogAnalyzer

{

public bool IsValidLogFileName(string fileName)

{

if(!fileName.EndsWith(".SLF"))

{

return false;

}

28

CHAPTER 2

A first unit test

return true;

}

}

This method may not seem complicated, but we’ll test it to make sure it works. In the real world, you’ll want to test any method that contains logic, even if it seems to be simple. Logic can fail, and we want to know when it does. In the following chapters, we’ll test more complicated scenarios and logic.

The method looks at the file extension to determine whether a file is a valid log file or not. Our first test will be to send in a valid filename, and make sure the method returns true.

Here are the first steps for writing an automated test for the IsValid-

LogFileName method:

1Add a new class library project to the solution, which will contain your test classes.

2To that library, add a new class that will hold your test methods.

3Add a new method to the preceding test case named IsValid-

LogFileName.

We’ll touch more on test-naming and arrangement standards later in the book, but the basic rules are listed in table 2.2.

Table 2.2 Basic rules for placing and naming tests

Object to be tested

Object to create on the testing side

 

 

Project

Create a test project named [ProjectUnderTest].Tests.

 

 

Class

For each class, create at least one class with the name [ClassName]Tests.

 

 

Method

For each method, create at least one test method with the following name:

 

[MethodName]_[StateUnderTest]_[ExpectedBehavior].

 

 

First steps with NUnit

29

For example, the name for our LogAn test project would be AOUT.Logan.Tests (with AOUT standing for Art of Unit Testing). The name for the LogAnalyzer test class would be LogAnalyzerTests.

Here are the three parts of the test method name:

MethodName—The name of the method you’re testing

StateUnderTest—The conditions used to produce the expected

behavior

ExpectedBehavior—What you expect the tested method to do under the specified conditions

In our test of the IsValidLogFileName method, the state or condition is that we’re sending the method a valid filename, and the expected behavior is that the method will return a true value. Our test method

name might be IsValidFileName_validFile_ReturnsTrue().

We haven’t used the NUnit test framework yet, but we’re close. We still need to add a reference to the project under test for the new testing project. Do this by right-clicking on the test project and selecting Add Reference. Then select the Projects tab and select the LogAn project.

The next thing to learn is how to mark the test method to be loaded and run by NUnit automatically.

2.3.3Using the NUnit attributes in your code

NUnit uses an attribute scheme to recognize and load tests. Just like bookmarks in a book, these attributes help the framework identify the important parts in the assembly that it loads, and which parts are tests that need to be invoked.

NUnit provides an assembly that contains these special attributes. You just need to add a reference in your test project (not in your production code!) to the NUnit.Framework assembly. You can find it under the

.NET tab in the Add Reference dialog box. Type Nunit and you’ll see several assemblies starting with that name; add nunit.framework.

NUnit needs at least two attributes to know what to run:

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]