Tryouts - v0.7 BETA

Tryouts is a high-level testing library (DSL) for your Ruby codes.

A tryout is made up of one of more drills. The return value of the drill block is compared to the expectations defined by one or more dreams. The goal is to achieve your dreams.

Terminology

  • Tryout: a set of drills (think: basketball tryouts)

  • Drill: a single test.

  • Drill Sergeant: The class responsible for executing a drill.

  • Dream: the expected outcome of a drill. There’s always one or more dream per drill.

Installation ==

$ gem install tryouts

}}

Examples

The examples below are a complete overview of Tryouts syntax.

Testing Ruby Codes (:api)

library :caesars, "../path/to/caesars/lib"

tryouts "Common Usage" do

  # This drill block should return 3.
  drill "Maths R Us", 3 do
    12 / 4
  end

  # You can specify a method to execute
  # on the return value of the drill block. 
  drill "We want a symbol", Symbol, :class do
    orange.methods.first
  end

  # Dreams can also be specified explicitly which is
  # important b/c it's possible to specify multiple.
  dream Array, :class
  dream [:a, :b, :c]
  drill "Should return a list of 3" do
    Letters.list(3)
  end

  # Drills can pass based on an exception too.
  dream NameError, :exception
  drill "Something failed" do
    raise NameError
  end

  # We can even put simple drills on a single line. 
  drill "Santa's postal code", 'H0H 0H0', /\w\d\w \d\w\d/, :match

end

Benchmarks (:benchmark)

You can also use Tryouts to run benchmarks. The tryouts method takes a second parameter which specifies which drill sergeant to use. Below we specify :benchmark so each drill is timed and executed multiple times.

tryouts "Benchmark examples", :benchmark do

  # We create an Array and store it in a class
  # variable so it's available to other drills.
  drill "Create test array" do
    @@array = (1..100000).map { rand }
  end

  dream 3.0, :mean        # The mean should be <= 3.0 seconds
  dream 0.1, :sdev        # and the standard deviation <= 0.1
  drill("Array sort!") { @@array.dup.sort! }

  # You can also include a dream inline
  drill("Array sort", 3.0, :mean) { @@array.dup.sort }

  # The 3rd argument is the number of times to 
  # execute the drill block. The mean and sdev
  # are calculate based on all iterations. The
  # default is 5 and the maximum is 30.
  dream 0.1, :sdev, 15    
  drill("Array sort") { @@array.dup.sort }

end

The drill blocks in a benchmark Tryout return Tryouts::Stats objects. See: Tryouts::Drill::Sergeant::Benchmark

Screenshots

Here is an example of Tryouts output from a gibbler tryout:

The drill that failed looks like this:

dream :to_gibble, :respond_to?
dream 'ab33b9dec202d136d0e695a3a7b06ee678134882', :to_gibble 
drill "Array", Array

BETA Notice

This library is very new (est. 2009-05-19) and has not been vetted by the scrutiny of time. In particular you can expect:

  • The test definition syntax may change in future releases.

  • Unexpected errors.

On Threads

Tryouts does some funky stuff to make it simple to write tests. This “funky stuff” means that this library is not thread-safe at definition-time. However, once all tryouts files are parsed (or in OO-syntax, once all objects are created), this class should be thread-safe at drill-time.

More Info

Thanks

  • Everyone at Utrecht.rb and Amsterdam.rb for putting up with my Ruby questions :]

  • Sam Aaron (sam.aaron.name) for the early feedback.

Credits

  • Delano (@solutious.com)

Related Projects

License

See: LICENSE.txt