Use NUnit to make cell test in .NET programming [ZT]

xiaoxiao2021-03-06  64

From: http://www.microsoft.com/china/Community/column/59.mspx

Use NUnit to make cell testing in .NET programming

Lu Yan

introduction:

Events that may happen will be more close to the actual, but fortunately, there is a very common task in the programmer now:

You will go to work on the first day today. Your project manager gives you a stacked document, telling you that today's task is written in a .NET class in accordance with the requirements in the document, possibly because the task is not complicated, so he looks up Very random.

It is very special for you to complete the task today. After you have taken it, you will have a project introduction of the previous paragraph, because you know that those who are not important to you, the impression is a ticket system engineering. Soon, you have found the key points you need to pay attention to: Class demand documentation. You read it in detail, it feels not complicated, class Ticket, there is a read-only int type public property, the name is amount, there is two ways, one is the name is SELL, the function is to minus Amount It means that a ticket is sold, of course, the ticket can not be negative, if it is, throw an exception instruction reason. The other is Add, it has an int type parameter, the function is to add this value of this parameter to the Amount, maybe it means something such as the ticket, you are not too concerned, anyway, this program is very simple, you hide Live in the heart, turn on the computer, call up the editor, and start preparing to write the program.

"Hey, wait", the project manager doesn't know when it is back, "I want to know how you plan to test, I am most concerned about this."

"What is a unit test?" You turned his head and frustrated looking at disappointed project manager.

What is a unit test:

There will be many tests during the programming process, and the unit is only one of them. The unit test does not guarantee that the program is perfect, but in all the tests, the unit test is the first link, which is also the most important part. . Unit testing is a work that is tested by the programmer. Simply said that the unit test is whether the test code writer executes whether or not the expected result is created in terms of its way. There are many articles about the importance of unit testing, there are many in-depth analysis, and will not be described here.

NUNIT is an automation unit test framework for NET. Its role is to help you make cellular test work, like a famous JUnit, is a member of the XUnit family. Its download address is:

http://www.nunit.org.

Test first:

"What? Write test first?" You must be very surprised, right! That is to write test code, according to the theory of limit programming (XP), write testing is the process of designing software, and its importance even exceeds the actual completion function. First, write the test, then complete the code, so that all the test passes the date of the program is completed.

First, we will introduce NUnit.framework.dll files into the project and create a class named TicketTest:

[TestFixTure]

Public class tickettest

{

[TEST]

Public void add ()

{

Ticket ticket = new ticket ();

Ticket.Add (100);

Assertion.Assrtequals (100, Ticket.Amount);

}

Note that [TestFixTure] and [TEST] two attributes must be added to NUNIT, so that the test framework can know which classes or methods need to test.

We define a Ticket object in the Add method and add 100 tickets to him, then you can use:

Ask.ASSERTEQUALS (100, Ticket.Amount);

Test if the Amount property of Ticket is actually 100.

Next, let's add a method of testing Sell to TicketTest:

[TEST]

Public void sell ()

{

Ticket ticket = new ticket ();

Ticket.Add (100);

Ticket.sell ();

Ticket.sell ();

Ticket.sell ();

Assertion.Assertequals (97, Ticket.Amount);

}

Here, after we add 100 tickets, we sell 3 tickets, then look at whether we have 97 tickets.

Ok, these two methods have been finished, let's take a look at the results of the test, according to the requirements, we wrote the following code:

Public class ticket

{

PRIVATE INT AMOUNT;

Public Int Amount

{

get

{

Return Amount;

}

}

Public void add (int Num)

{

}

Public void sell ()

{

}

}

Note that this code is just to complete the structure of the class, and the implementation of the method is temporarily empty. Then compile this code into a DLL dynamic connection library file: UnitTest.dll.

We run NUNIT's graphics test tool, open our compiled DLL file, click "Run" to press New, you can see the following screen:

Very eye-catching, said that the test did not succeed, but this is among our expectations.

Next, we complete our Add method to the Ticket class:

Public void add (int Num)

{

Amount = NUM;

}

Save, recompile.

Switch to NUNIT, then click Run, you can see:

The Add method has become green, and then the SELL method is also completed:

Public void sell ()

{

Amount - = 1;

}

Test again, the result is turned:

Ah, finally become beautiful green, everyone now realizes the importance of environmental protection. :)

So can you make a task? Wait, don't worry, there is an exception that has not been tested. If our Amount is less than 0, it will produce an exception, then how is the abnormal test? Please look again.

Test is abnormal:

Still following the above, write the test code first:

[TEST]

[EXPECTEDEXCEPTION (TypeOf (Exception)]

Public void ExcPetationTesting ()

{

Ticket ticket = new ticket ();

Ticket.Add (3);

Ticket.sell ();

Ticket.sell ();

Ticket.sell ();

Ticket.sell ();

}

Among them, [ExcectionException] means that we hope to capture an exception that occurred. If no exception is captured, the test failed. The latter code is very understanding, we added three tickets, but sold four out, this is not a stock, there is no way to stay in the future. :)

Compile operation, we see the following test screens:

In the Ticket class, we modify the SELL method to turn it:

Public void sell ()

{

IF (AMOUNT - 1 <0)

Throw New Exception ("Amount cannot be 0");

Amount - = 1;

}

Compile, re-test, the results are as follows:

Ok, even if you complete our unit test, you have a basic understanding of how to test cell test in C #. In addition, NUnit is not only for C #, in fact, you can use NUnit to test your unit in any .NET language, the method is the same.

to sum up:

The unit test seems to be a bit trouble, but it provides programmers with a safe point of view, so that the programmer has more confidence in his own procedure, while reducing the time of frequent DEBUG for frequent DEBUG, also provides the application A safety protection net, therefore, unit testing is an important means of improving development efficiency and software quality.

With Unint, we can make cell testing during .NET programming, its graphical interface and simple and powerful test framework provide us with a very comfortable and interesting test environment, allowing programmers to feel units. The test is not boring, and it can even become a pleasure after habit.

After reading this article, if you are the poor programmer in the introduction, you will now easily face your project manager, hand over a peace of mind.

转载请注明原文地址:https://www.9cbs.com/read-84686.html

New Post(0)