Using NUnit for UT with command line

Source: Internet
Author: User
Tags assert command line ultraedit xunit
Command line
XP is getting more and more into the eyes of programmers, TDD (Test drived design) is becoming more and more popular, UT (unit testing) is the first step of TDD, mainly for the front-line developers, rather than project managers, system design and analysis personnel even testers , of course, some UT methods can also be used for subsequent testing, but conceptually it is not a UT. UT is "a small piece of code written by a developer to verify that a small, well-defined function of the measured code is correct". The Xunit series is a dedicated user UT framework (tool) that contains a class library of UT on the target code. NUnit is a member of the Xunit series that is used to UT the code written for languages under the. NET framework, which are actually supported by the language nunit that conform to the CTS specification.

UT's main goal is to improve the robustness of the code and improve the production efficiency of the Code, NUnit is designed for this purpose, so for the use of Vs.net IDE development, Adding Nunit.framework.dll references can be done directly in the Project unit test, the reference details by the IDE itself to execute, for programmers is transparent. If you don't use a large IDE such as vs.net, such as coding using the editor +sdk+ command-line, or simply using Emacs, then a lot of things have to be done manually, and this process may be a bit boring, which is also impossible because the editor + The command line would have been less productive, but it was a good way to learn (and the way I liked it). Here, if the test code and the test code are written in the unused source file, perform an UT typical step as follows (take C # as an example and assume that two files are Target.cs and Test.cs respectively):

• Compile the tested code as a DLL or module:
Csc/t:library Target.cs or Csc/t:module Target.cs
• Compile test files as DLLs and add references to external library files Nunit.framework.dll and Target.dll:
Csc/t:library/r:c:\progra~1\nunit2~1.2\bin\nunit.framework.dll;target.dll Test.cs
Or add only a reference to the external library file Nunit.framework.dll and add the target module:
Csc/t:library/r:c:\progra~1\nunit2~1.2\bin\nunit.framework.dll/addmodule:target.netmodule Test.cs
· NUnit command line:
Nunit-console Test.dll
Or run the NUnit GUI to open Test.dll, test, and generate test reports.

This assumes that NUnit is installed in the C:\Program Files\nunit 2.2\ directory and that the file directory name in the 8.3 format is used on the command line because the CSC uses spaces to differentiate between different compilation parameters. Also, a reference to the Nunit.framework.dll is required, otherwise in the test Code, the declaration using Nunit.framework will report "error cs0246:the type or namespace name ' NUnit ' Coul D is found (Are you missing a using directive or a assembly reference?) " of errors.

This process is a bit cumbersome, especially if you have more code, you need to take these steps for each change to unit test. Writing this process as a batch, and then executing the bat file every time, can alleviate a bit, except that the file does not rewrite the batch at the same time.

Although writing different classes (tested classes and test classes) in a source file is a way of developing the program, but for personal learning to write the test class and test class in a file, and then use the editor (I use the UltraEdit) user command-line tools, can greatly simplify the above compilation link, Here's an example of UltraEdit: After the class is designed, all the tested classes and test classes are placed in the same file (for the code to be clear, you can insert a page break between the class code without having to affect compilation), and then click on the Menu "Advanced-〉 Toolbar Configuration" In the command line, enter:

Csc/t:library/r:c:\progra~1\nunit2~1.2\bin\nunit.framework.dll%n%e

Working directory Input:%p, and then select the "Save active File" "Output to List window" "Capture output", and then the same way to create a new user tool, the other parameters are the same, only the command line to change:

Nunit-console%n.dll

The two tools can be named: C#nunit Federated compilation, NUnit command line. At this point the Advanced menu already has more of these two commands, and each has a default shortcut key. OK, click on the following two commands, you can complete a simple unit test. The following is a simple source program:

Using Nunit.framework;
Using System;

Class to is tested.
public class Target
{
public int Add (int i, int j)
{
return i+j;
}
}

Class to perform testing.
[Testfixture]
public class Test
{
[Test]
public void Testadd ()
{
Target testobj = new Target ();
Assert.AreEqual (1, testobj.add (0, 1));
Assert.AreEqual (Testobj.add (2, 7));
Assert.AreEqual (Testobj.add (2, 8));
Assert.AreEqual (Testobj.add (18, 3));
}
}

After clicking on the two commands, NUnit will detect the failure of the second assertion.

This paper briefly introduces the method of manual unit test, in which the Nunit.core.dll reference can be added to perform some complex unit tests. It's still the same sentence. For their own learning, the Editor +sdk+ command line is a good way to write code, to help understand the relationship between the file, accessories (Assembly), but also to help the memory class of the architecture and accurate method attribute name, but after all, not suitable for industrial production. After a period of time with the IDE, return to simplicity, more can realize the fun of programming. This reminds me of the phrase, how to say: Using Emacs is a spiritual:-) that programmers pursue



Reference:
Andrew Hunt, David Thomas, Unit Test path C # edition; http://www.nunit.org/csc.exe-hhttp://msdn.microsoft.com/msdnmag/issues/04/04/ extremeprogramming/
Roadahead, 0503, in dormitory


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.