简化了web服务的测试。
webunit2的Python项目详细描述
webunit2是Richard Jones’ webunit的精神继承者,但决不是叉子。这个 图书馆从一开始就建起来了,里面有文件和 测试。也是hosted on GitHub, 很容易叉开。
文档
Documentation is available on PyPi.。
您也可以自己构建文档。确保安装了狮身人面像 通过运行:
$ easy_install -U sphinx
然后,克隆存储库,cd并运行:
$ python setup.py build_sphinx
文档位于build/sphinx/html。
开始
开始真的很容易。你只需要执行 webunit2.testcase.Testcase并开始编写单元测试。
import webunit2 class TestMyApp(webunit2.TestCase): def __init__(self): # You aren't required to initialize the base class here, but it # makes dealing with subsequent requests easier--because they can # be relative paths! webunit2.TestCase.__init__(self, "http://myapp.com") def test_some_url(self): # Just do a normal get self.get('/somepath') # Lets post some data self.post('/somepath', post_params={"key": "value"}) # No helper function for your HTTP method? No problem! self.make_request("HEAD", "/somepath")
当然,因为这是一个单元测试框架,您可能会 很多断言——特别是在内容方面。那也很容易!
def test_for_content(self): # Assert the word `hippo` appears in the content. self.get_assertContent('/something', content="hippo") # Assert that the `X-Customheader` header was set. self.get_assertHeader('/something', 'X-Customheader')
如果其中任何一个失败(即返回码不是200或内容失败 不包含河马,AssertionError将正常筹集。以及 当然,如果需要检查响应上的多个值,则 assert*函数直接在响应上可用。例如, 以上内容可以重写为:
def test_for_content_resp(self): resp = self.get('/something') # Assert the word `hippo` appears in the content. resp.assertContent("hippo") # Assert that the `X-Customheader` header was set. resp.assertHeader('X-Customheader')
这也允许您将更多的断言链接在一起,即检查 标题、饼干等。这些都是all设计用来简化测试,所以 如果您有建议或投诉…
贡献
源代码是hosted on Github, 这使它成为一个叉和贡献。Please submit issues using the GitHub tracker!我喜欢得到 反馈,我强烈建议您提交功能和/或错误的罚单。