@@ -75,4 +75,161 @@ req.end();
75
75
76
76
然而,在当前这种情况下,我知道我想要的功能,但是我并不理解其深层次的功能。我需要花费大量的时候来理解,它为什么是这样的,需要先有一些脚本来知道它是怎么工作的。TDD变显得很有价值,换句话来说,在现有的情况下,TDD对于我们不了解的一些事情,可以驱动出更多的开发。毕竟在我们完成测试脚本之后,我们也会发现这些测试脚本成为了代码的一部分。
77
77
78
- 在这种理想的情况下,我们为什么不TDD呢?
78
+ 在这种理想的情况下,我们为什么不TDD呢?
79
+
80
+
81
+ ##轻量级网站测试TWill
82
+
83
+ > twill was initially designed for testing Web sites, although since then people have also figured out that it's good for browsing unsuspecting Web sites.
84
+
85
+ 之所以说轻量的原因是他是拿命令行测试的,还有DSL,还有Python。
86
+
87
+ 除此之外,还可以拿它做压力测试,这种压力测试和一般的不一样。可以模拟整个过程,比如同时有多少人登陆你的网站。
88
+
89
+ 不过,它有一个限制是没有JavaScript。
90
+
91
+ 看了一下源码,大概原理就是用`` requests `` 下载html,接着用`` lxml `` 解析html,比较有意思的是内嵌了一个`` DSL `` 。
92
+
93
+ 这是一个Python的库。
94
+
95
+ pip install twill
96
+
97
+ ##Twill 登陆测试
98
+
99
+ 1.启动我们的应用。
100
+
101
+ 2.进入twill shell
102
+
103
+ twill-sh
104
+ -= Welcome to twill! =-
105
+ current page: *empty page*
106
+
107
+ 3.打开网页
108
+
109
+ >> go http://127.0.0.1:5000/login
110
+ ==> at http://127.0.0.1:5000/login
111
+ current page: http://127.0.0.1:5000/login
112
+
113
+ 4.显示表单
114
+
115
+ >> showforms
116
+
117
+ Form #1
118
+ ## ## __Name__________________ __Type___ __ID________ __Value__________________
119
+ 1 csrf_token hidden csrf_token 1423387196##5005bdf3496e09b8e2fbf450 ...
120
+ 2 email email email None
121
+ 3 password password password None
122
+ 4 login submit (None) 登入
123
+
124
+ current page: http://127.0.0.1:5000/login
125
+
126
+ 5.填充表单
127
+
128
+ formclear 1
129
+ fv 1 email test@tes.com
130
+ fv 1 password test
131
+
132
+ 6.修改action
133
+
134
+ formaction 1 http://127.0.0.1:5000/login
135
+
136
+ 7.提交表单
137
+
138
+ >> submit
139
+ Note: submit is using submit button: name="login", value="登入"
140
+ current page: http://127.0.0.1:5000/
141
+
142
+ 发现重定向到首页了。
143
+
144
+ ##Twill 测试脚本
145
+
146
+ 当然我们也可以用脚本直接来测试`` login.twill `` :
147
+
148
+ go http://127.0.0.1:5000/login
149
+
150
+ showforms
151
+ formclear 1
152
+ fv 1 email test@tes.com
153
+ fv 1 password test
154
+ formaction 1 http://127.0.0.1:5000/login
155
+ submit
156
+
157
+ go http://127.0.0.1:5000/logout
158
+
159
+ 运行
160
+
161
+ twill-sh login.twill
162
+
163
+ 结果
164
+
165
+ >> EXECUTING FILE login.twill
166
+ AT LINE: login.twill:0
167
+ ==> at http://127.0.0.1:5000/login
168
+ AT LINE: login.twill:2
169
+
170
+ Form #1
171
+ ## ## __Name__________________ __Type___ __ID________ __Value__________________
172
+ 1 csrf_token hidden csrf_token 1423387345##7a000b612fef39aceab5ca54 ...
173
+ 2 email email email None
174
+ 3 password password password None
175
+ 4 login submit (None) 登入
176
+
177
+ AT LINE: login.twill:3
178
+ AT LINE: login.twill:4
179
+ AT LINE: login.twill:5
180
+ AT LINE: login.twill:6
181
+ Setting action for form (<Element form at 0x10e7cbb50>,) to ('http://127.0.0.1:5000/login',)
182
+ AT LINE: login.twill:7
183
+ Note: submit is using submit button: name="login", value="登入"
184
+
185
+ AT LINE: login.twill:9
186
+ ==> at http://127.0.0.1:5000/login
187
+ --
188
+ 1 of 1 files SUCCEEDED.
189
+
190
+ 一个成功的测试诞生了。
191
+
192
+ ##Fake Server
193
+
194
+ 实践了一下怎么用sinon去fake server,还没用respondWith,于是写一下。
195
+
196
+ 这里需要用到sinon框架来测试。
197
+
198
+ 当我们fetch的时候,我们就可以返回我们想要fake的结果。
199
+
200
+ var data = {"id":1,"name":"Rice","type":"Good","price":12,"quantity":1,"description":"Made in China"};
201
+ beforeEach(function() {
202
+ this.server = sinon.fakeServer.create();
203
+ this.rices = new Rices();
204
+ this.server.respondWith(
205
+ "GET",
206
+ "http://localhost:8080/all/rice",
207
+ [
208
+ 200,
209
+ {"Content-Type": "application/json"},
210
+ JSON.stringify(data)
211
+ ]
212
+ );
213
+ });
214
+
215
+ 于是在afterEach的时候,我们需要恢复这个server。
216
+
217
+ afterEach(function() {
218
+ this.server.restore();
219
+ });
220
+
221
+ 接着写一个jasmine测试来测试
222
+
223
+ describe("Collection Test", function() {
224
+ it("should get data from the url", function() {
225
+ this.rices.fetch();
226
+ this.server.respond();
227
+ var result = JSON.parse(JSON.stringify(this.rices.models[0]));
228
+ expect(result["id"])
229
+ .toEqual(1);
230
+ expect(result["price"])
231
+ .toEqual(12);
232
+ expect(result)
233
+ .toEqual(data);
234
+ });
235
+ });
0 commit comments