功能测试

轻量级网站测试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的库。

  1. pip install twill

Twill 登陆测试

1.启动我们的应用。

2.进入twill shell

  1. twill-sh
  2. -= Welcome to twill! =-
  3. current page: *empty page*

3.打开网页

  1. >> go http://127.0.0.1:5000/login
  2. ==> at http://127.0.0.1:5000/login
  3. current page: http://127.0.0.1:5000/login

4.显示表单

  1. >> showforms
  2. Form #1
  3. ## ## __Name__________________ __Type___ __ID________ __Value__________________
  4. 1 csrf_token hidden csrf_token 1423387196##5005bdf3496e09b8e2fbf450 ...
  5. 2 email email email None
  6. 3 password password password None
  7. 4 login submit (None) 登入
  8. current page: http://127.0.0.1:5000/login

5.填充表单

  1. formclear 1
  2. fv 1 email test@tes.com
  3. fv 1 password test

6.修改action

  1. formaction 1 http://127.0.0.1:5000/login

7.提交表单

  1. >> submit
  2. Note: submit is using submit button: name="login", value="登入"
  3. current page: http://127.0.0.1:5000/

发现重定向到首页了。

Twill 测试脚本

当然我们也可以用脚本直接来测试login.twill:

  1. go http://127.0.0.1:5000/login
  2. showforms
  3. formclear 1
  4. fv 1 email test@tes.com
  5. fv 1 password test
  6. formaction 1 http://127.0.0.1:5000/login
  7. submit
  8. go http://127.0.0.1:5000/logout

运行

  1. twill-sh login.twill

结果

  1. >> EXECUTING FILE login.twill
  2. AT LINE: login.twill:0
  3. ==> at http://127.0.0.1:5000/login
  4. AT LINE: login.twill:2
  5. Form #1
  6. ## ## __Name__________________ __Type___ __ID________ __Value__________________
  7. 1 csrf_token hidden csrf_token 1423387345##7a000b612fef39aceab5ca54 ...
  8. 2 email email email None
  9. 3 password password password None
  10. 4 login submit (None) 登入
  11. AT LINE: login.twill:3
  12. AT LINE: login.twill:4
  13. AT LINE: login.twill:5
  14. AT LINE: login.twill:6
  15. Setting action for form (<Element form at 0x10e7cbb50>,) to ('http://127.0.0.1:5000/login',)
  16. AT LINE: login.twill:7
  17. Note: submit is using submit button: name="login", value="登入"
  18. AT LINE: login.twill:9
  19. ==> at http://127.0.0.1:5000/login
  20. --
  21. 1 of 1 files SUCCEEDED.

一个成功的测试诞生了。