• Stars
    star
    154
  • Rank 242,095 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 14 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Isolated tests in Ruby.

Cutest

Gem Version

Forking tests.

Description

Each test file is run in a forked process to avoid shared state. Once a failure is found, you get a report detailing what failed and how to locate the error and the rest of the file is skipped.

You can use the scope command around tests: it guarantees that no instance variables are shared between tests.

There are two commands very similar in nature, but with a subtle difference that makes them easy to combine in order to satisfy different needs: prepare and setup.

The prepare blocks are executed before each test. If you call prepare many times, each passed block is appended to an array. When the test is run, all those prepare blocks are executed in order. The result of the block is discarded, so it is only useful for preparing the environment (flushing the database, removing a directory, etc.).

The setup block is executed before each test and the result is passed as a parameter to the test block. Unlike prepare, each definition of setup overrides the previous one. Even if you can declare instance variables and share them between tests, the recommended usage is to pass the result of the block as a parameter to the test blocks.

The test method executes the passed block after running prepare and setup. This is where assertions must be declared.

Three assertions are available: assert, that accepts a value and raises if it's false or nil; assert_equal, that raises if its arguments are not equal; and assert_raise, that executes a passed block and compares the raised exception to the expected one. In all cases, if the expectation is no met, an AssertionFailed exception is raised.

You can customize the output of assert by providing a second argument with a string you want to get as an error report if the assertion is not fulfilled. This can also be used as a simple building block to build custom assertions.

Usage

In your terminal:

$ cutest test/*.rb

In your tests:

setup do
  {:a => 23, :b => 43}
end

test "should receive the result of the setup block as a parameter" do |params|
  assert params == {:a => 23, :b => 43}
end

test "should evaluate the setup block before each test" do |params|
  params[:a] = nil
end

test "should preserve the original values from the setup" do |params|
  assert 23 == params[:a]
end

An example working with a prepare block:

prepare do
  Ohm.flush
end

setup do
  Ohm.redis.get("foo")
end

test do |foo|
  assert foo.nil?
end

And working with scopes:

setup do
  @foo = true
end

@bar = true

scope do
  test "should not share instance variables" do |foo|
    assert !defined?(@foo)
    assert !defined?(@bar)
    assert foo == true
  end
end

The tests in these two examples will pass.

Unlike other testing frameworks, Cutest does not compile all the tests before running them.

A simple example for adding a custom empty assertion:

def assert_empty(string)
  assert(string.empty?, "not empty")
end

test "failed custom assertion" do
  assert_empty "foo"
end

Handling errors

If you get an error when running the tests, this is what you will see:

Exception: assert_equal 24, params[:a] # 24 != 23
test/setup.rb +14

Running the build

Using Rake:

task :test do
  exec "cutest test/*.rb"
end

task :default => :test

Using Make:

.PHONY: test

test:
  cutest test/*.rb

Command-line interface

The tool cutest accepts a list of files and sends them to Cutest.run. If you need to require a file or library before running the tests, as is the case with test helpers, use the -r flag:

$ cutest -r ./test/helper.rb ./test/*_test.rb

If you want to check which version you are running, try the -v flag.

Installation

$ gem install cutest

License

Copyright (c) 2010 Damian Janowski and Michel Martens

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

hasp

Half-assed CSS preprocessor.
Shell
156
star
2

ack-tutorial

Perl
80
star
3

batch

Iterate over Enumerables with progress reporting.
Ruby
59
star
4

pygmentize

A Ruby gem that vendors Pygments
Ruby
49
star
5

tele

Ruby
31
star
6

mock-server

Ruby
30
star
7

dependencies

Specify your project's dependencies in a single place.
Ruby
26
star
8

yoredis

A minimalistic Redis client using modern Node.js.
JavaScript
22
star
9

collage

Rack middleware that packages your JS into a single file.
Ruby
18
star
10

replete

Ruby
16
star
11

joe

Release your gems, no pain involved.
Ruby
15
star
12

google_services

Rails plugin that provides simple integration with Google services like Analytics, Webmaster Tools, etc.
Ruby
13
star
13

estafabot

Alerto sobre posibles intentos de estafa en Twitter.
JavaScript
12
star
14

havanna

Ruby workers for Disque.
Ruby
11
star
15

twisque

A Buffer clone using Ruby and Disque, deployed to Heroku for free.
Ruby
11
star
16

websequencediagrams

Command-line utility to interface with websequencediagrams.com.
10
star
17

git-spring-cleaning

Clean up your local Git repository every once in a while.
Shell
9
star
18

redic-pool

A Redis connection pool using Redic.
Ruby
8
star
19

redis-pool

A Redis connection pool on top of redis-rb.
Ruby
8
star
20

helm

A command line interface to interact with project management tools.
Ruby
8
star
21

layout

Enable Rails to handle layouts programatically
Ruby
7
star
22

ost-bin

ost(1)
Ruby
7
star
23

cacho

A careless caching client optimized for scraping.
Ruby
6
star
24

dolarhoy

Ruby
5
star
25

sequel-fusiontables

Fusion Tables adapter for Sequel
Ruby
5
star
26

cpa

A CLI tool to query Argentinean zip codes.
Ruby
5
star
27

lightspec

Generate RSpec User Stories stubs out of Lighthouse tickets tagged as 'story'.
4
star
28

rails_basic_helpers

A Ruby on Rails plugin providing some super simple helpers.
Ruby
4
star
29

redis-scripted

A Ruby client that supports the experimental scripting feature of Redis.
Ruby
4
star
30

node-mincsv

A minimal CSV parser for Node.js.
JavaScript
3
star
31

dis

Does it scale?
3
star
32

djanowski.github.com

3
star
33

snippets

Include snippets of content in your Rails views and let the client take it from there.
Ruby
3
star
34

validate_options

Validate your options hash.
Ruby
3
star
35

taskpaper2html

Converts a taskpaper file into HTML.
3
star
36

selenium-rc

Ruby
2
star
37

tti

Ruby (and Rails) text to image generation.
Ruby
2
star
38

par

Ruby
2
star
39

ft

Low-level interface to Fusion Tables + CLI tool
Ruby
2
star
40

minidesk

Minidesk will be a minimal help desk / ticketing system mounted on top of other services like GitHub and Google Apps.
2
star
41

webrat-selenium

Webrat's Selenium container.
1
star
42

foo

1
star
43

interwebz

Ruby
1
star
44

ohm-stats

Ruby
1
star
45

microajax

Microajax for Duo/Component.
JavaScript
1
star
46

aleph

Literate-programming HTML
Ruby
1
star
47

canonical-repo

Get redirected to a RubyGem's canonical repository!
Ruby
1
star