├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── Makefile ├── README.markdown ├── bin └── cutest ├── cutest.gemspec ├── lib └── cutest.rb └── test ├── assert.rb ├── assert_equal.rb ├── assert_raise.rb ├── fixtures ├── exception.rb ├── fail_custom_assertion.rb ├── fail_custom_message.rb ├── failure.rb ├── failure_in_loaded_file.rb ├── only_run_given_scope_name.rb ├── outside_block.rb └── success.rb ├── prepare.rb ├── run.rb ├── scopes.rb └── setup.rb /.gitignore: -------------------------------------------------------------------------------- 1 | /pkg 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 1.2.3 - 2015-12-04 2 | ================== 3 | 4 | * `assert_raise` now works with lower-level exceptions. 5 | 6 | * `assert` can now receive a custom failure message, which should help write 7 | better custom assertions. 8 | 9 | * `cutest -v` now exits after printing the version number. 10 | 11 | 1.2.2 - 2014-11-05 12 | ================== 13 | 14 | * `assert_raise` now returns the raised exception. 15 | 16 | * Use `-s` to run a single scope. 17 | 18 | 1.2.1 - 2013-08-14 19 | ================== 20 | 21 | * `cutest(1)` now exits with a non-zero status when a test fails. 22 | 23 | Previous versions 24 | ================= 25 | 26 | Check the commit list for earlier changes. 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 Damian Janowski & Michel Martens 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | TESTS=$(shell ls test/*.rb) 2 | 3 | test: 4 | ruby -W2 bin/cutest $(TESTS) 5 | 6 | .PHONY: test 7 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | Cutest 2 | ======= 3 | 4 | [![Gem Version](https://badge.fury.io/rb/cutest.svg)](https://badge.fury.io/rb/cutest) 5 | 6 | Forking tests. 7 | 8 | Description 9 | ----------- 10 | 11 | Each test file is run in a forked process to avoid shared state. Once a failure 12 | is found, you get a report detailing what failed and how to locate the error 13 | and the rest of the file is skipped. 14 | 15 | You can use the `scope` command around tests: it guarantees that no instance 16 | variables are shared between tests. 17 | 18 | There are two commands very similar in nature, but with a subtle difference that 19 | makes them easy to combine in order to satisfy different needs: `prepare` and 20 | `setup`. 21 | 22 | The `prepare` blocks are executed before each test. If you call `prepare` many 23 | times, each passed block is appended to an array. When the test is run, all 24 | those prepare blocks are executed in order. The result of the block is 25 | discarded, so it is only useful for preparing the environment (flushing the 26 | database, removing a directory, etc.). 27 | 28 | The `setup` block is executed before each test and the result is passed as a 29 | parameter to the `test` block. Unlike `prepare`, each definition of `setup` 30 | overrides the previous one. Even if you can declare instance variables and 31 | share them between tests, the recommended usage is to pass the result of the 32 | block as a parameter to the `test` blocks. 33 | 34 | The `test` method executes the passed block after running `prepare` and 35 | `setup`. This is where assertions must be declared. 36 | 37 | Three assertions are available: `assert`, that accepts a value and raises 38 | if it's false or nil; `assert_equal`, that raises if its arguments are not 39 | equal; and `assert_raise`, that executes a passed block and compares the raised 40 | exception to the expected one. In all cases, if the expectation is no met, an 41 | `AssertionFailed` exception is raised. 42 | 43 | You can customize the output of `assert` by providing a second argument with 44 | a string you want to get as an error report if the assertion is not fulfilled. 45 | This can also be used as a simple building block to build custom assertions. 46 | 47 | Usage 48 | ----- 49 | 50 | In your terminal: 51 | 52 | $ cutest test/*.rb 53 | 54 | In your tests: 55 | 56 | ````ruby 57 | setup do 58 | {:a => 23, :b => 43} 59 | end 60 | 61 | test "should receive the result of the setup block as a parameter" do |params| 62 | assert params == {:a => 23, :b => 43} 63 | end 64 | 65 | test "should evaluate the setup block before each test" do |params| 66 | params[:a] = nil 67 | end 68 | 69 | test "should preserve the original values from the setup" do |params| 70 | assert 23 == params[:a] 71 | end 72 | ```` 73 | 74 | An example working with a prepare block: 75 | ````ruby 76 | prepare do 77 | Ohm.flush 78 | end 79 | 80 | setup do 81 | Ohm.redis.get("foo") 82 | end 83 | 84 | test do |foo| 85 | assert foo.nil? 86 | end 87 | ```` 88 | 89 | And working with scopes: 90 | ````ruby 91 | setup do 92 | @foo = true 93 | end 94 | 95 | @bar = true 96 | 97 | scope do 98 | test "should not share instance variables" do |foo| 99 | assert !defined?(@foo) 100 | assert !defined?(@bar) 101 | assert foo == true 102 | end 103 | end 104 | ```` 105 | 106 | The tests in these two examples will pass. 107 | 108 | Unlike other testing frameworks, Cutest does not compile all the tests before 109 | running them. 110 | 111 | A simple example for adding a custom `empty` assertion: 112 | ````ruby 113 | def assert_empty(string) 114 | assert(string.empty?, "not empty") 115 | end 116 | 117 | test "failed custom assertion" do 118 | assert_empty "foo" 119 | end 120 | ```` 121 | 122 | Handling errors 123 | --------------- 124 | 125 | If you get an error when running the tests, this is what you will see: 126 | ````bash 127 | Exception: assert_equal 24, params[:a] # 24 != 23 128 | test/setup.rb +14 129 | ```` 130 | Running the build 131 | ----------------- 132 | 133 | Using Rake: 134 | ````ruby 135 | task :test do 136 | exec "cutest test/*.rb" 137 | end 138 | 139 | task :default => :test 140 | ```` 141 | 142 | Using Make: 143 | ````yml 144 | .PHONY: test 145 | 146 | test: 147 | cutest test/*.rb 148 | ```` 149 | Command-line interface 150 | ---------------------- 151 | 152 | The tool `cutest` accepts a list of files and sends them to `Cutest.run`. If 153 | you need to require a file or library before running the tests, as is the case 154 | with test helpers, use the `-r` flag: 155 | 156 | $ cutest -r ./test/helper.rb ./test/*_test.rb 157 | 158 | If you want to check which version you are running, try the `-v` flag. 159 | 160 | Installation 161 | ------------ 162 | 163 | $ gem install cutest 164 | 165 | License 166 | ------- 167 | 168 | Copyright (c) 2010 Damian Janowski and Michel Martens 169 | 170 | Permission is hereby granted, free of charge, to any person 171 | obtaining a copy of this software and associated documentation 172 | files (the "Software"), to deal in the Software without 173 | restriction, including without limitation the rights to use, 174 | copy, modify, merge, publish, distribute, sublicense, and/or sell 175 | copies of the Software, and to permit persons to whom the 176 | Software is furnished to do so, subject to the following 177 | conditions: 178 | 179 | The above copyright notice and this permission notice shall be 180 | included in all copies or substantial portions of the Software. 181 | 182 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 183 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 184 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 185 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 186 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 187 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 188 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 189 | OTHER DEALINGS IN THE SOFTWARE. 190 | -------------------------------------------------------------------------------- /bin/cutest: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | if ARGV.empty? 4 | puts "usage: cutest [-v] [-r lib] [-o test] [-s scope] file ..." 5 | exit 6 | end 7 | 8 | require_relative "../lib/cutest" 9 | require "clap" 10 | 11 | files = Clap.run ARGV, 12 | "-r" => lambda { |file| require file }, 13 | "-o" => lambda { |name| cutest[:only] = name }, 14 | "-s" => lambda { |name| cutest[:scope] = name }, 15 | "-v" => lambda { puts Cutest::VERSION; exit } 16 | 17 | if files.any? 18 | success = Cutest.run(Dir[*files]) 19 | 20 | exit(1) unless success 21 | end 22 | -------------------------------------------------------------------------------- /cutest.gemspec: -------------------------------------------------------------------------------- 1 | require "./lib/cutest" 2 | 3 | Gem::Specification.new do |s| 4 | s.name = "cutest" 5 | s.version = Cutest::VERSION 6 | s.summary = "Forking tests." 7 | s.description = "Run tests in separate processes to avoid shared state." 8 | s.authors = ["Damian Janowski", "Michel Martens", "Cyril David"] 9 | s.email = ["djanowski@dimaion.com", "michel@soveran.com", "me@cyrildavid.com"] 10 | s.homepage = "https://github.com/djanowski/cutest" 11 | 12 | s.license = "MIT" 13 | 14 | s.files = `git ls-files`.split("\n") 15 | 16 | s.executables.push "cutest" 17 | 18 | s.add_dependency "clap" 19 | end 20 | -------------------------------------------------------------------------------- /lib/cutest.rb: -------------------------------------------------------------------------------- 1 | class Cutest 2 | unless defined?(VERSION) 3 | VERSION = "1.2.3" 4 | FILTER = %r[/(ruby|jruby|rbx)[-/]([0-9\.])+] 5 | CACHE = Hash.new { |h, k| h[k] = File.readlines(k) } 6 | end 7 | 8 | def self.run(files) 9 | status = files.all? do |file| 10 | run_file(file) 11 | 12 | Process.wait2.last.success? 13 | end 14 | 15 | puts 16 | 17 | status 18 | end 19 | 20 | def self.run_file(file) 21 | fork do 22 | begin 23 | load(file) 24 | 25 | rescue LoadError, SyntaxError 26 | display_error 27 | exit 1 28 | 29 | rescue StandardError 30 | trace = $!.backtrace 31 | pivot = trace.index { |line| line.match(file) } 32 | 33 | puts "\n test: %s" % cutest[:test] 34 | 35 | if pivot 36 | other = trace[0..pivot].select { |line| line !~ FILTER } 37 | other.reverse.each { |line| display_trace(line) } 38 | else 39 | display_trace(trace.first) 40 | end 41 | 42 | display_error 43 | 44 | exit 1 45 | end 46 | end 47 | end 48 | 49 | def self.code(fn, ln) 50 | begin 51 | CACHE[fn][ln.to_i - 1].strip 52 | rescue 53 | "(Can't display line)" 54 | end 55 | end 56 | 57 | def self.display_error 58 | print "\n#{$!.class}: " 59 | print "#{$!.message}\n" 60 | end 61 | 62 | def self.display_trace(line) 63 | fn, ln = line.split(":") 64 | 65 | puts " line: #{code(fn, ln)}" 66 | puts " file: #{fn} +#{ln}" 67 | end 68 | 69 | class AssertionFailed < StandardError 70 | end 71 | 72 | class Scope 73 | def initialize(&scope) 74 | @scope = scope 75 | end 76 | 77 | def call 78 | instance_eval(&@scope) 79 | end 80 | end 81 | end 82 | 83 | module Kernel 84 | private 85 | 86 | # Use Thread.current[:cutest] to store information about test preparation 87 | # and setup. 88 | Thread.current[:cutest] ||= { :prepare => [] } 89 | 90 | # Shortcut to access Thread.current[:cutest]. 91 | def cutest 92 | Thread.current[:cutest] 93 | end 94 | 95 | # Create an instance where the block will be evaluated. Recommended to improve 96 | # isolation between tests. 97 | def scope(name = nil, &block) 98 | if !cutest[:scope] || cutest[:scope] == name 99 | Cutest::Scope.new(&block).call 100 | end 101 | end 102 | 103 | # Prepare the environment in order to run the tests. This method can be 104 | # called many times, and each new block is appended to a list of 105 | # preparation blocks. When a test is executed, all the preparation blocks 106 | # are ran in the order they were declared. If called without a block, it 107 | # returns the array of preparation blocks. 108 | def prepare(&block) 109 | cutest[:prepare] << block if block_given? 110 | cutest[:prepare] 111 | end 112 | 113 | # Setup parameters for the tests. The block passed to setup is evaluated 114 | # before running each test, and the result of the setup block is passed to 115 | # the test as a parameter. If the setup and the tests are declared at the 116 | # same level (in the global scope or in a sub scope), it is possible to use 117 | # instance variables, but the parameter passing pattern is recommended to 118 | # ensure there are no side effects. 119 | # 120 | # If the setup blocks are declared in the global scope and the tests are 121 | # declared in sub scopes, the parameter passing usage is required. 122 | # 123 | # Setup blocks can be defined many times, but each new definition overrides 124 | # the previous one. It is recommended to split the tests in many different 125 | # files (the report is per file, not per assertion). Usually one setup 126 | # block per file is enough, but nothing forbids having different scopes 127 | # with different setup blocks. 128 | def setup(&block) 129 | cutest[:setup] = block if block_given? 130 | cutest[:setup] 131 | end 132 | 133 | # Kernel includes a test method for performing tests on files. 134 | undef test if defined? test 135 | 136 | # Call the prepare and setup blocks before executing the test. Even 137 | # though the assertions can live anywhere (it's not mandatory to put them 138 | # inside test blocks), it is necessary to wrap them in test blocks in order 139 | # to execute preparation and setup blocks. 140 | def test(name = nil, &block) 141 | cutest[:test] = name 142 | 143 | if !cutest[:only] || cutest[:only] == name 144 | prepare.each { |blk| blk.call } 145 | block.call(setup && setup.call) 146 | end 147 | 148 | cutest[:test] = nil 149 | end 150 | 151 | # Assert that value is not nil or false. 152 | def assert(value, msg = "expression returned #{value.inspect}") 153 | flunk(msg) unless value 154 | success 155 | end 156 | 157 | # Assert that actual and expected values are equal. 158 | def assert_equal(actual, expected) 159 | assert(actual == expected, "#{actual.inspect} != #{expected.inspect}") 160 | end 161 | 162 | # Assert that the block raises an expected exception. 163 | def assert_raise(expected = Exception) 164 | begin 165 | yield 166 | rescue expected => exception 167 | exception 168 | ensure 169 | assert(exception.kind_of?(expected), "got #{exception.inspect} instead") 170 | end 171 | end 172 | 173 | # Stop the tests and raise an error where the message is the last line 174 | # executed before flunking. 175 | def flunk(message = nil) 176 | backtrace = caller.find { |line| line.include? 'top (required)' } 177 | exception = Cutest::AssertionFailed.new(message) 178 | exception.set_backtrace(backtrace) 179 | 180 | raise exception 181 | end 182 | 183 | # Executed when an assertion succeeds. 184 | def success 185 | print "." 186 | end 187 | end 188 | -------------------------------------------------------------------------------- /test/assert.rb: -------------------------------------------------------------------------------- 1 | test "succeeds if the value is true" do 2 | assert true 3 | end 4 | 5 | test "raises if the assertion fails" do 6 | assert_raise(Cutest::AssertionFailed) do 7 | assert false 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /test/assert_equal.rb: -------------------------------------------------------------------------------- 1 | test do 2 | assert_equal 1, 1 3 | end 4 | 5 | test "raises if the assertion fails" do 6 | assert_raise(Cutest::AssertionFailed) do 7 | assert_equal 1, 2 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /test/assert_raise.rb: -------------------------------------------------------------------------------- 1 | test "catches default exception" do 2 | assert_raise do 3 | raise 4 | end 5 | end 6 | 7 | test "catches the right exception" do 8 | assert_raise(RuntimeError) do 9 | raise RuntimeError 10 | end 11 | end 12 | 13 | test "catches exceptions lower than StandardError" do 14 | assert_raise(NotImplementedError) do 15 | raise NotImplementedError 16 | end 17 | end 18 | 19 | test "raises if nothing raised" do 20 | assert_raise(Cutest::AssertionFailed) do 21 | assert_raise {} 22 | end 23 | end 24 | 25 | test "raises if the expectation is not met" do 26 | assert_raise(Cutest::AssertionFailed) do 27 | assert_raise(RuntimeError) do 28 | raise ArgumentError 29 | end 30 | end 31 | end 32 | 33 | test "returns the exception" do 34 | exception = assert_raise(RuntimeError) do 35 | raise RuntimeError, "error" 36 | end 37 | 38 | assert_equal "error", exception.message 39 | end 40 | 41 | test "catches a custom exception" do 42 | assert_raise do 43 | raise Class.new(Exception) 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /test/fixtures/exception.rb: -------------------------------------------------------------------------------- 1 | def foo 2 | raise "Oops" 3 | end 4 | 5 | test "some unhandled exception" do 6 | foo 7 | end 8 | -------------------------------------------------------------------------------- /test/fixtures/fail_custom_assertion.rb: -------------------------------------------------------------------------------- 1 | def assert_empty(string) 2 | assert(string.empty?, "not empty") 3 | end 4 | 5 | test "failed custom assertion" do 6 | assert_empty "foo" 7 | end 8 | -------------------------------------------------------------------------------- /test/fixtures/fail_custom_message.rb: -------------------------------------------------------------------------------- 1 | test "failed with custom message" do 2 | assert("hello".empty?, "not empty") 3 | end 4 | -------------------------------------------------------------------------------- /test/fixtures/failure.rb: -------------------------------------------------------------------------------- 1 | test "failed assertion" do 2 | assert false 3 | end 4 | -------------------------------------------------------------------------------- /test/fixtures/failure_in_loaded_file.rb: -------------------------------------------------------------------------------- 1 | load("test/fixtures/failure.rb") 2 | -------------------------------------------------------------------------------- /test/fixtures/only_run_given_scope_name.rb: -------------------------------------------------------------------------------- 1 | scope "another scope" do 2 | test do 3 | raise "This is not raised" 4 | end 5 | end 6 | 7 | scope "scope" do 8 | test "test" do 9 | assert true 10 | end 11 | 12 | test do 13 | raise "This is raised" 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /test/fixtures/outside_block.rb: -------------------------------------------------------------------------------- 1 | test "named success" do 2 | assert true 3 | end 4 | 5 | assert false 6 | -------------------------------------------------------------------------------- /test/fixtures/success.rb: -------------------------------------------------------------------------------- 1 | def foo 2 | raise "Invalid code" 3 | end 4 | 5 | test "external exceptions" do 6 | assert true 7 | end 8 | -------------------------------------------------------------------------------- /test/prepare.rb: -------------------------------------------------------------------------------- 1 | prepare do 2 | $foo = [] 3 | end 4 | 5 | prepare do 6 | $foo << true 7 | end 8 | 9 | test "all the prepare blocks are called" do 10 | assert $foo == [true] 11 | end 12 | 13 | prepare do 14 | $foo << false 15 | end 16 | 17 | test "and are cumulative" do 18 | assert $foo == [true, false] 19 | end 20 | 21 | scope do 22 | test "and run inside scopes" do 23 | assert $foo = [true, false] 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /test/run.rb: -------------------------------------------------------------------------------- 1 | test "output of successful run" do 2 | expected = ".\n" 3 | 4 | out = %x{./bin/cutest test/fixtures/success.rb} 5 | 6 | assert_equal(expected, out) 7 | end 8 | 9 | test "exit code of successful run" do 10 | %x{./bin/cutest test/fixtures/success.rb} 11 | assert_equal 0, $?.to_i 12 | end 13 | 14 | test "output of failed run" do 15 | expected = "\n" + 16 | " test: failed assertion\n" + 17 | " line: assert false\n" + 18 | " file: test/fixtures/failure.rb +2\n\n" + 19 | "Cutest::AssertionFailed: expression returned false\n\n" 20 | 21 | out = %x{./bin/cutest test/fixtures/failure.rb} 22 | 23 | assert_equal(expected, out) 24 | end 25 | 26 | test "output of failed run" do 27 | expected = "\n" + 28 | " test: some unhandled exception\n" + 29 | " line: raise \"Oops\"\n" + 30 | " file: test/fixtures/exception.rb +2\n\n" + 31 | "RuntimeError: Oops\n\n" 32 | 33 | out = %x{./bin/cutest test/fixtures/exception.rb} 34 | 35 | assert_equal(expected, out) 36 | end 37 | 38 | test "exit code of failed run" do 39 | %x{./bin/cutest test/fixtures/failure.rb} 40 | 41 | assert $?.to_i != 0 42 | end 43 | 44 | test "output of an assertion with custom message" do 45 | expected = "\n" + 46 | " test: failed with custom message\n" + 47 | " line: assert(\"hello\".empty?, \"not empty\")\n" + 48 | " file: test/fixtures/fail_custom_message.rb +2\n\n" + 49 | "Cutest::AssertionFailed: not empty\n\n" 50 | 51 | out = %x{./bin/cutest test/fixtures/fail_custom_message.rb} 52 | 53 | assert_equal(expected, out) 54 | end 55 | 56 | test "output of custom assertion" do 57 | expected = "\n" + 58 | " test: failed custom assertion\n" + 59 | " line: assert_empty \"foo\"\n" + 60 | " file: test/fixtures/fail_custom_assertion.rb +6\n\n" + 61 | "Cutest::AssertionFailed: not empty\n\n" 62 | 63 | out = %x{./bin/cutest test/fixtures/fail_custom_assertion.rb} 64 | 65 | assert_equal(expected, out) 66 | end 67 | 68 | test "output of failure in nested file" do 69 | expected = "\n" + 70 | " test: failed assertion\n" + 71 | " line: assert false\n" + 72 | " file: test/fixtures/failure.rb +2\n\n" + 73 | "Cutest::AssertionFailed: expression returned false\n\n" 74 | 75 | out = %x{./bin/cutest test/fixtures/failure_in_loaded_file.rb} 76 | 77 | assert_equal(expected, out) 78 | end 79 | 80 | test "output of failure outside block" do 81 | expected = ".\n" + 82 | " test: \n" + 83 | " line: assert false\n" + 84 | " file: test/fixtures/outside_block.rb +5\n\n" + 85 | "Cutest::AssertionFailed: expression returned false\n\n" 86 | 87 | out = %x{./bin/cutest test/fixtures/outside_block.rb} 88 | 89 | assert_equal(expected, out) 90 | end 91 | 92 | test "only runs given scope name" do 93 | out = %x{./bin/cutest test/fixtures/only_run_given_scope_name.rb -s scope} 94 | 95 | assert out =~ /This is raised/ 96 | end 97 | 98 | test "runs by given scope and test names" do 99 | %x{./bin/cutest test/fixtures/only_run_given_scope_name.rb -s scope -o test} 100 | 101 | assert_equal 0, $?.to_i 102 | end 103 | 104 | test "only prints the version" do 105 | expected = "#{Cutest::VERSION}\n" 106 | 107 | out = %x{./bin/cutest test/fixtures/success.rb -v} 108 | 109 | assert_equal(expected, out) 110 | end 111 | -------------------------------------------------------------------------------- /test/scopes.rb: -------------------------------------------------------------------------------- 1 | @bar = true 2 | 3 | scope do 4 | @foo = true 5 | 6 | test "something" do 7 | assert defined?(@foo) 8 | assert !defined?(@bar) 9 | end 10 | end 11 | 12 | scope do 13 | test "something" do 14 | assert !defined?(@foo) 15 | assert !defined?(@bar) 16 | end 17 | end 18 | 19 | scope do 20 | @baz = true 21 | 22 | scope do 23 | test "something" do 24 | assert !defined?(@baz) 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /test/setup.rb: -------------------------------------------------------------------------------- 1 | setup do 2 | {:a => 23, :b => 43} 3 | end 4 | 5 | test "should receive the result of the setup block as a parameter" do |params| 6 | assert params == {:a => 23, :b => 43} 7 | end 8 | 9 | test "if the params are modified..." do |params| 10 | params[:a] = nil 11 | end 12 | 13 | test "...it should preserve the original values from the setup" do |params| 14 | assert_equal 23, params[:a] 15 | end 16 | 17 | setup do 18 | "Hello world!" 19 | end 20 | 21 | test "only the most recently defined setup block is executed" do |value| 22 | assert "Hello world!" == value 23 | end 24 | 25 | scope do 26 | test "works inside scopes too" do |value| 27 | assert "Hello world!" == value 28 | end 29 | end 30 | --------------------------------------------------------------------------------