nodev帮助编写规范测试。
项目描述
nodev.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.FlatContainer辅助器重写测试
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测试无需更新,因为它对返回值的细节几乎没有假设。
项目资源
支持 |
|
开发 |
|
讨论 |
待定,请参阅pytest-nodev存储库的问题#15。 |
下载 |
|
代码质量 |
|
nodev 网站 |
贡献
非常欢迎贡献。请参阅CONTRIBUTING文档以了解最佳帮助方式。如果您遇到任何问题,请提交包含详细描述的问题。
作者
Alessandro Amici - @alexamici
赞助商
许可证
nodev.specs 是免费的开源软件,根据MIT许可证条款分发。
项目详情
关闭
nodev.specs-0.3.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 209506e6c73be3c0a007039f97ad4b83cb96443f4e876732e0b07c5a9cee54de |
|
MD5 | f21e5e30716317321c943d9dac7adbe5 |
|
BLAKE2b-256 | c0514e85802fbdec9cb65f44a8f1a73a7b804b7ae957ff45ac2d2759a536ea63 |
关闭
nodev.specs-0.3.1-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 588fc8dd84bf14d8b78f14228b8211d9117314484ca36905cca8821629f00919 |
|
MD5 | 0aa7ed472b882859dfab767b1ff127f2 |
|
BLAKE2b-256 | ee9146c6857b7d32b8b8083215dc7da8e41ea69afe2d8f2baa358f68848e8036 |