itesttool 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +129 -111
- data/lib/itesttool/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -2,39 +2,50 @@
|
|
2
2
|
|
3
3
|
[](https://travis-ci.org/bati11/itesttool)
|
4
4
|
|
5
|
-
Web
|
6
|
-
RSpecにユーティリティを追加して実現してます。
|
5
|
+
itesttool is Web API end-to-end test tool in RSpec.
|
7
6
|
|
7
|
+
```ruby
|
8
|
+
describe 'send GET request' do
|
9
|
+
_given {
|
10
|
+
headers 'referer' => 'http://local.example.com',
|
11
|
+
}
|
12
|
+
_when { get 'http://localhost:4567/index', as_json }
|
13
|
+
_then {
|
14
|
+
res.code.should eq '200'
|
15
|
+
res.body.should eq_schema_of 'json_schema/hello.json'
|
16
|
+
res['$.members..name'].should eq ['Ichiro', 'Jiro', 'Saburo']
|
17
|
+
}
|
18
|
+
end
|
19
|
+
```
|
20
|
+
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
Add this line to your application's Gemfile
|
8
24
|
|
9
|
-
|
10
|
-
|
11
|
-
|
25
|
+
```ruby
|
26
|
+
gem 'rspec-parameterized'
|
27
|
+
```
|
28
|
+
|
29
|
+
And then execute
|
30
|
+
```sh
|
12
31
|
$ bundle install --path vendor/bundle
|
13
|
-
|
14
|
-
サンプルアプリケーションが起動します。
|
32
|
+
```
|
15
33
|
|
16
|
-
~~~~~ {sh}
|
17
|
-
$ bundle exec ruby sample/app.rb
|
18
|
-
~~~~~
|
19
34
|
|
20
|
-
|
35
|
+
# Usage
|
36
|
+
Require `itesttool`
|
21
37
|
|
22
|
-
|
23
|
-
|
24
|
-
|
38
|
+
```ruby
|
39
|
+
require 'itesttool'
|
40
|
+
```
|
25
41
|
|
26
|
-
テストが通れば準備完了です。
|
27
42
|
|
43
|
+
## GET request
|
44
|
+
* `_given` is setup phase. `_given` is alias of `before` in RSpec.
|
45
|
+
* `_when` is execution phase. it send HTTP request to server.
|
46
|
+
* `_then` is assertion phase. in `_then`block, HTTP response is set `res`. `res` is instance of Net::HTTPResponse.
|
28
47
|
|
29
|
-
|
30
|
-
## GET リクエストを送信する
|
31
|
-
GET リクエスト送って、レスポンスコードを確認するには以下のようにします。
|
32
|
-
`_given` ブロックでテストの前提条件を書きます。`_given`は、単純にRSpecの`before`の別名です。
|
33
|
-
`_when` ブロックでGETリクエストを送ります。
|
34
|
-
`_then` ブロック内でres変数を使うことでレスポンスにアクセスできます。
|
35
|
-
resは、Net::HTTPResponseオブジェクトです。
|
36
|
-
|
37
|
-
~~~~~ {ruby}
|
48
|
+
```ruby
|
38
49
|
describe 'send GET request' do
|
39
50
|
_given {
|
40
51
|
headers 'referer' => 'http://local.example.com',
|
@@ -44,30 +55,43 @@ describe 'send GET request' do
|
|
44
55
|
res.code.should eq '200'
|
45
56
|
}
|
46
57
|
end
|
47
|
-
|
58
|
+
```
|
48
59
|
|
49
|
-
## レスポンスを検証する
|
50
|
-
上の例にもありますが、ステータスコードは`res.code.should eq '200'`と書きます。
|
51
|
-
レスポンスボディは`res.body.should eq 'Hello world!'`というように書きます。
|
52
60
|
|
53
|
-
|
54
|
-
|
55
|
-
|
61
|
+
## asser HTTP response
|
62
|
+
assert status code:
|
63
|
+
```ruby
|
64
|
+
res.code.should eq '200'
|
65
|
+
```
|
56
66
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
67
|
+
assert response body:
|
68
|
+
```ruby
|
69
|
+
res.body.should eq 'Hello world!'
|
70
|
+
```
|
61
71
|
|
72
|
+
If respons body is the JSON, XML, HTML, you can assert an element using JSONPath, XPath, CSS Selectors.
|
73
|
+
About JSONPath and XPath, please refer to the site: [http://goessner.net/articles/JsonPath/](http://goessner.net/articles/JsonPath/)
|
74
|
+
|
75
|
+
For assert to the array, itesttool defines custom matchers.
|
62
76
|
- `all`
|
63
77
|
- `be_one_and`
|
64
78
|
- `be_sorted`
|
65
79
|
|
80
|
+
```ruby
|
81
|
+
[2, 3].should all be > 1 # same next code: [2, 3].each do |x| x.should be > 1 end
|
66
82
|
|
67
|
-
|
68
|
-
|
83
|
+
[2].should be_one_and be > 1 # success
|
84
|
+
[2, 3].should be_one_and be > 1 # failure
|
69
85
|
|
70
|
-
|
86
|
+
[1, 2, 3].should be_sorted :asc # success
|
87
|
+
[1, 2, 3].should be_sorted :desc # failure
|
88
|
+
[3, 2, 1].should be_sorted :desc # success
|
89
|
+
```
|
90
|
+
|
91
|
+
|
92
|
+
### JSON
|
93
|
+
with JSONPath:
|
94
|
+
```ruby
|
71
95
|
res['$.team'].should eq ['ABC']
|
72
96
|
res['$.members..name'].should eq ['Ichiro', 'Jiro', 'Saburo']
|
73
97
|
res['$.members..age'].should include 32
|
@@ -81,24 +105,26 @@ res['$.members..age'].should all be >= 12
|
|
81
105
|
res['$.members..age'].should all be < 33
|
82
106
|
res['$.members..age'].should all be <= 32
|
83
107
|
res['$.members..age'].should be_sorted :desc
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
member_ages.should all be <= 32
|
96
|
-
member_ages.should be_sorted :desc
|
108
|
+
```
|
109
|
+
|
110
|
+
with `select` helper method:
|
111
|
+
```ruby
|
112
|
+
res.select('$.members..age') do |ages|
|
113
|
+
ages.should all be_kind_of Integer
|
114
|
+
ages.should all be > 11
|
115
|
+
ages.should all be >= 12
|
116
|
+
ages.should all be < 33
|
117
|
+
ages.should all be <= 32
|
118
|
+
ages.should be_sorted :desc
|
97
119
|
end
|
98
|
-
|
120
|
+
```
|
99
121
|
|
100
|
-
|
101
|
-
|
122
|
+
you can assert with the JSON Schema.
|
123
|
+
```ruby
|
124
|
+
res.body.should eq_schema_of 'json_schema/hello.json'
|
125
|
+
```
|
126
|
+
|
127
|
+
json_schema/hello.js is JSON schema sample.
|
102
128
|
|
103
129
|
{
|
104
130
|
"type": "object",
|
@@ -117,17 +143,11 @@ json_schema/hello.js ファイルに以下のようなJSON schemaが記述しま
|
|
117
143
|
}
|
118
144
|
}
|
119
145
|
|
120
|
-
`_then`ブロックに以下のように記述することで、レスポンスボディが上記のJSON schemaとマッチするかを検証できます。
|
121
|
-
|
122
|
-
~~~~~ {ruby}
|
123
|
-
res.body.should eq_schema_of 'json_schema/hello.json'
|
124
|
-
~~~~~
|
125
146
|
|
126
147
|
|
127
148
|
### XML
|
128
|
-
XPath
|
129
|
-
|
130
|
-
~~~~~ {ruby}
|
149
|
+
with XPath:
|
150
|
+
```ruby
|
131
151
|
res['/root/team/text()'].should eq ['ABC']
|
132
152
|
res['/root/members//name/text()'].should eq ['Ichiro', 'Jiro', 'Saburo']
|
133
153
|
res['/root/members//age/text()'].should include '32'
|
@@ -135,19 +155,19 @@ res['/root/members/*'].should have(3).items
|
|
135
155
|
res['/root/members/*'].should have_at_most(3).items
|
136
156
|
res['/root/members/*'].should have_at_least(1).items
|
137
157
|
res['/root/members//age/text()'].should all be > "11"
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
158
|
+
res.select('/root/members//age/text()') do |ages|
|
159
|
+
ages.should all be >= "10"
|
160
|
+
ages.should all be < "33"
|
161
|
+
ages.should all be <= "32"
|
162
|
+
ages.should be_sorted :desc
|
163
|
+
end
|
143
164
|
res['/root/members/member/@order'].should be_sorted :asc
|
144
|
-
|
165
|
+
```
|
145
166
|
|
146
167
|
|
147
168
|
### HTML
|
148
|
-
CSS
|
149
|
-
|
150
|
-
~~~~~ {ruby}
|
169
|
+
with CSS Selector:
|
170
|
+
```ruby
|
151
171
|
res['title'].should eq ['Page Title!']
|
152
172
|
res['h1#team'].should eq ['ABC']
|
153
173
|
res['.member dd.name'].should eq ['Ichiro', 'Jiro', 'Saburo']
|
@@ -156,26 +176,25 @@ res['.member'].should have(3).items
|
|
156
176
|
res['.member'].should have_at_most(3).items
|
157
177
|
res['.member'].should have_at_least(1).items
|
158
178
|
res['.member dd.age'].should all be > "11"
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
~~~~~ {ruby}
|
179
|
+
res.select('.member dd.age') do |ages|
|
180
|
+
ages.should all be >= 10
|
181
|
+
ages.should all be < 33
|
182
|
+
ages.should all be <= 32
|
183
|
+
ages.should be_sorted :desc
|
184
|
+
end
|
185
|
+
```
|
186
|
+
|
187
|
+
|
188
|
+
## query parameter
|
189
|
+
```ruby
|
171
190
|
_when { get 'http://localhost:4567/index?night=true', as_text }
|
172
191
|
_then {
|
173
192
|
res.code.should eq '200'
|
174
193
|
}
|
175
|
-
|
194
|
+
```
|
176
195
|
|
177
|
-
|
178
|
-
|
196
|
+
with `query` helper function:
|
197
|
+
```ruby
|
179
198
|
_when {
|
180
199
|
get 'http://localhost:4567/index',
|
181
200
|
as_text,
|
@@ -185,13 +204,11 @@ _when {
|
|
185
204
|
_then {
|
186
205
|
res.code.should eq '200'
|
187
206
|
}
|
188
|
-
|
207
|
+
```
|
189
208
|
|
190
209
|
|
191
|
-
##
|
192
|
-
|
193
|
-
`headers`関数に、ハッシュを渡して設定します。
|
194
|
-
~~~~~ {ruby}
|
210
|
+
## HTTP request header
|
211
|
+
```ruby
|
195
212
|
_given {
|
196
213
|
headers 'referer' => 'http://local.example.com',
|
197
214
|
'user_agent' => 'itesttool'
|
@@ -200,17 +217,19 @@ _when { get 'http://localhost:4567/index.html', as_html }
|
|
200
217
|
_then {
|
201
218
|
res.code.should eq '200'
|
202
219
|
}
|
203
|
-
|
204
|
-
|
220
|
+
```
|
205
221
|
|
206
|
-
## POSTリクエストを送信する
|
207
|
-
POSTリクエストを送信する場合、`get`の代わりに`post`を使います。
|
208
|
-
第1引数に送信先URL、第2引数にリクエストボディ、第3引数にレスポンスのフォーマット、を指定します。
|
209
222
|
|
210
|
-
|
211
|
-
|
223
|
+
## POST request
|
224
|
+
```ruby
|
225
|
+
_when {
|
226
|
+
post 'http://localhost:4567/login',
|
227
|
+
}
|
228
|
+
```
|
212
229
|
|
213
|
-
|
230
|
+
### application/x-www-form-urlencoded
|
231
|
+
use `body_as_form`:
|
232
|
+
```ruby
|
214
233
|
_when {
|
215
234
|
post 'http://localhost:4567/login',
|
216
235
|
body_as_form('nickname' => 'admin',
|
@@ -221,12 +240,12 @@ _then {
|
|
221
240
|
res.code.should eq '200'
|
222
241
|
res['$.nickname'].should eq ['admin']
|
223
242
|
}
|
224
|
-
|
243
|
+
```
|
225
244
|
|
226
|
-
### JSON
|
227
|
-
リクエストボディに、JSONを設定する場合は、`body_as_json`を使用します。
|
228
245
|
|
229
|
-
|
246
|
+
### application/json
|
247
|
+
use `body_as_json`:
|
248
|
+
```ruby
|
230
249
|
_when {
|
231
250
|
post 'http://localhost:4567/echo',
|
232
251
|
body_as_json('name' => 'Shiro',
|
@@ -237,11 +256,10 @@ _then {
|
|
237
256
|
res.code.should eq '200'
|
238
257
|
res.body.should eq '{"name":"Shiro","age":2}'
|
239
258
|
}
|
240
|
-
|
259
|
+
```
|
241
260
|
|
242
|
-
|
243
|
-
|
244
|
-
~~~~~ {ruby}
|
261
|
+
use string:
|
262
|
+
```ruby
|
245
263
|
_when {
|
246
264
|
post 'http://localhost:4567/echo',
|
247
265
|
body('{"name":"Shiro","age":2}'),
|
@@ -251,11 +269,11 @@ _then {
|
|
251
269
|
res.code.should eq '200'
|
252
270
|
res.body.should eq '{"name":"Shiro","age":2}'
|
253
271
|
}
|
254
|
-
|
272
|
+
```
|
273
|
+
|
255
274
|
|
256
|
-
## PUT, DELETE
|
257
|
-
|
258
|
-
後は`post`の場合といっしょです。
|
275
|
+
## PUT, DELETE request
|
276
|
+
same as a `post`. instead of use `put`, or `delete`.
|
259
277
|
|
260
278
|
|
261
279
|
# License
|
data/lib/itesttool/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: itesttool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -168,7 +168,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
168
168
|
version: '0'
|
169
169
|
segments:
|
170
170
|
- 0
|
171
|
-
hash:
|
171
|
+
hash: -2774708177748211322
|
172
172
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
173
173
|
none: false
|
174
174
|
requirements:
|
@@ -177,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
177
177
|
version: '0'
|
178
178
|
segments:
|
179
179
|
- 0
|
180
|
-
hash:
|
180
|
+
hash: -2774708177748211322
|
181
181
|
requirements: []
|
182
182
|
rubyforge_project:
|
183
183
|
rubygems_version: 1.8.23
|