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:
assertBlock(message="") { block }
The assertion upon which all other assertions are based. Passes if block
yields true
.
assert(boolean, message="")
Passes if boolean
is true
.
assertEqual(expected, actual, message="")
Passes if expected == actual
. Note that the ordering of arguments is important, since a helpful error message is generated when this one fails that tells you the values of expected
and actual
.
assertNotEqual(expected, actual, message="")
Passes if expected != actual
.
assertSame(expected, actual, message="")
Passes if actual.equal?(expected)
(i.e. they are the same instance).
assertNotSame(expected, actual, message="")
Passes if !actual.equal?(expected)
.
assertRaises(exception, message="") { block }
Passes if block
raises exception
.
assertNothingRaised(message="") { block }
Passes if block
does not raise an exception
.
assertThrows(symbol, message="") { block }
Passes if block
throws symbol
.
assertNothingThrown(message="") { block }
Passes if block
does not throw anything.
assertInstanceOf(klass, object, message="")
Passes if object.class == klass
.
assertNil(object, message="")
Passes if object.nil?
.
assertNotNil(object, message="")
Passes if !object.nil?
.
assertKindOf(klass, object, message="")
Passes if object.kind_of?(klass)
.
assertMatch(string, regularExpression, message="")
Passes if string =~ regularExpression
.
assertDoesNotMatch(string, regularExpression, message="")
Passes if string !~ regularExpression
.
flunk(message="")
Always fails.
Notes:
assertBlock()
.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:
require 'Lapidary/TestCase'
in your test scriptLapidary::TestCase
test
to your classsetup()
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'
Lapidary::UI::Console::TestRunner.run(MyTestCase)
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 = Lapidary::TestSuite.new()
suite.add(TC_MyFirstTests.suite())
suite.add(TC_MoreTestsByMe.suite())
suite.add(TS_AnotherSetOfTests.suite())
return suite
end
end
Lapidary::UI::Console::TestRunner.run(TS_MyTests)
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