1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
## 1. First step of the `test-unit`
Let's getting start `test-unit`.
This document creates an example gem package called `sample` with the `test-unit` testing framework.
## 2. Install bundler and test-unit.
* First, install the `bundler` gem for generating gem template.
* Second, install the `test-unit` itself.
~~~
!!!plain
gem install bundler
gem install test-unit
~~~
The `gem list` command output installed packages.
You will find the following lines.
~~~
!!!plain
gem list
...
bundler (2.6.3, default: 2.6.2)
...
test-unit (3.6.8, 3.6.7)
~~~
## 3. Create gem template.
Next, create a gem template using `bundler` command.
This command generates package skeleton with a testing framework.
The `bundle gem -t test-unit sample` command will generate a gem template with the `test-unit` testing framework.
## 4. Execute test.
The `rake test` command execute test scenarios in the `test` directory.
Now it tries to two tests. One will success the other one fails.
~~~
!!!plain
$ rake test
Loaded suite /path/to/ruby/lib/ruby/gems/3.4.0/gems/rake-13.2.1/lib/rake/rake_test_loader
Started
F
================================================================================
Failure: test: something useful(SampleTest)
/path/to/sample/test/sample_test.rb:13:in 'block in <class:SampleTest>'
10: end
11:
12: test "something useful" do
=> 13: assert_equal("expected", "actual")
14: end
15: end
<"expected"> expected but was
<"actual">
diff:
? expected
? a ual
? ???? ??
================================================================================
Finished in 0.013737 seconds.
--------------------------------------------------------------------------------
2 tests, 2 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
50% passed
--------------------------------------------------------------------------------
145.59 tests/s, 145.59 assertions/s
rake aborted!
Command failed with status (1)
Tasks: TOP => test
(See full trace by running task with --trace)
~~~
## 5. Create original tests.
Let's create your original tests with the following rules.
* Create a test file in the `test` directory.
* The file needs suffix `xxx_test.rb`.
* You can put test file into the subdirectory like `test/sub`.
Example directory layout.
~~~
!!!plain
test
|-- sample_test.rb
|-- sub
| `-- sample2_test.rb
`-- test_helper.rb
~~~
Example test file in the sub directory.
~~~
!!!ruby
require 'test_helper'
module Sub
class Sample2Test < Test::Unit::TestCase
def test_that_it_has_a_version_number
refute_nil ::Sample::VERSION
end
def test_it_does_something_useful
assert false
end
end
end
~~~
## 6. For more information
Let's read the official document.
* [test-unit](https://test-unit.github.io/index.html)
|