测试驱动的python源代码搜索。
pytest-nodev的Python项目详细描述
pytest nodev是一个简单的测试驱动的python代码搜索引擎, 它查找与给定测试指定的行为匹配的类和函数。
“测试驱动代码搜索”如何工作?
更准确地说,pytest nodev是一个pytest插件 它允许您执行一组测试,这些测试指定类或函数的预期行为 在python标准库和您安装的所有模块中的所有对象上。
演示它在实践中的工作原理。我需要编写一个parse_bool函数,它可以从字符串中可靠地解析布尔值。这是我打算在编写自己的实现之后用来验证它的测试:
deftest_parse_bool():assertnotparse_bool('false')assertnotparse_bool('FALSE')assertnotparse_bool('0')assertparse_bool('true')assertparse_bool('TRUE')assertparse_bool('1')
首先,安装latest version of pytest-nodev 从python包索引:
$ pip install pytest-nodev
然后将规范测试复制到test_parse_bool.py文件并 用pytest.mark.candidate装饰它如下:
importpytest@pytest.mark.candidate('parse_bool')deftest_parse_bool():assertnotparse_bool('false')assertnotparse_bool('FALSE')assertnotparse_bool('0')assertparse_bool('true')assertparse_bool('TRUE')assertparse_bool('1')
最后,指示pytest对python标准库中的所有候选可调用函数运行测试:
$ py.test --candidates-from-stdlib test_parse_bool.py ======================= test session starts ========================== platform darwin -- Python 3.5.1, pytest-2.9.2, py-1.4.31, pluggy-0.3.1 rootdir: /tmp, inifile: setup.cfg plugins: nodev-1.0.0, timeout-1.0.0 collected 4000 items test_parse_bool.py xxxxxxxxxxxx[...]xxxxxxxxXxxxxxxxx[...]xxxxxxxxxxxx ====================== pytest_nodev: 1 passed ======================== test_parse_bool.py::test_parse_bool[distutils.util:strtobool] PASSED === 3999 xfailed, 1 xpassed, 260 pytest-warnings in 75.38 seconds ====
在一分钟多的时间里,pytest nodev从标准库中收集了4000个函数, 对所有这些组件运行规范测试,然后 报告了distutils.util模块中的strtobool函数 是唯一通过你考试的候选人。
现在您可以查看它,如果您喜欢,可以在代码中使用它。 无需编写自己的实现!
wow!哇!它一直都很好用吗?
老实说,strobool是python标准库中一个鲜为人知的瑰宝 非常适合说明测试驱动代码搜索的所有好处。 以下是一些大致的重要性排序:
- 导入的函数是一个少了一个的函数,它被编码、测试、记录、调试, 移植、维护……
- 这是经过战斗测试的代码,很多旧的bug已经被压缩了
- 是其他人编写的代码有一个上游可以向报告新的错误
- 除此之外,它还免费为您提供其他有用的功能
- 它在python标准库中,不需要额外的依赖项
大胖子警告!
使用pytest nodev搜索代码看起来非常像使用随机参数运行任意可调用函数。 使用错误的参数集调用的许多函数可能会产生意外的结果,范围包括 从稍微烦人的角度,想想os.mkdir('false'), 对于完全灾难性的,请考虑shutil.rmtree('/', True)。 认真使用pytest nodev,特别是使用--candidates-from-all, 需要在操作系统级隔离的情况下运行测试, 例如,作为一个专用的用户,或者在专用的容器中做得更好。 那张Starter kit 指导文档如何安全高效地运行pytest nodev。
项目资源
Documentation | http://pytest-nodev.readthedocs.io |
Support | https://stackoverflow.com/search?q=pytest-nodev |
Development | https://github.com/nodev-io/pytest-nodev |
Discussion | To be decided, see issue #15 |
Download | https://pypi.python.org/pypi/pytest-nodev |
Code quality |
许可证
Pytest Nodev是免费的开源软件 根据MIT许可证的条款分发。