micro_test 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,15 +2,14 @@
2
2
 
3
3
  ### Testing should be simple.
4
4
 
5
- It bothers me that testing frameworks never seem to resist
6
- the slippery slope of feature creep and eventually become an end unto themselves.
5
+ Testing frameworks never seem to resist the slippery slope of feature creep and eventually become an end unto themselves.
7
6
  MiniTest is a step in the right direction, but still feels bigger than it should.
8
7
 
9
8
  #### MicroTest is an experiment to see just how simple a testing "framework" can be.
10
9
 
11
10
  ## Features
12
11
 
13
- * __Opinionated & small__ - _only 70 lines of code_
12
+ * __Opinionated & small__ - _only 100 lines of code_
14
13
  * __Only one assertion: `assert`__ - _since this is the heart of testing_
15
14
  * __Tests run in random order__ - _to prevent the bad practice of run order depenencies_
16
15
  * __Plays nice with others__ - _easy to introduce to an existing codebase_
@@ -25,6 +24,8 @@ gem install micro_test
25
24
 
26
25
  * Tests subclass `MicroTest::Test`
27
26
  * Define tests with `test "description" do ...`
27
+ * Setup with `before [:all|:each] do ...`
28
+ * Teardown with `after [:all|:each] do ...`
28
29
  * Assert statements with `assert [statement]`
29
30
  * Run tests from the terminal with `$mt /path/to/test_file_or_dir` or simply `$mt`
30
31
  * Run tests from Ruby with `MicroTest::Runner.run`
@@ -39,6 +40,14 @@ Define a test.
39
40
  # /example/test/math_test.rb
40
41
  class MathTest < MicroTest::Test
41
42
 
43
+ before :all do
44
+ # runs once before all tests
45
+ end
46
+
47
+ before :each do
48
+ # runs before each test
49
+ end
50
+
42
51
  test "addition" do
43
52
  assert 2 + 2 == 4
44
53
  end
@@ -60,6 +69,14 @@ class MathTest < MicroTest::Test
60
69
  assert 2 + 2 == 5
61
70
  end
62
71
 
72
+ after :each do
73
+ # runs after each test
74
+ end
75
+
76
+ after :all do
77
+ # runs once after all tests
78
+ end
79
+
63
80
  end
64
81
  ```
65
82
 
@@ -0,0 +1,78 @@
1
+ module MicroTest
2
+ module Runner
3
+ class << self
4
+
5
+ def red(text)
6
+ "\e[31m#{text}\e[0m"
7
+ end
8
+
9
+ def green(text)
10
+ "\e[32m#{text}\e[0m"
11
+ end
12
+
13
+ def update(event, arg)
14
+ send event, arg
15
+ end
16
+
17
+ def add_test_class(klass)
18
+ test_classes << klass
19
+ end
20
+
21
+ def test_classes
22
+ @test_classes ||= []
23
+ end
24
+
25
+ def file(path)
26
+ @files ||= {}
27
+ @files[path] ||= File.open(path, "r").readlines.map { |l| l.gsub(/\n/, "") }
28
+ end
29
+
30
+ def file_info(callstack_entry)
31
+ path = callstack_entry[0, callstack_entry.index(/:[0-9]+:/)]
32
+ file = file(path)
33
+ line_num = callstack_entry.scan(/:[0-9]+:/).first.gsub(/:/, "").to_i
34
+ line = file[line_num - 1]
35
+ {
36
+ :path => path,
37
+ :file => file,
38
+ :line => line,
39
+ :line_num => line_num
40
+ }
41
+ end
42
+
43
+ def assert(value)
44
+ info = file_info(caller[6])
45
+ if value
46
+ puts " #{green :PASS}: #{info[:line]}"
47
+ @passed += 1
48
+ else
49
+ puts " #{red :FAIL}: #{red info[:line]}"
50
+ puts " #{info[:path]}:#{info[:line_num]}"
51
+ @failed += 1
52
+ end
53
+ end
54
+
55
+ def run
56
+ @passed = @failed = 0
57
+ @test_classes.shuffle.each do |test_class|
58
+ before = test_class.callbacks[:before]
59
+ after = test_class.callbacks[:after]
60
+
61
+ puts test_class.name
62
+ before[:all].call if before[:all]
63
+ test_class.tests.keys.shuffle.each do |desc|
64
+ before[:each].call if before[:each]
65
+ puts "- test #{desc}"
66
+ test_class.tests[desc].call
67
+ after[:each].call if after[:each]
68
+ end
69
+ after[:all].call if after[:all]
70
+ end
71
+ puts "---"
72
+ puts "Passed: #{green @passed}, Failed: #{red @failed}"
73
+ puts "---"
74
+ end
75
+
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,43 @@
1
+ require "observer"
2
+
3
+ module MicroTest
4
+ class Test
5
+ class << self
6
+ include Observable
7
+
8
+ def inherited(subclass)
9
+ notify(:add_test_class, subclass)
10
+ end
11
+
12
+ def notify(event, arg)
13
+ MicroTest::Test.send :changed
14
+ MicroTest::Test.send :notify_observers, event, arg
15
+ end
16
+
17
+ def callbacks
18
+ @callbacks ||= { :before => {}, :after => {} }
19
+ end
20
+
21
+ def before(what, &block)
22
+ callbacks[:before][what] = block
23
+ end
24
+
25
+ def after(what, &block)
26
+ callbacks[:after][what] = block
27
+ end
28
+
29
+ def tests
30
+ @tests ||= {}
31
+ end
32
+
33
+ def test(desc, &block)
34
+ tests[desc] = block
35
+ end
36
+
37
+ def assert(value)
38
+ notify(:assert, value)
39
+ end
40
+
41
+ end
42
+ end
43
+ end
data/lib/micro_test.rb CHANGED
@@ -1,84 +1,5 @@
1
- module MicroTest
2
- module Runner
1
+ path = File.expand_path(File.join(File.dirname(__FILE__), "micro_test"))
2
+ require File.join(path, "test")
3
+ require File.join(path, "runner")
3
4
 
4
- def self.red(text)
5
- "\e[31m#{text}\e[0m"
6
- end
7
-
8
- def self.green(text)
9
- "\e[32m#{text}\e[0m"
10
- end
11
-
12
- def self.test_classes
13
- @test_classes ||= []
14
- end
15
-
16
- def self.log(value)
17
- file_path = caller[1][0, caller[1].index(/:[0-9]+:/)]
18
- line_num = caller[1].scan(/:[0-9]+:/).first.gsub(/:/, "").to_i
19
- @files ||= {}
20
- @files[file_path] ||= File.open(file_path, "r").readlines.map { |l| l.gsub(/\n/, "") }
21
- line = @files[file_path][line_num - 1].strip
22
-
23
- if value
24
- puts " #{green :PASS}: #{line}"
25
- @passed += 1
26
- else
27
- puts " #{red :FAIL}: #{red line}"
28
- puts " #{file_path}:#{line_num}"
29
- @failed += 1
30
- end
31
- end
32
-
33
- def self.run
34
- @passed = @failed = 0
35
- test_classes.shuffle.each do |test_class|
36
- before = test_class.callbacks[:before]
37
- after = test_class.callbacks[:after]
38
-
39
- puts test_class.name
40
- before[:all].call if before[:all]
41
- test_class.tests.keys.shuffle.each do |desc|
42
- before[:each].call if before[:each]
43
- puts "- test #{desc}"
44
- test_class.tests[desc].call
45
- after[:each].call if after[:each]
46
- end
47
- after[:all].call if after[:all]
48
- end
49
- puts "---"
50
- puts "Passed: #{green @passed}, Failed: #{red @failed}"
51
- puts "---"
52
- end
53
- end
54
-
55
- class Test
56
- def self.inherited(subclass)
57
- MicroTest::Runner.test_classes << subclass
58
- end
59
-
60
- def self.callbacks
61
- @callbacks ||= { :before => {}, :after => {} }
62
- end
63
-
64
- def self.before(what, &block)
65
- callbacks[:before][what] = block
66
- end
67
-
68
- def self.after(what, &block)
69
- callbacks[:after][what] = block
70
- end
71
-
72
- def self.tests
73
- @tests ||= {}
74
- end
75
-
76
- def self.test(desc, &block)
77
- tests[desc] = block
78
- end
79
-
80
- def self.assert(value)
81
- MicroTest::Runner.log(value)
82
- end
83
- end
84
- end
5
+ MicroTest::Test.add_observer MicroTest::Runner
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: micro_test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -21,6 +21,8 @@ executables:
21
21
  extensions: []
22
22
  extra_rdoc_files: []
23
23
  files:
24
+ - lib/micro_test/runner.rb
25
+ - lib/micro_test/test.rb
24
26
  - lib/micro_test.rb
25
27
  - bin/mt
26
28
  - README.md