一个元包,包含一个完整的用于tdd敏捷开发的工具集

agile的Python项目详细描述


https://travis-ci.org/gabrielfalcao/agile.svg?branch=masterhttps://codecov.io/gh/gabrielfalcao/agile/branch/master/graph/badge.svg

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类,但我建议使用基于函数的 测试,为了清晰,表达和加强简单性。我们 开发人员倾向于在设置和拆卸函数时添加太多逻辑 在编写基于测试的类时。

要点:

创建基本的python测试基础结构
mkdir -p tests/{unit,functional}
touch tests/{unit,functional,}/__init__.py
printf'import sure\nsure\n' > tests/unit/__init__.py
printf'import sure\nsure\n' > tests/functional/__init__.py

现在继续添加一个单元测试文件,尝试将您的测试文件命名为 它类似于正在测试的模块,例如,假设您是 测试my_cool_library/engine.py,您可以创建一个测试文件,如 这个

printf"# -*- coding: utf-8 -*-\n\n" > tests/unit/test_engine.py

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何序列化数字?   java如何用我的Mainclass扩展ListActivity类和Fragment类?   JavaJersey、Jackson和JAXRS发布了多种JSON格式   java如何使用JavaFXKeyCombination覆盖系统默认的键盘快捷键,如Ctrl+C、Ctrl+V?   java Jersey类路径扫描示例Jersey。配置。服务器供应商。类路径   java什么样的数据结构可以作为一个数组,但在同一索引下给定多个值时会自动增长到第二维度?   java如何序列化非持久实体中的嵌套PersistentEntityResource   协议缓冲区我可以让protoc在Java中生成int数组吗?   在GregorianCalendar ArrayList Java中添加日期元素   从html模板动态生成pdf文件并用java生成目录   java Gridgain 6.5.5开源多个节点速度较慢。。?   java如何检查数组中所有整数的不相等性?   java在Eclipse中,如何多次运行JUnit测试用例   java侦听器不能处理特定的片段   java不是一个声明?(蓝色J)   找不到Attributer类型的PersonId的java定义