Doctest::RSpec
Add doctests to your ruby files and have them executed within a rspec test suite.
Installation
Add this line to your application's Gemfile:
gem 'doctest-rspec'
And then execute:
$ bundle
Or install it yourself as:
$ gem install doctest-rspec
Usage
Add doctest-formatted tests to your ruby files (see http://github.com/doctest/doctest-core for syntax examples)
Add doctest directives to your rspec suite:
class MyClassWithDoctests
# Adds two to a given number
#
# Usage:
# >> m = MyClassWithDoctests.new
# >> m.plus_two(3)
# => 5
def plus_two(x)
2 + x
end
end
Within the rspec suite:
require 'doctest/rspec'
describe MyClassWithDoctests do
doctest MyClassWithDoctests
end
This will lead to one passing spec.
This will parse the given source files for doctest occurrences and add a spec for each instance.
Alternative Solutions
- rubydoctest, rdoctest: Serve a very similar purpose. However, they do not integrate into rspec, but provide a custom test runner.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request