Lapidary Usage


The general idea behind unit testing is that you write a test method that makes certain assertions about your code, working against a test fixture. A bunch of these test methods are bundled up into a test suite and can be run any time the developer wants. The results of a run are gathered in a test result and displayed to the user through some UI. So, lets break this down and see how Lapidary provides each of these necessary pieces.


These are the heart of the framework. Think of an assertion as a statement of expected outcome, i.e. "I assert that x should be equal to y". If, when the assertion is executed, it turns out to be correct, nothing happens, and life is good. If, on the other hand, your assertion turns out to be false, an error is propagated with pertinent information so that you can go back and make your assertion succeed, and, once again, life is good. So, what assertions can you make in Lapidary? Here's the current set:


Test Method & Test Fixture

Obviously, these assertions have to be called within a context that knows about them and can do something meaningful with their pass/fail value. Also, it's handy to collect a bunch of related tests, each test represented by a method, into a common test class that knows how to run them. The tests will be in a separate class from the code they're testing for a couple of reasons. First of all, it allows your code to stay uncluttered with test code, making it easier to maintain. Second, it allows the tests to be stripped out for deployment, since they're really there for you, the developer, and your users don't need them. Third, and most importantly, it allows you to set up a common test fixture for your tests to run against.

What's a test fixture? Well, tests do not live in a vacuum; rather, they're run against the code they are testing. Often, a collection of tests will run against a common set of data, also called a fixture. If they're all bundled into the same test class, they can all share the setting up and tearing down of that data, eliminating unnecessary duplication and making it much easier to add related tests.

Lapidary::TestCase wraps up a collection of test methods together and allows you to easily set up and tear down the same test fixture for each test. This is done by overriding the setup and/or tearDown(), which will be called before and after each test method that is run. The TestCase also knows how to collect the results of your assertions into a Lapidary::TestResult, which can then be reported back to you... but I'm getting ahead of myself. To write a test, follow these steps:

  1. Make sure Lapidary is in your library path
  2. require 'Lapidary/TestCase' in your test script
  3. Create a class that subclasses Lapidary::TestCase
  4. Add a method that begins with test to your class
  5. Make assertions in your test method
  6. Optionally define setup() and/or tearDown() to setup and/or tear down your common test fixture.


So, now you have this great test class, but you still need a way to run it and view any failures that occur during the run. This is where Lapidary::UI::Console::TestRunner and Lapidary::UI::GTK::TestRunner come into play. They both serve the same purpose, just with different UIs. To use one, simply call its run() class method and pass in an object that responds to the a suite() message with a Lapidary::TestSuite. This can be as simple as passing in your TestCase class (which has a class suite() method). It might look something like this:

	require 'Lapidary/UI/Console/TestRunner'

Test Suite

As more and more unit tests accumulate for a given project, it becomes a real drag running them one at a time, and it also introduces the potential to overlook a failing test because you forget to run it. Suddenly it becomes very handy that the TestRunners can take any object that returns a Lapidary::TestSuite in response to a suite() method. The TestSuite can, in turn, contain other TestSuites or individual tests (typically created by a TestCase). In other words, you can easily wrap up a group of TestCases and TestSuites like this:

	class TS_MyTests
		def MyTests.suite()
			suite =
			return suite


I'd really like to get feedback from all levels of Ruby practitioners about typos, grammatical errors, unclear statements, missing points, etc., in this document. Please see the index for how to contact me. -- Nathaniel Talbott