Toolbox

Powerful Text Editing, Web Testing in .NET, Extended Unit Testing, and More

James Avery

Contents

Edit Well with E Text Editor
Create Functional Web App Tests
A New Testing Framework
Pragmatic Unit Testing

Edit Well with E Text Editor

A good text editor is an essential tool for every programmer. A programmer's choice of text editor is also one of the more frequent debates you will hear in the halls of conferences and workplaces. TextMate, a Mac OS X text editor, has quickly risen to popularity on that platform based on its ease of use and powerful extensibility capabilities. E Text Editor is a new text editor that tries to bring that same simplicity and powerful extensibility to the Windows® platform.

E Text Editor lays a solid foundation with easy-to-use and simple text editor functionality. The program provides you with the ability to open multiple files at a time using tabs in a similar manner to the tabbed browsing format offered in many browsers. It also offers a project pane that provides you with a directory structure view of a given directory. In the example screenshot, I have an ASP.NET Web application folder open, and you can see each of the different directories and files (including the correct icons). The project pane makes it very easy to move around between directories and files in your project.

E Text Editor extensibility is centered on the concept of bundles, which provide a way to include specific functionality for various programming languages without increasing the bloat of the native application, similar to plug-ins or add-ins in other software. By default, E Text Editor includes bundles for the majority of languages any Microsoft developer is going to be looking for, including C, C#, JavaScript, HTML, CSS, XML, and more. The one language that appears to be missing is Visual Basic® .NET. However, E Text Editor includes the functionality to create your own bundles, so maybe someone will create and publish a Visual Basic .NET bundle soon.

Bundles can include a number of different features, the first of which is text completion. Unfortunately, this is not the text completion you'll find in IntelliSense® in Visual Studio®, but rather it is text completion based on a certain set of keystrokes (similar to code snippets in Visual Studio). For example, in the C# bundle there is a text completion setup that lets you quickly declare a new namespace by typing "ns" and then clicking the tab key. You can create a new property by typing prop and then tab and so forth. Each bundle contains text completions that are specific to the language the bundle is written for.

Bundles also contain formatting rules for their particular language. When you open a new file, you will see in the bottom middle of the status bar the format type being used for the document. For instance, opening a C# document will show C# in the toolbar and provide C# syntax highlighting and formatting. You can click on the format in the status bar to change how the document should be formatted. This is important, as certain files can be viewed in different formats. For example, you could have an .html file that could be ASP, ASP.NET, or another Web framework. Choosing the correct format will not only give you the best syntax highlighting and formatting but will also enable the text completions available in that bundle.

E Text Editor is a great new option in text editors, and I fully expect to hear it mentioned in the next text editor debate that I encounter.

Price: $34.95.

www.e-texteditor.com

E-Text Editor Offers Simple Text-Editing Functionality

E-Text Editor Offers Simple Text-Editing Functionality  (Click the image for a larger view)

Create Functional Web App Tests

Unit testing can go a long way to helping avoid the testing and re-testing of an application, but unit tests usually test the layers and objects behind an application and not the application itself. All of your unit tests might pass, but there might be an error on your ASP.NET page or in your Windows Forms code that wasn't covered by your unit tests.

Watin, which stands for Web Application Testing in .NET, was inspired by Watir, a similar open source framework written in Ruby. Watin is an open source tool that can be used to write tests directly against your Web applications. Tests written in this way are called functional tests since they are testing the functionality of your application as opposed to a single unit of code, such as with unit tests.

Watin interfaces with the Internet Explorer® object to launch and interact with the browser in the same way that your user would. Watin can enter text into fields, select radio buttons, click checkboxes and buttons, and read values from your forms and pages. You can even sit and watch Watin do its work on your screen, which, with the help of a cleverly hidden book, can make it appear that you are working really hard.

Watin tests are written using your favorite unit-testing framework in conjunction with the Watin library. The Watin library provides the methods and objects needed in order to interface with the Internet Explorer object while your testing framework provides the tests and assertions.

Watin tests can be somewhat frustrating to write by hand. To identify textboxes and buttons, you have to use the client identifier, which you can find by viewing the source of your page or using one of the various popular browser toolbars.

An even better way to write Watin tests, though, is by using another open source project called the Watin Test Recorder. The Watin Test Recorder is a Windows Forms application that lets you create and record Watin tests by walking through the test using the browser.

To create a simple test, you would only need to navigate to the page, name your test, and then perform the actions that are needed to replicate your test. For example, you might navigate to your home page and enter a user name in the user name textbox, a password in the password textbox, and then click the login button.

The code for your test is displayed in the box at the bottom of the application. You can then copy this code out or save it directly from the recorder. After copying or saving the test, you will then need to add assertions. Using the previous example, you would write an assertion that confirms the user was logged in successfully. The assertion could either test that the browser redirected to the correct page or look for a certain section of text on that page.

Watin can be a great replacement for manually testing your Web application over and over again and helps you quickly find issues before your users do.

Price: Free; source code available.

watin.sourceforge.net

Write Tests against Your Web Apps with Watin

Write Tests against Your Web Apps with Watin  (Click the image for a larger view)

A New Testing Framework

The Microsoft® .NET Framework has a number of established unit-testing frameworks including nUnit, csUnit, and mbUnit (which was featured last month in this column), but recently another new framework was launched called xUnit.net.

A couple of things make this framework stand out from the existing frameworks. Most significant is that it was built by James Newkirk and Brad Wilson. Newkirk, who helped build nUnit, is a product manager at Microsoft responsible for CodePlex, and he has written a number of books about unit testing. Brad Wilson is a long-time blogger (as thedotguy), a former member of the patterns and practices team, and a fellow Microsoft employee. The goal with this new framework was to take the best practices learned over the past half-decade about unit testing and build a new framework that embodies and encourages those practices.

There are quite a few changes in this framework compared with the existing .NET unit-testing frameworks. Most notable are the features that have been removed—in an effort to simplify the framework and the tests written using it, a number of attributes that are present in many testing frameworks have been removed. To make tests easier to read and understand, there are no setup or teardown attributes—all initialization and teardown code must be done in the individual tests.

The idea behind removing these attributes is that they only make it harder to understand what exactly a test is doing because the logic can be split over three different methods. By removing the setup and teardown attributes, Newkirk and Wilson are encouraging developers to put all of the logic for their tests into the single-test method.

The ExpectedException attribute is gone in favor of a new method on the Assert object called Throws. The Throws assertion allows the developer to be more explicit in specifying where the expected exception will come from, making false positives less frequent. TestFixtureSetup and TextFixtureTeardown have been replaced with an ITestFixture interface. The Ignore attribute has been removed and instead implemented as a property on the Test attribute.

xUnit.net is not just about removing features, though—a number of very useful features have been added to the framework as well. xUnit.net includes complete support for generics and anonymous delegates, which make tests more concise and accurate. Also, xUnit.net includes the ability to extend the functionality of the framework using custom comparers, custom test methods, and custom classes.

There is no GUI testing tool included with this release of xUnit.net, only a console runner and Visual Studio support using TestDriven.net.

I would not recommend dropping your current testing framework for xUnit.net just yet, but it is definitely something to keep your eye on, even if only to learn from the great ideas and best practices being put forth through this tool.

Price: Free; source code available.

codeplex.com/xunit

Pragmatic Unit Testing

It may be easy to get started with unit testing, but it can be very difficult to get your unit tests right. When I first started writing unit tests, I had only a couple of blog posts and articles to guide me—I sure wish I had a book like this to refer to then. Pragmatic Unit Testing in C# with NUnit (Pragmatic Bookshelf, 2006), by Andrew Hunt and David Thomas is the second volume in a three-volume series titled The Pragmatic Starter Kit. The first volume is about using source control, and the third volume covers project automation.

Pragmatic Unit Testing covers the basic ideas, principles, and benefits of unit testing, as well as the planning and writing of unit tests. The book uses NUnit for its examples, but the theories apply to just about any of the unit-testing frameworks available for .NET.

Offering a wealth of practical advice on testing, this book is both an excellent introduction to unit testing and a great guide on how to write better unit tests and solve common testing issues.

Price: $29.95.

pragprog.com/titles/utc2

  (Click the image for a larger view)

Send your questions and comments to toolsmm@microsoft.com.

James Avery runs his own .NET consulting practice,Infozerk Inc. He has written a number of books and articles, and his most recent book is Visual Studio Hacks (O'Reilly, 2005). You can e-mail James at javery@infozerk.com.