nodev帮助编写规范测试。
nodev.specs的Python项目详细描述
specs帮助您编写描述代码抽象行为的健壮测试 在测试中保留许多实现细节。
用一个例子来最好地解释这个总的想法, 让我们为下面的函数skip_comments编写一个规范测试 返回输入文件中每行的非注释部分:
def skip_comments(stream): return [line.partition('#')[0] for line in stream]
最简单的单元测试可能如下所示:
def test_skip_comments(): assert skip_comments(['# comment']) == [''] assert skip_comments(['value # comment']) == ['value '] assert skip_comments(['value 1', '', 'value 2']) == ['value 1', '', value 2']
这样的单元测试与当前的skip_comments实现的关联比需要的多 每次添加一个小功能时,测试都需要更新, 就像把函数变成生成器一样:
def skip_comments(stream): for line in stream: yield line.partition('#')[0]
这可以通过以更通用的方式重新编写测试来解决:
def test_skip_comments(): assert '' in skip_comments(['# comment']) assert 'value ' in skip_comments(['value # comment']) assert 'value 1' in skip_comments(['value 1', '', 'value 2']) assert 'value 2' in skip_comments(['value 1', '', 'value 2'])
让我们使用nodev.specs.FlatContainerhelper重新编写测试:
def test_skip_comments(): assert '' in FlatContainer(skip_comments(['# comment'])) assert 'value ' in FlatContainer(skip_comments(['value # comment'])) assert 'value 1' in FlatContainer(skip_comments(['value 1', '', 'value 2'])) assert 'value 2' in FlatContainer(skip_comments(['value 1', '', 'value 2']))
现在您可以选择跳过返回当前行索引的空行:
def skip_comments(stream): for index, line in enumerate(stream): value = line.partition('#')[0] if value: yield index, value
或者还返回每行的注释:
def skip_comments(stream): for index, line in enumerate(stream): value, sep, comment = line.partition('#') if value: yield index, value, sep + comment
nodev测试不需要更新,因为它几乎不需要假设细节 返回值的。
项目资源
Support | https://stackoverflow.com/search?q=nodev |
Development | https://github.com/nodev-io/nodev.specs |
Discussion | To be decided, see issue #15 of the pytest-nodev repository. |
Download | https://pypi.python.org/pypi/nodev.specs |
Code quality | |
nodev website | http://nodev.io |
许可证
specs是免费的开源软件 根据MIT许可证的条款分发。