尝试了按照 Behave 文档中的指导在咱们的 Django 项目上加入 BDD。今天就来分享下过程当中解决的坑和一些心得体会。 html
若是你项目中的数据库初始数据一直维护得好,或者大家已经在用 Django 自己提供的测试机制在跑测试了,那你应该不会踩到这个坑。否则,跑测试时动态建立的 test db 中空空如也,代码中的大部分 view 应该都会抛出各类的异常来。 python
这个坑和上一个紧密关联。因为使用 mechanize 来模拟一个浏览器来访问,这中间抛出的异常的内容其实就是用 Django 开发过程当中常常看到的异常信息页,是一个 HTML 页面。Behave 驱动 mechanize 时的输出没有进一步的细节,只能知道是 500 INTERNAL ERROR。并且若是只是把异常内容打印到 console,HTML 的内容也很难阅读。个人解决办法是把异常内容写到临时文件去: git
try: ... except Exception as e: with open("/tmp/error.html", 'w') as f: f.write(e.read())
固然目前这个办法只能应对一次 test run 中只抛出一个异常。抛出多个时的状况还须要进一步改进。 github
也可能 Behave 自己就能作到这样的事情,不过目前还不知道。 sql
运行的输出样子是这样: 数据库
» behave tests/features Feature: Demonstrate how to use the mechanize browser to do useful things. # tests/features/browser.feature:1 Creating test database for alias 'default'... Scenario: Logging in to our new Django site # tests/features/browser.feature:3 Given a user # tests/features/steps/browser_steps.py:7 When I log in # tests/features/steps/browser_steps.py:15 Then I see my account summary # tests/features/steps/browser_steps.py:29 And I see a warm and welcoming message # tests/features/steps/browser_steps.py:37 Destroying test database for alias 'default'... 1 feature passed, 0 failed, 0 skipped 1 scenario passed, 0 failed, 0 skipped 4 steps passed, 0 failed, 0 skipped, 0 undefined Took 0m0.7s
和驱动 selenium 或者 splinter 不同的是,这个方法并不须要真正运行起 server 来(或者须要 django-live-server),而是利用 WSGI intercept 把 mechanize 产生的请求直接短路给 WSGI handler。在 view layer 层面作测试,这样就够了。因为避免了运行一个 browser 进程,以及其加载静态资源和运行 JS 代码的 overhead,跑一遍测试的速度应该是大大加快了的。 django
在 postgresql.conf 中修改下列参数 浏览器
fsync=off full_page_writes=off synchronous_commit=off checkpoint_segments = 6 checkpoint_timeout = 1h
依据参照 SO 上的这个回答。 post
做者:czhang 测试
原文连接:http://czhang.writings.io/articles/5-django-bdd-behave-20130519