A good assistant for TestDriven.NET 2.0--Unit test __.net

Source: Internet
Author: User
Tags assert instance method reflector
a good assistant for TestDriven.NET 2.0--Unit test Introduction

In the area of software development, testing-driven development (test driven DEVELOPMENT,TDD) technology is increasingly accepted by developers as a development technique for making your test code go wrong before adding new code. Before you start writing your code, write your test code, and then write the code to get your test through, and imagine that without a good unit test tool, it's not going to work in the first place. Believe a lot. NET developers are not unfamiliar with the nunit of open source, but it does not integrate well with the Visual Studio.NET IDE.

Here I will introduce another excellent unit test Tool--testdriven.net 2.0. It is a plug-in-integrated unit Test tool in the Visual Studio.NET IDE, compatible with a variety of Visual Studio versions, and integrates a variety of unit test frameworks such as Nunit,mbunit, as well as the Visual Studio Team System adapters, and so on.

The predecessor of TestDriven.NET was Nunitaddin, developed by Jamie Cansdale Master, who initially wanted to do just one NUnit plug-in integrated into Visual Studio, after several versions of the update, Nunitaddin changed its name to TestDriven.NET at the end of September 2004 and released its first release version, TestDriven.NET 1.0, at the beginning of December. Thanks to its excellent performance in development, many developers are looking forward to its next release, a wait that has been waiting for two years, at the end of October this year, finally ushered in a belated TestDriven.NET 2.0 version. But the wait was worth it, and in 2.0, it brought us too many surprises, integrating. NET Reflector, Nconver, Nconverexplorer, Typemock.net and so on, the function is more powerful, I will introduce the TestDriven.NET 2.0 in the following highlights. Basic Use

After you install TestDriven.NET 2.0, restart Visual Studio 2005 to discover that there are new items in the right-click menu of the Visual Studio.NET IDE, all of which are done directly through these right-click menus. Let's start by writing a short piece of simple test code:

[Test]

public void Testmethodrun ()

{

ClassA CA = new ClassA ();

int expected = 2;

int result = ca. GetResult ();

Assert.AreEqual (expected, result);

}

In the past, if you used NUnit for unit testing, for such a simple test, you first need to run the NUnit GUI, load the current assembly, and click the Run button to run the test, and then go back to the IDE to write the code after the test passes:

Now with TestDriven.NET 2.0, there's no need to run any more programs, just right-click and select the Run Test menu command in the visual Studio IDE to run the test. We're used to doing this in the IDE in our day-to-day development, so it's more in line with our development habits, and we don't have to switch windows frequently between applications.

The results of the TestDriven.NET 2.0 test will be displayed in the Output window.

A friend who writes a little bit of unit test code knows that every test in the actual development cannot have just one assertion (assert), and that when NUnit is used, the test, if it fails, cannot be quickly and accurately positioned against an assertion in the code, With TestDriven.NET 2.0, when a test error occurs, you simply double-click the error prompt in the Output window to navigate directly to the line of assertion code that is wrong, greatly improving the efficiency of the error.

In the test code above, we just clicked on the right-click menu run test and did not choose which test method to run or the test class, so how TestDriven.NET 2.0 chooses and runs the test. Actually, it's about where we click the mouse, click the location of the mouse if it is in test method marked as test, it will only execute the test method; If you click outside the method and in the class labeled Testfixture, it executes all test methods that are labeled test in that class. The location of the mouse click, if it is outside the class, and within a namespace, it executes all the test classes marked as Testfixture in that namespace. Ad-hoc Test

In development, we often encounter values that track some variables and return values for some methods, and in general you will choose to use command Window, which requires a few commands to view, which is a bit of a hassle. TestDriven.NET 2.0 provides us with the function of one key operation so that we can track the return value of the method anytime and anywhere without entering any commands. TestDriven.NET 2.0 in the normal method can also run the test (run test), whether the scope of the method is public or private, an instance method or a static method. If the test method is an instance method, it creates an object of a class using the default constructor, and then releases the object when the test completes. If the method has a return value, it will appear in the Output window. Run the following test:

will be exported in the Output window:

test with Debug mode

In the process of running a test, you sometimes need to debug and trace some information, and if you use NUnit, you cannot set breakpoints because it is not integrated in the IDE. The TestDriven.NET 2.0 provides testing in debug mode, where breakpoints can be set and tracked in test code, and we need to select Test with | Debugger menu commands.

Stop the test process

Some application domains are sometimes created while the test is running, but are not freed, so that some assemblies are loaded into the application domain and are locked, so that compilation may fail. Then you need to release some of the test process to continue compiling. In the lower-right corner of the taskbar, all of the test processes will be displayed here, right click and select the Stop menu command to end the test process. Integrated Reflector

. NET Reflector is a. NET platform, we can use it to browse the classes and methods of assemblies, to analyze the intermediate language (MSIL) generated by these classes and methods, which is believed to have been used, but it still exists as an independent tool, It is well integrated into the Visual Studio.NET IDE in TestDriven.NET 2.0, and you can use. NET Reflector directly from the right-click menu.

Integrated Code Coverage Analysis tool

When we do unit testing, we always want to know how much of our code is covered, which code is not tested, and if you do not have visual Studio team System installed, you cannot use team Converage for code coverage analysis. Using the famous nconver is a good choice and using Nconverexplorer to view the results of the analysis, all of which still need to run new applications, TestDriven.NET 2.0 integrates all of this into the IDE, You can use team converage in environments where Team System is not installed, and you do not have to install nconver separately.

Run tests repeatedly

Running tests repeatedly can be said to be one of the highlights of TestDriven.NET 2.0. Before testdriven.net2.0 appears, if you are running a test, test code can not pass, then we will spend a few seconds to modify the test code, and run the test again, but often found that the test is not found what the code block, then you can use the repeated run test commands, regardless of where you click the location of the mouse , it will run the previous test directly.

Pluggable Unit Test Framework

There are a lot of unit test frameworks under the. NET platform, and TestDriven.NET 2.0 is unlikely to take all of the unit test frameworks, but it provides a good pluggable mechanism. When you install a new test framework, TestDriven.NET 2.0 automatically appears in its menu, right-click the test with menu command, and choose which unit to use to test the framework.

Integrated Typemock.net

In the process of doing unit testing, the most troublesome part is that our test code must rely on other parts of the system, such as database. At this time we will use a substitute for these real objects, that is, to use mock objects, fortunately we do not have to implement our own mock objects, under the. NET platform, there are many mock object framework for our choice. Before TestDriven.NET 2.0 appears, use the framework of which mock object we need to download and install them, in TestDriven.NET 2.0 has integrated the Typemock.net framework, which makes it easy to use directly without having to choose again.

Concluding remarks

If you only do unit tests, I will choose NUnit, if you want to do the class library decompile, I will choose. NET Relector, if you want to analyze the code coverage, I will choose Nconver, if you want to see the code Coverage Analysis results, I will choose Nconverexplorer, But if you want to integrate all of these things together in the IDE, there is no doubt that I will choose TestDriven.NET 2.0. I hope this article can be a guide to the use of TestDriven.NET 2.0, on the road to unit testing. Of course, for TestDriven.NET 2.0, its professional and development editions have begun to charge, and only the personal version can still be downloaded for free.

For more information about TestDriven.NET 2.0, please visit: http://www.testdriven.net/

Original posted Address:http://www.cnblogs.com/carlice/archive/2008/04/13/1151260.html

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.