param_test 0.0.1beta → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.md +7 -0
- data/README.md +67 -3
- data/param_test.gemspec +1 -1
- data/test/param_test_test.rb +9 -0
- metadata +5 -4
data/LICENSE.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2013 Nik Haldimann
|
2
|
+
|
3
|
+
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:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
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.
|
data/README.md
CHANGED
@@ -1,4 +1,68 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
param_test
|
2
|
+
==========
|
3
3
|
|
4
|
-
Parametrized unit tests for Ruby/ActiveSupport
|
4
|
+
**Parametrized unit tests for Ruby/ActiveSupport**
|
5
|
+
|
6
|
+
This gem extends `ActiveSupport::TestCase` with a simple `param_test` class method that generates multiple tests with different parameters from a single code block.
|
7
|
+
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
In unit tests, you'll often want to run a set of the same assertions on multiple inputs. One way to do this is by looping over the inputs, for example:
|
12
|
+
|
13
|
+
class StringTest < ActiveSupport::TestCase
|
14
|
+
|
15
|
+
test "strings are ASCII only" do
|
16
|
+
["foo", "bar", "baz"].each do |string|
|
17
|
+
# Bad programmer! Don't do this
|
18
|
+
assert string.ascii_only?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
**Don't do this!** This is almost always a bad idea. If this test fails you won't know at which of the 3 input parameters it failed. A failure will also prevent subsequent inputs from getting tested, masking other failures that you won't discover until later.
|
25
|
+
|
26
|
+
`param_test` addresses these problems. The above test should be rewritten like this:
|
27
|
+
|
28
|
+
require 'param_test'
|
29
|
+
|
30
|
+
class StringTest < ActiveSupport::TestCase
|
31
|
+
|
32
|
+
param_test "string %s is ASCII only",
|
33
|
+
["foo", "bar", "baz"] do |string|
|
34
|
+
assert string.ascii_only?
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
This will generate three separate tests, one for each of the three parameters (`"foo"`, `"bar"` and `"baz"`) passed in the second argument. Each test is independent and can fail independently.
|
40
|
+
|
41
|
+
The tests will be named after the description template passed as the first argument. Standard Ruby string formatting is used to substitute the parameters into the description. Generally you'll want to just use string substitution with `%s`.
|
42
|
+
|
43
|
+
You can have multiple parameters per test:
|
44
|
+
|
45
|
+
param_test "%s is uppercase %s",
|
46
|
+
[["FOO", "foo"], ["BAR", "bar"]] do |expected, param|
|
47
|
+
assert_equal expected, param.upcase
|
48
|
+
end
|
49
|
+
|
50
|
+
Each parameter set must have the same number of parameters. The description template must have a corresponding number of substitutions.
|
51
|
+
|
52
|
+
### About Test Naming
|
53
|
+
|
54
|
+
Ruby's underlying unit test framework requires tests to be defined as methods beginning with `test`, so the description is actually converted to a valid method name. In the last example, the methods generated are `:test_FOO_is_uppercase_foo` and `:test_BAR_is_uppercase_bar`.
|
55
|
+
|
56
|
+
If string substitution would create the same method names for two different sets of parameters (this can happen because for example any all-whitespace string converts to the same single underscore), a counting variable will be added to the method name to keep them unique.
|
57
|
+
|
58
|
+
For better readability, any `nil` parameters will be string substituted as a `"nil"` string (the default string substitution for `nil` values is the empty string).
|
59
|
+
|
60
|
+
|
61
|
+
## Installation
|
62
|
+
|
63
|
+
gem install param_test
|
64
|
+
|
65
|
+
|
66
|
+
## License
|
67
|
+
|
68
|
+
Distributed under an [MIT license](https://github.com/nikhaldi/param-test-ruby/blob/master/LICENSE.md).
|
data/param_test.gemspec
CHANGED
data/test/param_test_test.rb
CHANGED
@@ -22,4 +22,13 @@ class ParamTestTest < ActiveSupport::TestCase
|
|
22
22
|
[["FOO", "foo"], ["BAR", "bar"]] do |expected, param|
|
23
23
|
assert_equal expected, param.upcase
|
24
24
|
end
|
25
|
+
|
26
|
+
# Example from README.md
|
27
|
+
test "strings are ASCII only" do
|
28
|
+
["foo", "bar", "baz"].each do |string|
|
29
|
+
# Bad programmer! Don't do this
|
30
|
+
assert string.ascii_only?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
25
34
|
end
|
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: param_test
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Nik Haldimann
|
@@ -52,6 +52,7 @@ extra_rdoc_files: []
|
|
52
52
|
files:
|
53
53
|
- .gitignore
|
54
54
|
- Gemfile
|
55
|
+
- LICENSE.md
|
55
56
|
- README.md
|
56
57
|
- Rakefile
|
57
58
|
- lib/active_support/testing/parametrized.rb
|
@@ -75,9 +76,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
76
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
77
|
none: false
|
77
78
|
requirements:
|
78
|
-
- - ! '
|
79
|
+
- - ! '>='
|
79
80
|
- !ruby/object:Gem::Version
|
80
|
-
version:
|
81
|
+
version: '0'
|
81
82
|
requirements: []
|
82
83
|
rubyforge_project:
|
83
84
|
rubygems_version: 1.8.24
|