1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Test Driven Development in Ruby--Test Suites


Why Use Test Suites?

Test suites are a way of grouping your test case into a single, pseudo test case that encapsulates all of your test cases. While that may sound complicated, it's actually intended to make things easier for you. Ideally in Test Driven Development, you should have one or more test cases for every class, with each test case having its own file. However, this can quickly grow out of hand. Keep in mind that tests are useless unless they're run often. If tests are spread across multiple files, then running them becomes difficult, so it may not be done. Test suites solve this problem by encapsulating all of your test cases into a single runnable file.

Besides running all of your tests at once, creating test suites has another advantage. Since tests do take time to run--and you're usually only working on a small subset of the program at once--forming smaller test suites to focus on what you're doing at the moment can save time when running tests. That doesn't mean, though, that you can forget to run all of your tests often. If you fail to run all your tests, you may break some of your code and not realize it until much later.

Keeping Things Neat and Tidy

It's important to follow the naming convention to keep things neat and tidy. Test case classes should begin with TC_, and end with either the class name they test or a short name of the functionality they test. The filenames should be the same as the class names, only with camel case converted to underscores. For example, the TC_NameParser test case class should reside in the tc_name_parser.rb file.

Your overall test suite should be in a file all of its own. The filename doesn't matter: test_suite.rb is a good choice. Name it something that should be easy to find or is recognizable, because this is what you're going to be running every time you run your tests.

The test suite itself only really needs to require the test cases. Though it is possible to create a test suite class and gather all the tests from the test cases into the class, it just isn't necessary. Test runners are able to crawl the ObjectSpace and find all TestCase instances. Simply requiring them inserts them into the ObjectSpace and makes them known to the test runners.

Here is a simple test suite. It requires two test cases: tc_first_class and tc_second_class. Note it doesn't tell the runner to run, that's done automatically when you run the script. However, running other test runners besides the console runner is possible. Running this test suite is just like running any of the test cases individually.

[blockquite shade="yes"] require 'tc_first_class.rb'
require 'tc_second_class.rb'
  1. About.com
  2. Technology
  3. Ruby
  4. Advanced Ruby
  5. Test-Driven Development
  6. Test Driven Development in Ruby--Test Suites

©2014 About.com. All rights reserved.