NUNIT (unit test)

xiaoxiao2021-03-06  142

NUnit

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. 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.

Suppose we are writing a bank app. First, we will introduce NUnit to NUNIT.FRAMEWORK.DLL files into the project and create a class named AccountTest, and the first class method we want to test is TransferFunds.

Namespace Bank

{

Using nunit.framework;

[TestFixTure]

Public Class AccountTest

{

[TEST]

Public void transferts ()

{

Account Source = new account ();

Source.Deposit (

200.00F

);

Account destination = new account ();

Destination.Deposit (

150.00F

);

Source.transferfunds (Destination,

100.00F

);

AskERT.Arequal

250.00F

Destination.balance);

AskERT.Arequal

100.00F

Source.balance);

}

}

}

First of all, this class is related to an [TestFixTure] feature (attribute) - which means that this class contains test code (this feature can be inherited). This class must be public, but his parent class is not limited. This class must also have a default constructor.

One of the only ways in the class - TRANSFERFUNDS, associated with [TEST] feature - this means it is a test method. The return value of the test method must be VOID and cannot have parameters. In our test method, we have initialized the object being tested, perform the method of test and check the status of the object. The Assert class defines a set of methods to check for a given condition. In our example, we use the areequal method to ensure that the two accounts after the transaction have the correct balance (this method has a lot of overload, we are in this example The version used with two parameters: The first parameter is our expected value, the second parameter is the actual value).

Account supports deposit, withdrawal, and fund transfer. This Account class looks like this:

Namespace Bank

{

Public Class Account

{

PRIVATE FLOAT Balance;

Public void Deposit (Float Amount)

{

Balance = Amount;}

Public void withdraw (Float Amount)

{

Balance- = Amount;

}

Public void Transferfunds (Account Destination, Float Amount)

{

}

Public Float Balance

{

Get {return balance;}

}

}

}

Compile and run this example. Suppose you have compiled your test code to Bank.dll. Open the Nuint GUI (the installer creates a shortcut in your desktop and "Programs" menu), open the GUI, select the file-> Open menu item, find your bank.dll and select in the "Open" dialog box it. After Bank.dll is loaded, you will see a test tree structure in the panel on the left, and a set of state panels on the right. Click the Run button, the status and test tree species TransferFunds nodes become red - our test failed. The "ERRORS AND FAILES" panel displays the following message - "Transferfunds: Expected <250> But Was <150>", in its piles tracking panel reports the location of the test failed in the code - "At Bank. AccountTest.Transferfunds () in C: /NUnit/banksampletests/accounttest.cs: line

17 "

This is the expected result because we have not implemented TransferFunds methods. Now let's get it. Don't close the GUI, go back to your IDE and modify the code, so that your transferts method looks like this:

Public void Transferfunds (Account Destination, Float Amount)

{

Destination.Deposit (Amount);

WITHDRAW (AMOUNT);

}

Now recompile your code and click the RUN button in the GUI - the status bar and the number node have turned green. (Note that the GUI will automatically reload the assembly for you; we can continue to work in the IDE and write more tests.

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

New Post(0)