受rspec启发的实验性行为框架。
describe的Python项目详细描述
描述
从RSpec中得到启发的Behavior Driven Development或bdd框架。bdd结束的原因 TDD不在本项目范围内。但这个框架只是一种尝试 鼓励我做更多的测试。
<> >值得注意的是,{a3}打破了现有的API。安装
要安装,请使用PIP或Easy_安装:
pip install describe
然后您可以导入它:
from describe import Spec, Value, Mock # classes from describe import arg, repository # submodules
用法
快速启动
然后您可以导入库以在unittest或nose中使用。核心功能是value对象:
from describe import Value
使用此值类包装要设置期望值的值。这是一些api 在我得到正确的文档之前的示例:
# self.assertEquals(9, 9) Value(9).should == 9 # self.assertAlmostEqual(5.0-4.0, 1.0) Value(5.0-4.0).should.be_close_to(1.0) # self.assertIn(3, (2,3,4)) Value((2,3,4)).should.contain(3) # self.assertNotIn(5, (2,3,4)) Value((2,3,4)).should_not.contain(5) # self.assertFalse(False) Value(False).should.be.false() # self.assertTrue(isinstance((), tuple)) Value(()).should.be.instance_of(tuple) # self.assertEqual(len(range(5)), 5) # '.elements' is optional Value(range(5)).should.have(5).elements # self.assertGreaterEqual(len(range(5)), 4) Value(range(5)).should.have.at_least(4)
模拟
mock用于抽象出未被测试的类。他们可以定制返回 特定的结果并记录对其执行的所有操作,以便稍后进行验证。模拟api包装 那张voidspace mock library。可以直接使用它而不是这个api。
descripe.mock支持一些操作:
- Mock.should_access property-允许您设置方法调用和属性访问的期望值。
- Mock.should_not_access(attr_name)-允许您设置无法访问的属性的预期值。
- Mock.verify()-验证所有期望,如果需要,则抛出断言错误。
- Mock.reset_mock()-清除访问日志,您不应该直接使用它。
所有其他属性都指向voidspace mock object。一个基本示例:
# create the mock from describe import Mock m = Mock() # set an expectation on what will be performed on the object and it's response m.should_access.upper().and_return('FOO') # run it m.upper() # => 'FOO' # verify that the operations were executed m.verify() # if you want to invoke any operations on the mock object (and not the API), get the # voidspace mock object via the mock attribute: m.should_access.verify().and_return('bar') m.mock.verify() # => 'bar' m.verify()
这些期望期望期望您提供的功能原型:
m.should_access.rjust(5).and_return(' ') m.rjust() # => None - prototype does not match. m.rjust(5) # => ' ' m.verify() # will raise AssertionError because m.rjust() was called
关键字参数和参数一样工作。你可以提出几个特别的论点 特别行动。
args.anything接受任何有效参数,包括无参数:
from describe import args m.should_access.rjust(args.ANYTHING).and_return('anything works') m.rjust('foo', 'bar') # => 'anything works'
args.any_arg接受任何单个参数:
from describe import args m.should_access.rjust(args.ANY_ARG).and_return(4) m.rjust() # => None - is not one argument m.rjust(3) # => 4
其他特殊参数包括:
- arg.ARGS-任何非关键字参数
- arg.KWARGS-任何关键字参数
- arg.an_instance_of(type)-任何值与给定类型匹配的参数
- arg.regexp-arg的别名。正则表达式类型的(type(re.compile(''))的实例。
- arg.includes_pair(key, value)-任何具有键和关联值的参数。
- arg.contains(item, *items)-给定列表或字典中的任何键或项。
- arg.duck_type(*attributes)-具有所有给定属性的任何对象
您还可以设置getter属性的期望值:
m.should_access.score.and_return(23) m.score # => 23
对您创建的每个模拟对象调用verify是很烦人的。幸运的是,每个模拟都被添加到 实例化时的注册表。默认情况下,它们被添加到descripe.mock.repository.default 储存库。您可以对存储库调用verify()来验证其中的所有模拟:
# create mocks: from describe import Mock, Value from describe.mock import repository for i in range(5): m = Mock() m.should_access.lower().and_return('bar') Value(m.lower()).should == 'bar' repository.default.verify() # will verify all mock objects we created above
规格
行为驱动开发的全部目的,是将基于测试的术语重新映射到 更多规范驱动的。spec类是unittest.testcase的替代品,但是 需要nose/sniffer来获得所有的好处。
当前规范继承unittest.testcase:
from describe import Spec, Mock from describ.args import * class DescribeRSpecExpectingArguments(Spec): def before(self): self.m = Mock() def it_should_expect_arguments(self): self.m.should_access.msg(1,2,3) self.m.msg(1,2,3) def it_should_expect_arguments_with_count(self): self.m.should_access.msg(1,2,3).once self.m.msg(1,2,3) def it_should_expect_no_arguments(self): self.m.should_access.msg() self.m.msg() def it_should_expect_any_arguments(self): self.m.should_access.msg(ANYTHING) self.m.msg(1,2,3,4,5,6)
然后使用nose的specplugin`运行specs,或者运行descripe.main程序。