自动测试运行程序。把鼻子撑出盒子。
sniffer的Python项目详细描述
概述
sniffer是使用nosetest库的Python自动测试工具。
new:现在可以自定义嗅探器来运行任何东西,请参阅“高级用法”。
如果代码更改,嗅探器将自动重新运行测试。和另一个第三方 库(见下文),相比之下,文件系统监视的CPU使用量减少了 纯python解决方案。但是,如果没有这些库,sniffer仍然可以工作。
寻找维修人员
我(@jeffh)正在寻找一个新的维修人员来把这个项目进行到新的高度。我目前正在离开这个项目的维护模式(响应问题,合并拉请求),但我没有把我的大部分空闲时间用于这个项目。
如果你有兴趣接手这个项目,可以通过twitter(@jeffhui)或电子邮件联系我。
使用量
要安装:
pip install sniffer
只需在项目目录中运行sniffer。
你可以使用sniffer --help作为选项,就像autonose,你可以通过鼻子 前缀为-x的参数:-x--with-doctest或-x--config。
autonose的问题是,自动检测可能很慢,无法检测更改。这是到期日 到纯python实现-手动遍历文件系统以查看 已更改[1]。尽管嗅探器的默认安装有相同的问题,但是 第三方库可以帮助解决问题。库取决于您的操作系统:
- 如果使用linux,则需要安装pyinotify。
- 如果使用windows,则需要安装pywin32。
- 如果使用mac os x10.5+(leopard),则需要安装MacFSEvents。
如果您希望支持其他通知系统,可以安装:
- gntp支持Growl(Mac OS X)。
- osxnotify和libosxnotify用于本机os x通知(max os x 10.9.4及更新版本)
- py-notify支持LibNotify(Linux)。
[1] | This has been resolved in subsequent autonose versions, using watchdog. |
高级使用
不想流鼻涕吗?你可以做你真正想做的事。在中创建一个scent.py文件 您当前的工作目录。以下是迄今为止您可以做的一个示例:
fromsniffer.apiimport*# import the really small APIimportos,termstyle# you can customize the pass/fail colors like thispass_fg_color=termstyle.greenpass_bg_color=termstyle.bg_defaultfail_fg_color=termstyle.redfail_bg_color=termstyle.bg_default# All lists in this variable will be under surveillance for changes.watch_paths=['.','tests/']# this gets invoked on every file that gets changed in the directory. Return# True to invoke any runnable functions, False otherwise.## This fires runnables only if files ending with .py extension and not prefixed# with a period.@file_validatordefpy_files(filename):returnfilename.endswith('.py')andnotos.path.basename(filename).startswith('.')# This gets invoked for verification. This is ideal for running tests of some sort.# For anything you want to get constantly reloaded, do an import in the function.## sys.argv[0] and any arguments passed via -x prefix will be sent to this function as# it's arguments. The function should return logically True if the validation passed# and logicially False if it fails.## This example simply runs nose.@runnabledefexecute_nose(*args):importnosereturnnose.run(argv=list(args))
这是最基本的情况。没什么好奇怪的。您可以有多个文件验证器和 如果你想的话,可以运行装饰程序。
还支持通过文件验证器选择可运行函数。 如果你想用nose运行python文件,mocha运行javascript文件, csslint和css。或者其他你能想到的组合。为了 示例:
# Here we instruct the 'python_tests' runnable to be kicked off# when a .py file is changed@select_runnable('python_tests')@file_validatordefpy_files(filename):returnfilename.endswith('.py')andnotos.path.basename(filename).startswith('.')# Here we instruct the 'javascript_tests' runnable to be kicked off# when a .js file is changed@select_runnable('javascript_tests')@file_validatordefjs_files(filename):returnfilename.endswith('.js')andnotos.path.basename(filename).startswith('.')@runnabledefpython_tests(*args):importnosereturnnose.run(argv=list(args))@runnabledefjavascript_tests(*args):command="mocha tests/js-tests.js"returncall(command,shell=True)==0
当 javascript文件已更改。
其他用途
与其他测试框架一起运行
如果要运行另一个单元测试框架,可以重写sniffer.Sniffer,
它是处理运行测试或任何您想要的东西的类。特别是,你会想
重写run,方法以配置需要执行的操作。
属性test_args是通过--config=blah和^{tt10}收集的参数。$
配置选项。应该在函数内部而不是外部执行导入,
让类重新加载测试框架(并减少多个运行错误的可能性)。
子类化之后,在调用run时将sniffer_instance参数设置为自定义类
或者主要的。
当前问题
对于Linux,在终止时有时会抛出异常。
当前程序只在当前工作目录中查找更改。这不是 最佳解决方案:它不理解对源代码的更改如何影响它。