NUnit Cookbook (.NET unit test tool)

zhaozj2021-02-16  54

NUnit cookbook

This article tells how to write and organize test code with NUnit.

Simple Test Case

Steps to write test code:

1. Create an instance of TestCase;

2. Override method runtest ();

3. To check a value, call Assert.

For example, the test code for testing "two Money objects and objects that contains the sum of them" is as follows:

Public void testsimpleadd () {

Money M12CHF = New Money (12, "CHF");

Money M14CHF = New Money (14, "CHF");

Money expertted = new Money (26, "chf");

Money Result = M12CHF.Add (M14CHF);

Assert (Expected.equals (Result);

}

If the test to write is very similar to the previous test, then write a fixture instead. If you want to run more things in a test, create a Suite.

FixTure

What should we do if there are two or more tests that do the same or similar objects? Testing acts on a known object set, which is called fixture. Generally establishing this fixture is much more time than the actual test.

When there is a public fixture, we can do this:

1. Create a subclass of TestCase.

2. Add an instance variable for each part of FixTure.

3. Override setup () initializes these variables.

4. Override Teardown () Releases any permanent resources allocated in Setup.

For example, when writing TestCases for working with 12 SWISS FRANCS, 14 SWISS FRANCS and 28 US DOLLARS, first create a fixture:

Public Class Moneytest: Testcase {

PRIVATE MONEY F12CHF;

PRIVATE MONEY F14CHF;

PRIVATE MONEY F28USD;

Protected Override Void Setup () {

F12CHF = New Money (12, "CHF");

F14CHF = New Money (14, "CHF");

F28usd = new Money (28, "USD");

}

}

Once FixTure is ready, you can write any more test cases.

TEST CASE

How do I write and call a separate Test Case? Writing an unspeakable fixture test case is very simple - just override runtest in one subclass of Testcase. For a FixTure writing test case, the same method is the same method, and the subclass of TestCase is manufactured for SET UP code, and then different subclasses are manufactured for each independent test case. However, soon, you will notice that most of these code become victims of grammar.

NUNIT provides a concise method for writing a FixTure-based test:

1. Write a PUBLIC test method in the FixTure class. Here, you must make sure this method is public, otherwise it will not be called via the Reflection.

2. Create an instance of this TestCase and pass the name of this test method to its constructor. For example, test a Money object and another MONEYBAG:

Public void testmoneyMoneybag () {

// [12 CHF] [14 CHF] [28 USD] == {[26 CHF] [28 USD]}

Money Bag [] = {New Money (26, "CHF"), New Money (28, "USD")}

Moneybag expertted = new Moneybag (BAG);

Assertequals (expected, f12chf.add (f28usd.add (f14chf)));

}

Create an instance of Moneytest:

New Moneytest ("TestmoneyMoneybag")

When this test runs, the test will find the name of the test method and call it. Once there are multiple tests, you can organize them into a Suite.

Suite

How to do multiple tests at a time? NUnit offers TestSuite that can run any number of tests at a time. For example, run a separate test case:

TestResult result = (New Moneytest ("TestmoneyMoneybag")). Run ();

Create a Suite with two test cases and run them once:

Testsuite suite = new testsuite ();

Suite.Addtest (New Moneytest ("TestMoneyEquals");

Suite.Addtest (New Moneytest ("TestsImpleAdd"));

TestResult result = suite.run ();

Another way is to let NUNIT suck a Suite from a TestCase, just pass your TestCase to the constructor of TestSuite:

Testsuite Suite = New Testsuite (TypeOf (MoneyTest));

TestResult result = suite.run ();

When creating only Suite of a test example class, uses a manual method. Otherwise, the automatic method is used, which avoids changing the creation code of Suite when each new test case is added.

TestSuites is not only included with TestCase classes. It can include any object that implements itest. For example, you can create a TestSuite in your code, and you can also do this in my code. Then we can create a TestSuite that contains them to run them once:

Testsuite suite = new testsuite ();

Suite.addtest (kent.suite ());

Suite.addtest (ERICH.Suite ());

TestResult result = suite.run ();

Testrunner

How to run these tests and collect results? NUnit provides tools for defining suite runs and display results. To make your Suite can be accessed for Testrunner, you must define a Static Property Suite that returns the test Suite.

For example, add the following code in MoneyTest to make MoneyTest Suite for Testrunner:

Public Static ITEST Suite {

Get {

Testsuite Suite = new testsuite (); suite.addtest (New Moneytest ("TestMoneyEquals");

Suite.Addtest (New Moneytest ("TestsImpleAdd"));

Return suite;

}

}

If a TestCase class does not define a Suite method, Testrunner will automatically generate Suite that fills with all methods and adds prefix "Test" before these method names. NUnit offers two Testrunner: GUI and character versions.

The GUI window contains: Fill in the text box that contains the DLL or EXE names of the test class; fill in the class name of the Suite property in DLL or EXE. As shown below:

In the dynamic programming environment of the RELOAD, you don't need a NUnit window. In other environments, you have to restart the GUI version for each run. This is a very boring and time-consuming job, which will be improved in future versions.

Similarly, there is a NUNIT batch interface. In order to use the NUNIT of the command line, you can type NUNITCONSOLLE to follow the assembly of the classes that contain the Suite property after the system prompt. For example, use the batch Testrunner test Moneytest:

C: / NUNITCONSOLE NUNIT.SAMPLES.MONEY.MONEYTEST, NUNITSMPLES.DLL

The batch interface will output the result in the text. Another optional method is to call the batch interface in the MAIN method of your TestCase class. as follows:

Public static void

Main

(String [] args) {

NUNIT.TEXTUI.TESTRUNNER.RUN (Suite);

}

This way, just type MoneyTest on the system prompt, you can run the test.

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

New Post(0)