|
YOUR FEEDBACK
|
TOP MICROSOFT .NET LINKS Hot Story Unit Testing with Whidbey
A concise look at Microsoft's impressive offering
By: Scott Dockendorf
Dec. 7, 2004 12:00 AM
Do you want to improve the quality of your software? Does your budget require you to shorten your development life cycle? Still searching for an effective way to provide requirements to development teams, internal and outsourced? If you answered yes to any of these questions, look no further than test-driven development (TDD). This unique approach to software development, created in 1989 as a component of eXtreme Programming, includes the creation and execution of automated unit tests before writing a line of implementation code. Coupled with a thorough design of your software, TDD will help reduce ambiguity in communication, ensure efficient software construction, and help reduce the amount of defects with your software. Whether your environment follows TDD's test-first approach (test, code, refactor), code-first approach (interface/test-code, refactor), or simply uses automated unit tests to support their traditional development, build, and deployment cycles, the new features included in Visual Studio 2005, Team Developer and Team Test editions, will satisfy your quality needs. This article offers a concise look at the timesaving features included in Microsoft Visual Studio 2005 Team System's Unit Testing Framework, using a code-first approach to TDD. What is TDD's Code-First Approach?
When considering TDD for the first time, some of the most common concerns are that it "requires too much code, adds additional hours to the estimate," or worse yet, "requires additional resources dedicated to these tests." While this approach does require additional code, the amount of code needed tends to be exaggerated. First, component developers already create their own unit testing logic, but usually in a different format - possibly a console or a WinForm application. These applications tend to be undocumented, and allow for ambiguity for the consumer of the components. Second, having designed the components up front, the time spent on brainstorming of tests should be dramatically less than the time actually coding these tests. Many TDD followers believe that the time spent building unit tests is dramatically less than the time spent on resolving defects raised during the quality assurance process. One of the most beneficial features of Unit Testing in Visual Studio 2005 Team System is the ability to dynamically generate code for base unit tests. This code generation engine provides an easy way to customize and enforce desired standards and naming conventions for yourself or a large group of developers. To begin this process, we'll create a new class library project that will represent the test suite, or grouping of text fixtures. Simply select File>Add>New Project and create your test suite project. Now that we created the destination project it's time to generate code for our tests. There are two ways to launch the Create Tests wizard. You can either use the icon located in the Test Views toolbar, or right-click any code element in the code editor (Namespace, Class, or Method) and select Create Tests.... Once clicked, Visual Studio 2005 displays the Unit Test Generation dialog, which dynamically builds a checkbox tree view that allows you to:
Because the Unit Testing Framework generated sample methods specific to our implementation, all that is required is to complete the test code and replace the Assert.Inconclusive with Assert method calls needed to pass the test. Feature #2: Unit Testing/Visual Studio 2005 Integration The Visual Studio 2005 IDE includes four main windowpanes used for unit testing, which are viewable using the View, Quality Tools menu option. First is the Test View, which lists all the methods containing [Test] attributes for each project in the solution. Next is the Test Explorer, which provides the ability to search, filter, and execute any number of included tests. Once the test execution is complete, Visual Studio 2005 will display the status for each test in the "Test Results" window pane. Finally, if you requested code coverage instrumentation, those metrics are located in the Code Coverage Results dialog. From the View menu, selecting the Quality Tools/Test Explorer menu item will display the Test Explorer dialog. Visual Studio 2005 uses reflection to dynamically discover each test suite, fixture, and test method uses this information to populate this dialog box. This window provides extensive searching and filtering options to help manage the execution of a large number of tests. Basically, you'll need to select which tests you want to execute. Clicking on entries in the left-hand navigation will select the respective tests on the right portion of the window. For our example, we'll want to execute all the tests. Follow these steps to accomplish this task: 1. Click the [All Tests] option on the left-hand navigation. Once clicked, Visual Studio 2005 will automatically select every test listed in the right-hand list view. The first time you attempt to execute your tests, Visual Studio 2005 will ask you to create a Unit Test Configuration, which results in a new *.rcg file added to the solution. Visual Studio creates the file from settings entered in the Select a Test Run Configuration dialog. This dialog allows you to perform the following functions:
At this point in the TDD process, we have (a) created our interfaces, (b) created our tests (via code generation), (c) coded our tests based on our requirements, (d) executed our tests, and watched them fail (red). Now is the fun part - generating the implementation code to satisfy our design requirements. Once coding is complete, it is time to rerun our tests to watch them pass (green). To accomplish this, simply edit the code for your class library, re-execute the tests, and monitor the results in the Test Results windowpane. Feature #3: Code Coverage - How Much Code Actually Executed? Microsoft recognized this need, and built the "code coverage" feature into the Unit Testing framework. This feature provides developers with valuable, quantitative metrics obtained directly from executed code. Specifically, code coverage provides the number and percentage of lines of code (a) executed, (b) not executed, and (c) partially executed, for each referenced assembly in your project down to an individual method level. To enable code coverage statistics, navigate to the Code Coverage tab in the Test Run Configuration dialog that appears when you execute your tests. In this dialog, you must complete two steps. First, select the checkbox, "Enable code coverage instrumentation. Second, select which assemblies to calculate statistics on by selecting the assemblies on the left and "add"ing them to the list box on the right, labeled "To instrument." Upon completion of your test, click the Code Coverage dialog to view the results, represented in Figure 3. For each test execution, Visual Studio 2005 will dynamically build a tree view, which provides the developer with drill-down capability for these metrics. With a simple click of a button, Visual Studio 2005 will highlight each line of source code with color-coding to help identify which lines were completely, partially, or not executed. Notice that Visual Studio 2005 loaded the "Code Coverage" toolbar in the IDE during test execution. To determine which lines of code executed, simply click the first button on this toolbar labeled "Show Code Coverage." With this button selected, the IDE will present the source code with the appropriate color coding. Conclusion References YOUR FEEDBACK
MICROSOFT .NET LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||