一个元包,包含一个完整的用于tdd敏捷开发的工具集
agile的Python项目详细描述
python的元包,带有用于敏捷开发工作流的工具。
将其添加到项目中
pip install agile
里面是什么?
模拟
mock库是 用python模拟的最简单和最具表现力的方法。
示例:模拟I/O调用:
# cool-git-project/my_cool_library/core.pyimportioimportjsonclassJSONDatabase(object):def__init__(self,filename=None,data={}):self.filename=filenameself.data=datadefstate_to_json(self):returnjson.dumps(self.data)defsave(self):# open filefd=io.open(self.filename,'wb')fd.write(self.state_to_json())fd.close()
# cool-git-project/tests/unit/test_core.pyfrommockimportpatchfrommy_cool_library.coreimportJSONDatabase@patch('my_cool_library.core.io')@patch('my_cool_library.core.JSONDatabase.state_to_json')deftest_json_database_save(state_to_json,io):("JSONDatabase.save() should open the database file, ""and write the latest json state of the data")# Given that the call to io.open returns a mockmocked_fd=io.open.return_value# And that I create an instance of JSONDatabase with some datajdb=JSONDatabase('my-database.json',data={'foo':'bar'})# When I call .save()jdb.save()# Then the file descriptor should have been opened in write mode,# and pointing to the right fileio.open.assert_called_once_with('my-database.json','wb')# And the returned file descriptor should have been used# to write the return value from state_to_jsonmocked_fd.write.assert_called_once_with(state_to_json.return_value)# And then the file descriptor should have been closedmocked_fd.close.assert_called_once_with()
可以找到模拟文档 here
当然
当然修改了内存中的所有python对象,添加了一个 属性should,它允许您测试给定 反对。
让我们在实践中看看。
仍然在考虑上面的mock示例中的项目,现在让我们 测试state_to_json是否返回json字符串。
deftest_json_database_state_to_json():("JSONDatabase.state_to_json() should return a valid json string")# Given that I have an instance of the database containing some datajdb=JSONDatabase(data={'name':'Foo Bar'})# When I call .state_to_jsonresult=jdb.state_to_json()# Then it should return a valid JSONresult.should.equal('{"name": "Foo Bar"}')
确保文档可用 here
鼻子+覆盖+红鼻子
nosetests -vsx --rednose --with-coverage --cover-package=my_cool_library tests/unit # or nosetests -vsx --rednose --with-coverage --cover-package=my_cool_library tests/functional
nose是一个很好的测试运行程序,递归地扫描以 test_和.py。它支持插件和敏捷安装 两个很酷的插件:
覆盖范围
覆盖率是一个收集测试覆盖率数据的模块,这样nose可以 显示哪些python代码行没有测试覆盖率的摘要。
红鼻子
rednose是一个插件,在运行 测试,并在red中显示不好的东西它突出显示问题并使 更容易看出问题在哪里,非常棒。
更重要的是,只要你写一行docstring来描述 你的测试rednose将显示整个句子,漂亮而且没有 砍。
JSONDatabase.save() should open the database file, and write the latest json state of the data ... passed JSONDatabase.state_to_json() should return a valid json string ... passed ----------------------------------------------------------------------------- 2 tests run in 0.0 seconds (2 tests passed)
注意:nose实际上匹配名称中包含test的文件,并且 也可以找到TestCase类,但我建议使用基于函数的 测试,为了清晰,表达和加强简单性。我们 开发人员倾向于在设置和拆卸函数时添加太多逻辑 在编写基于测试的类时。