我可以将鼻子覆盖输出限制到目录(而不是包)吗?
我的 SUT 如下所示:
foo.py
bar.py
tests/__init__.py [empty]
tests/foo_tests.py
tests/bar_tests.py
tests/integration/__init__.py [empty]
tests/integration/foo_tests.py
tests/integration/bar_tests.py
当我运行 nosetests --with-coverage
时,我获得了各种详细信息 我宁愿忽略的模块。 但我不能使用 --cover-package=PACKAGE
选项,因为 foo.py
& bar.py
不在 包裹。 (请参阅后面的线程 http://lists.idyll.org/pipermail/testing -in-python/2008-November/001091.html 因为我不将它们放入包中的原因。)
我可以将覆盖输出限制为仅 foo.py & 酒吧.py?
更新 - 假设没有比下面的 Nadia 更好的答案,我'我问了一个后续问题:“如何编写一些(bash)shell 脚本将目录中所有匹配的文件名转换为命令行选项?”
My SUT looks like:
foo.py
bar.py
tests/__init__.py [empty]
tests/foo_tests.py
tests/bar_tests.py
tests/integration/__init__.py [empty]
tests/integration/foo_tests.py
tests/integration/bar_tests.py
When I run nosetests --with-coverage
, I get details for all sorts of
modules that I'd rather ignore. But I can't use the--cover-package=PACKAGE
option because foo.py
& bar.py
are not in a
package. (See the thread after
http://lists.idyll.org/pipermail/testing-in-python/2008-November/001091.html
for my reasons for not putting them in a package.)
Can I restrict coverage output to just foo.py & bar.py?
Update - Assuming that there isn't a better answer than Nadia's below, I've asked a follow up question: "How do I write some (bash) shell script to convert all matching filenames in directory to command-line options?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你可以像这样使用它:
我快速查看了nose源代码以确认:这是行
You can use it like this:
I had a quick look at nose source code to confirm: This is the line
您可以使用:
或者甚至设置环境变量
用nose 1.1.2测试
You can use:
or even set environment variable
Tested with nose 1.1.2
我有很多顶级 Python 文件/包,并且发现使用 --cover-package 手动列出它们很烦人,所以我为自己创建了两个别名。 Alias
nosetests_cover
将对 --cover-package 中列出的所有顶级 Python 文件/包进行覆盖。 Aliasnosetests_cover_sort
也会执行相同的操作,并另外按覆盖率百分比对结果进行排序。注意:
-nr
替换为-n
。详细信息:
我并不声称这些是实现我想要的结果的最有效的命令。 它们只是我碰巧想出的命令。 =P
主要要解释的是 --cover-package 的参数。 它构建以逗号分隔的顶级 Python 文件/包名称列表(从文件名中删除“.py”),如下所示:
\$
-- 转义$
双引号字符串中的字符。$( )
-- 插入其中包含的命令的结果。ls
-- 列出当前目录中的所有名称(必须是顶级 Python 目录)。我还应该解释一下排序。
2>&1
-- 连接 stderr 和 stdout。%
字符的输出行。-nr
替换为-n
。希望这对某人有帮助! =)
I have a lot of top-level Python files/packages and find it annoying to list them all manually using --cover-package, so I made two aliases for myself. Alias
nosetests_cover
will run coverage with all your top-level Python files/packages listed in --cover-package. Aliasnosetests_cover_sort
will do the same and additionally sort your results by coverage percentage.Notes:
-nr
with-n
in the sort command.Details:
I don't claim that these are the most efficient commands to achieve the results I want. They're just the commands I happened to come up with. =P
The main thing to explain would be the argument to --cover-package. It builds the comma-separated list of top-level Python file/package names (with ".py" stripped from file names) as follows:
\$
-- Escapes the$
character in a double-quoted string.$( )
-- Inserts the result of the command contained within.ls
-- Lists all names in current directory (must be top-level Python directory).| sed -r 's/[.]py$//'
-- In the list, replaces "foo_bar.py" with "foo_bar".| fgrep -v '.'
-- In the list, removes all names without a dot (e.g. removes foo_bar.pyc and notes.txt).| paste -s -d ','
-- Changes the list from newline-separated to comma-separated.I should also explain the sorting.
2>&1
-- Joins stderr and stdout.| fgrep '%'
-- Removes all output lines without a%
character.| sort -nr -k 4
-- Sorts the remaining lines in reverse numerical order by the 4th column (which is the column for coverage percentage). If you want normal order instead of reverse order, replace-nr
with-n
.Hope this helps someone! =)
如果您使用 coverage:py 3.0,则默认情况下会忽略 Python 目录中的代码,包括标准库和所有已安装的软件包。
If you use coverage:py 3.0, then code in the Python directory is ignored by default, including the standard library and all installed packages.
我会这样做:
与其他建议的解决方案相比,我更喜欢这个解决方案; 它很简单,但您可以明确了解您希望承保哪些套餐。 Nadia 的答案涉及更多冗余输入,Stuart 的答案使用 sed 并且仍然通过调用
touch __init__.py
创建一个包,并且--cover-package=.
对我不起作用。I would do this:
I prefer this solution to the others suggested; it's simple yet you are explicit about which packages you wish to have coverage for. Nadia's answer involves a lot more redundant typing, Stuart's answer uses sed and still creates a package by invoking
touch __init__.py
, and--cover-package=.
doesn't work for me.对于尝试使用 setup.cfg 执行此操作的任何人,以下方法有效。 我在弄清楚如何指定多个包时遇到了一些麻烦。
For anyone trying to do this with setup.cfg, the following works. I had some trouble figuring out how to specify multiple packages.
您可以像这样改进已接受的答案
--cover-package=foo,bar
You can improve the accepted answer like so
--cover-package=foo,bar