Add basic test

This commit is contained in:
Fengda HUANG 2015-10-23 23:51:23 +08:00
parent f8e2c85bf2
commit 65f4541b70

View file

@ -75,4 +75,161 @@ req.end();
然而在当前这种情况下我知道我想要的功能但是我并不理解其深层次的功能。我需要花费大量的时候来理解它为什么是这样的需要先有一些脚本来知道它是怎么工作的。TDD变显得很有价值换句话来说在现有的情况下TDD对于我们不了解的一些事情可以驱动出更多的开发。毕竟在我们完成测试脚本之后我们也会发现这些测试脚本成为了代码的一部分。
在这种理想的情况下我们为什么不TDD呢?
在这种理想的情况下我们为什么不TDD呢?
##轻量级网站测试TWill
> 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.
之所以说轻量的原因是他是拿命令行测试的还有DSL还有Python。
除此之外,还可以拿它做压力测试,这种压力测试和一般的不一样。可以模拟整个过程,比如同时有多少人登陆你的网站。
不过它有一个限制是没有JavaScript。
看了一下源码,大概原理就是用``requests``下载html接着用``lxml``解析html比较有意思的是内嵌了一个``DSL``。
这是一个Python的库。
pip install twill
##Twill 登陆测试
1.启动我们的应用。
2.进入twill shell
twill-sh
-= Welcome to twill! =-
current page: *empty page*
3.打开网页
>> go http://127.0.0.1:5000/login
==> at http://127.0.0.1:5000/login
current page: http://127.0.0.1:5000/login
4.显示表单
>> showforms
Form #1
## ## __Name__________________ __Type___ __ID________ __Value__________________
1 csrf_token hidden csrf_token 1423387196##5005bdf3496e09b8e2fbf450 ...
2 email email email None
3 password password password None
4 login submit (None) 登入
current page: http://127.0.0.1:5000/login
5.填充表单
formclear 1
fv 1 email test@tes.com
fv 1 password test
6.修改action
formaction 1 http://127.0.0.1:5000/login
7.提交表单
>> submit
Note: submit is using submit button: name="login", value="登入"
current page: http://127.0.0.1:5000/
发现重定向到首页了。
##Twill 测试脚本
当然我们也可以用脚本直接来测试``login.twill``:
go http://127.0.0.1:5000/login
showforms
formclear 1
fv 1 email test@tes.com
fv 1 password test
formaction 1 http://127.0.0.1:5000/login
submit
go http://127.0.0.1:5000/logout
运行
twill-sh login.twill
结果
>> EXECUTING FILE login.twill
AT LINE: login.twill:0
==> at http://127.0.0.1:5000/login
AT LINE: login.twill:2
Form #1
## ## __Name__________________ __Type___ __ID________ __Value__________________
1 csrf_token hidden csrf_token 1423387345##7a000b612fef39aceab5ca54 ...
2 email email email None
3 password password password None
4 login submit (None) 登入
AT LINE: login.twill:3
AT LINE: login.twill:4
AT LINE: login.twill:5
AT LINE: login.twill:6
Setting action for form (<Element form at 0x10e7cbb50>,) to ('http://127.0.0.1:5000/login',)
AT LINE: login.twill:7
Note: submit is using submit button: name="login", value="登入"
AT LINE: login.twill:9
==> at http://127.0.0.1:5000/login
--
1 of 1 files SUCCEEDED.
一个成功的测试诞生了。
##Fake Server
实践了一下怎么用sinon去fake server还没用respondWith于是写一下。
这里需要用到sinon框架来测试。
当我们fetch的时候我们就可以返回我们想要fake的结果。
var data = {"id":1,"name":"Rice","type":"Good","price":12,"quantity":1,"description":"Made in China"};
beforeEach(function() {
this.server = sinon.fakeServer.create();
this.rices = new Rices();
this.server.respondWith(
"GET",
"http://localhost:8080/all/rice",
[
200,
{"Content-Type": "application/json"},
JSON.stringify(data)
]
);
});
于是在afterEach的时候我们需要恢复这个server。
afterEach(function() {
this.server.restore();
});
接着写一个jasmine测试来测试
describe("Collection Test", function() {
it("should get data from the url", function() {
this.rices.fetch();
this.server.respond();
var result = JSON.parse(JSON.stringify(this.rices.models[0]));
expect(result["id"])
.toEqual(1);
expect(result["price"])
.toEqual(12);
expect(result)
.toEqual(data);
});
});