Lab 5: Harnessing Tests
You’ve written a couple of tests. Now it’s time to put them together with a test harness. This will allow them to run in sequence and automatically report whether any of the tests fail.
We will use Rspec’s test harness. The following demonstrates an empty template. It can be run with Rspec, but doesn’t actually do anything.
require 'spec' describe 'depot tests' do it 'add a book' do end it 'purchase books' do end end
Copy the test scripts you wrote in the previous labs into the “it” blocks. In Ruby, a block is a section of code between “do” and “end”. Rspec uses “it” to mark the blocks containing tests. You can use F5 to execute the tests using Rspec.
- Test Harness. Use rspec to create a test harness that executes all of your test scripts.
There are additional options that are available when you run Rspec tests from the command line.
> spec test.rb -fs Show test description > spec test.rb --dry-run Don't run the tests (only show descriptions)
Completion
When you have completed this lab, you should be able to:
- Harness multiple tests using Rspec.
- Execute Rspec tests.
- Print the names of the defined tests without running them.
You can compare your script to a sample solution for this lab