Products Docs & Support Community

Test-Driven Rails Development

This article describes the NetBeans IDE features that can help you test your Ruby and Rails code. There are several testing options available to you that make it easy to unit test your application.


Tutorial Requirements
NetBeans IDE Ruby Testing Support
Test::Unit Tests
Auto Test
Rspec Tests

Tutorial Requirements

Those of you developing Ruby and Ruby on Rails applications can call on a number of testing features to help test and debug your projects. This article covers the features that are currently available.

This tutorial requires the following:

  • A basic knowledge of programming with the Ruby technologies
  • NetBeans IDE 6.0 with Ruby support

NetBeans IDE Ruby Testing Support

The NetBeans IDE supports some of the Ruby testing frameworks and features. We'll show you what is supported and give you an idea how to use these testing features. We'll also demonstrate some neat NetBeans features that help you track down errors and move between source code and test cases.

Before we cover the Ruby-related testing features, let's take a brief look at unit testing and its basic concepts. When we talk about unit testing, we mean testing that developers do to validate that individual units of an application's source code work properly. A unit is defined as the smallest part of an application that can be tested. Thus, for object-oriented programming, a unit is a class, and may be a base class, super class, abstract class, or derived (or child) class. In procedural programming, on the other hand, a unit is typically an individual program, function, or procedure. Be careful not to confuse units with modules, since modules are larger pieces of an application and are usually made up of units.

Unit testing usually entails a set of test cases independent from each other. A test case may use mock objects to assist the testing, particularly if using a real object is difficult, time-consuming, or hard to generate. A mock object is used to test the behavior of another object since it mimics the behavior of the real object in a controlled manner. A good example of a mock object is a dummy used instead of a real person in car crash testing.

Test harnesses, or automatic test frameworks, are also important in unit testing. A test harness is a collection of software code and test data that runs the program unit under varying conditions, all the while monitoring the unit's behavior and outputs. Because it can be used repeatedly, a test harness automates the unit testing.

Test::Unit Tests

Ruby has a built-in testing framework, designated as Test::Unit. Since the NetBeans IDE directly supports Test::Unit, you can create new unit tests right from a project's New menu. In the Projects window, right click the Ruby project and select New->Ruby Unit Test.

Figure 1: Create a Ruby Test::Unit

The New Ruby Unit Test dialog prompts you for information on the unit test file, including the class to run the test against, the test file name, its module, along with its target directory location within the project. When you click Finish, the IDE creates the skeleton unit test file to which you add your specific testing code. It places this file in the specified folder location within the project.

Figure 2: Create the Unit Test File

Click to enlarge

There are several ways to test or run a unit test. Most likely you'll use the Run File or Test File options from the Editor window context menu (you can also invoke the Run->Run File option on the file in the Project window).

Using Test File Versus Run File Options

The Test File (Ctrl+F6) option, available from the Editor context menu and the Run menu, is the better way to run a test file. You can run Test File from within the test file itself or from the class you want to test. The Run File option, also available from both the Editor context menu and the Run menu, requires that you be in the test file for the class you are testing. The advantage of the Test File option is that, while you can be in the file you want to test, you are not required to be in the test file for the class.

To illustrate the advantage of using the Test File option, suppose you are working on a unit test for a Rails controller. When you are in the controller test file, you run the Test File option and it runs this same test file as a test. However, you could just as easily be editing the controller class itself. If you are in the midst of editing the controller class and want to test it, you merely execute Test File, by pressing Ctrl+F6, and it runs the test file corresponding to the class you are currently in. If instead you use the Run File option on the controller class, the IDE opens the controller's URL in the browser. Thus, think of the Run File option as running a portion of the application in the browser whereas the Test File option runs the corresponding tests for that portion of the application.

There is another important difference between the Test File and Run File options. The Test File option, when applied to a test file, runs the file as a test. Running a file as a test signals the IDE to treat the executing code as a test and it looks at the output for failures. Currently, if it sees any failures, the IDE creates a red warning in the Editor window footer, similar to the red message in Auto Test.

The same differences apply to the Run Project and Test Project options. The Test Project option, which appears as Test in the project's context menu, runs all the tests in the project. (Currently, running all test for a project works just for Rails projects.) The Run option for a project actually runs the project in a browser.

Figure 3: Test the Unit Test File

The Output window at the bottom displays the output produced by executing the unit tests. Since the test output is hyperlinked, you can navigate through it using the Navigate->Next Error menu option.

Figure 4: Example of Test::Unit Output

The Goto Test action option, available from the Editor window's context menu, lets you jump quickly between a class and its corresponding test cases. Right click anywhere in the source file in the Editor window and select Navigate->Go to Test option (Control+Shift+T in Windows and Command+Shift+T on the MacIntosh). The Go to Test action is aware of Test::Unit conventions. It is also aware of Rails conventions, RSpec conventions, and ZenTest conventions.

Figure 5: Go to Test Action

Click to enlarge


Auto Test

Your Ruby project can also include an Auto Test option. Auto Test runs unit tests automatically whenever you modify a file. For Rails projects, and in many other cases, Auto Test can determine the unit tests that need to be run. If it cannot make this determination, Auto Test defaults to running all unit tests.

The Auto Test option is available when you install the ZenTest Ruby Gem. Use the Tools menu Ruby Gems option to install ZenTest or any other Ruby Gem. When this option is selected, the IDE loads the installed Ruby Gems and displays all available and new Gems. Click the Installed tab to see the Ruby Gems already installed.

Figure 6: Check Installed Ruby Gems

Click to enlarge

If ZenTest is not installed, click the New Gems tab, locate zentest in the list, select it and click Install.

Figure 7: Install ZenTest Ruby Gem

Click to enlarge

You should see a message indicating that ZenTest and its associated libraries and tools are installed.

Figure 8: ZenTest Installation Message

Once ZenTest is installed, you should see an Auto Test option in the context menu for the project. In the Projects window, right click the Ruby project node and select Auto Test to launch Auto Test on your project. As noted, Auto Test runs either the appropriate unit tests or all unit tests when files are modified.

Figure 9: Invoke Auto Test

The output for Auto Test appears in the Output window. If failures occur, Auto Test posts a summary message in the Editor window footer. Although the Auto Test pane in the Output window may be hidden, you can click the Auto Test tab and examine the output detail to determine the cause of the failure. You can also click the hyperlinks in the Auto Test output to go directly to the test cases that failed.

Figure 10: Auto Test Summary Message and Output Window

Click to enlarge

The short cut Goto Test action (Ctrl+Shift+T) also works in the Auto Test context; it takes you directly to the matching test file.

Note: Auto Test support will be enhanced in the near future. Enhancements may include displaying test failures in a task list. JRuby support for Auto Test will also be improved. If you encounter problems running Auto Test correctly on JRuby, switch to native Ruby for the moment.

RSpec Tests

The rspec Ruby Gem is another tool for unit tests. It is bundled with the NetBeans IDE as part of the JRuby distribution, so if you use the bundled Ruby interpreter rspec is already installed. When rspec is installed, your projects include a spec directory that contains specification files.

However, should you decide to use native Ruby, which you can do by switching interpreters in the Options dialog, then you need to check if rspec is installed (see Figure 6). If not, click Tools->Ruby Gems, then the New Gems tab (see Figure 7) to install it. If you want to use rspec with Rails, you may need to install another Ruby Gem and you should check these instructions.

You can use the Goto Test action to open any of these specification files. Use Test File or Run File from the Editor context menu to run the specification file under rspec control, which means that the test follows the specification file options found in the spec.opts file in the spec directory. Note that the same considerations discussed earlier apply for using Test File versus Run File in this context. You can also run the test from within the IDE and have it use a different set of options, such as turning off the red and green colored flags. Create a file called spec.opts.netbeans with the different options and place the file in the same spec directory.

Although not yet available, spec file templates are coming.


This article covered the NetBeans IDE features for testing Ruby applications. It covered the basic unit test feature, Auto Test, and rspec. It also explained how to install Ruby Gems into the IDE and use the IDE's testing short cut features.

Next Steps

  • Check out the Ruby and Rails Learning Trail to learn more about NetBeans IDE support for Ruby.
  • To obtain support and stay informed of the latest changes to the NetBeans Ruby development features, join the and mailing lists.
  • To submit your own NetBeans Ruby tutorial, please visit the NetBeans Community Docs page.

>> More NetBeans Ruby Documentation