test_startup 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +33 -0
- data/lib/test_startup.rb +41 -0
- data/test/test_startup_test.rb +49 -0
- metadata +58 -0
data/README.markdown
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Test Startup
|
2
|
+
============
|
3
|
+
|
4
|
+
Adds test-case wide startup and shutdown hooks to test/unit (or shoulda). Here's an example:
|
5
|
+
|
6
|
+
require "test_startup"
|
7
|
+
|
8
|
+
class MyTest < Test::Unit::TestCase
|
9
|
+
|
10
|
+
startup do
|
11
|
+
start_some_server
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_should_access_server_properly
|
15
|
+
# whatever
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_should_do_other_servery_things
|
19
|
+
# also, whatever
|
20
|
+
end
|
21
|
+
|
22
|
+
shutdown do
|
23
|
+
stop_the_server
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
This avoids the overhead of starting the server for each and every test case.
|
29
|
+
|
30
|
+
|
31
|
+
Caveats
|
32
|
+
-------
|
33
|
+
You should probably think long and hard about whether or not you need this. In general, it's a good idea to have each test operate independently of each other, and this technique is only useful when it takes so long to setup the server (for example) that performing that for every test is not practical.
|
data/lib/test_startup.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
module TestStartupAndShutdown
|
4
|
+
def startup(&block)
|
5
|
+
install_global_startup
|
6
|
+
@__startup_blocks ||= []
|
7
|
+
@__startup_blocks << block if block_given?
|
8
|
+
@__startup_blocks
|
9
|
+
end
|
10
|
+
|
11
|
+
def shutdown(&block)
|
12
|
+
install_global_startup
|
13
|
+
@__shutdown_blocks ||= []
|
14
|
+
@__shutdown_blocks << block if block_given?
|
15
|
+
@__shutdown_blocks
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_reader :__startup_blocks, :__shutdown_blocks
|
19
|
+
|
20
|
+
def install_global_startup
|
21
|
+
extend(TestSuiteWithGlobalStartup)
|
22
|
+
end
|
23
|
+
|
24
|
+
module TestSuiteWithGlobalStartup
|
25
|
+
def suite(*args)
|
26
|
+
mysuite = super
|
27
|
+
these_startup_blocks = __startup_blocks
|
28
|
+
these_shutdown_blocks = __shutdown_blocks
|
29
|
+
mysuite.instance_eval { @__startup_blocks = these_startup_blocks }
|
30
|
+
mysuite.instance_eval { @__shutdown_blocks = these_shutdown_blocks }
|
31
|
+
def mysuite.run(*args)
|
32
|
+
@__startup_blocks.each { |block| block.call }
|
33
|
+
super
|
34
|
+
@__shutdown_blocks.each { |block| block.call }
|
35
|
+
end
|
36
|
+
mysuite
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
Test::Unit::TestCase.extend(TestStartupAndShutdown)
|
@@ -0,0 +1,49 @@
|
|
1
|
+
$:.push File.join(File.dirname(__FILE__), *%w[.. lib])
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'test/unit/ui/console/testrunner'
|
5
|
+
require 'test_startup'
|
6
|
+
|
7
|
+
class TestArtefact
|
8
|
+
class << self
|
9
|
+
attr_accessor :started, :stopped
|
10
|
+
end
|
11
|
+
|
12
|
+
self.started = 0
|
13
|
+
self.stopped = 0
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestStartupTest < Test::Unit::TestCase
|
17
|
+
startup { TestArtefact.started += 1 }
|
18
|
+
shutdown { TestArtefact.stopped += 1 }
|
19
|
+
|
20
|
+
def test_should_call_startup_callback
|
21
|
+
assert_equal 1, TestArtefact.started
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_should_only_call_startup_callback_once
|
25
|
+
assert_equal 1, TestArtefact.started
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_should_not_call_stopped_while_tests_are_running
|
29
|
+
assert_equal 0, TestArtefact.stopped
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class TestStartupExternalTest < Test::Unit::TestCase
|
34
|
+
def test_should_call_shutdown_when_test_case_tests_have_finished
|
35
|
+
assert_equal 1, TestArtefact.stopped
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class TestStartupSuite < Test::Unit::TestSuite
|
40
|
+
# taking advantage of the fact that the tests run in the order they're added here.
|
41
|
+
def self.suite
|
42
|
+
result = self.new(self.class.name)
|
43
|
+
result << TestStartupTest.suite
|
44
|
+
result << TestStartupExternalTest.suite
|
45
|
+
return result
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
Test::Unit::UI::Console::TestRunner.run(TestStartupSuite)
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: test_startup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Adam
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-23 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: james@lazyatom.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.markdown
|
24
|
+
files:
|
25
|
+
- README.markdown
|
26
|
+
- test/test_startup_test.rb
|
27
|
+
- lib/test_startup.rb
|
28
|
+
has_rdoc: true
|
29
|
+
homepage: http://lazyatom.com
|
30
|
+
licenses: []
|
31
|
+
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options:
|
34
|
+
- --main
|
35
|
+
- README.markdown
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project: test_startup
|
53
|
+
rubygems_version: 1.3.3
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Adds Ruby 1.9-style startup and shutdown mechanisms to test/unit (or shoulda)
|
57
|
+
test_files: []
|
58
|
+
|