[英]Can I restrict nose coverage output to directory (rather than package)?
My SUT looks like: 我的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
When I run nosetests --with-coverage
, I get details for all sorts of modules that I'd rather ignore. 当我运行nosetests --with-coverage
,我会得到各种模块的详细信息,我宁愿忽略它们。 But I can't use the --cover-package=PACKAGE
option because foo.py
& bar.py
are not in a package. 但是我不能使用--cover-package=PACKAGE
选项,因为foo.py
和bar.py
不在包中。 (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.) (请参阅http://lists.idyll.org/pipermail/testing-in-python/2008-November/001091.html之后的帖子,以了解我没有将它们放入包中的原因。)
Can I restrict coverage output to just foo.py & bar.py? 我可以将coverage输出限制为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?" 更新 - 假设没有比下面的Nadia更好的答案,我问了一个跟进问题: “如何编写一些(bash)shell脚本将目录中所有匹配的文件名转换为命令行选项? “
You can use it like this: 你可以像这样使用它:
--cover-package=foo --cover-package=bar
I had a quick look at nose source code to confirm: This is the line 我快速浏览一下鼻子源代码来确认: 这就是这条线
if options.cover_packages:
for pkgs in [tolist(x) for x in options.cover_packages]:
You can use: 您可以使用:
--cover-package=.
or even set environment variable 甚至设置环境变量
NOSE_COVER_PACKAGE=.
Tested with nose 1.1.2 用鼻子1.1.2测试
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. 我有很多顶级的Python文件/包,并且发现使用--cover-package手动列出它们很烦人,所以我为自己制作了两个别名。 Alias nosetests_cover
will run coverage with all your top-level Python files/packages listed in --cover-package. 别名nosetests_cover
将覆盖--cover-package中列出的所有顶级Python文件/包。 Alias nosetests_cover_sort
will do the same and additionally sort your results by coverage percentage. 别名nosetests_cover_sort
将执行相同的操作,并按覆盖百分比对结果进行排序。
nosetests_cover_cmd="nosetests --with-coverage --cover-erase --cover-inclusive --cover-package=\$( ls | sed -r 's/[.]py$//' | fgrep -v '.' | paste -s -d ',' )"
alias nosetests_cover=$nosetests_cover_cmd
alias nosetests_cover_sort="$nosetests_cover_cmd 2>&1 | fgrep '%' | sort -nr -k 4"
Notes: 笔记:
-nr
with -n
in the sort command. 如果你想在正常的秩序,而不是相反的顺序进行排序,更换-nr
与-n
在sort命令。 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 = P
The main thing to explain would be the argument to --cover-package. 要解释的主要问题是--cover-package。 It builds the comma-separated list of top-level Python file/package names (with ".py" stripped from file names) as follows: 它构建了以逗号分隔的顶级Python文件/包名称列表(从文件名中删除了“.py”),如下所示:
\\$
-- 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). ls
- 列出当前目录中的所有名称(必须是顶级Python目录)。 | sed -r 's/[.]py$//'
| sed -r 's/[.]py$//'
-- In the list, replaces "foo_bar.py" with "foo_bar". | sed -r 's/[.]py$//'
- 在列表中,将“foo_bar.py”替换为“foo_bar”。 | fgrep -v '.'
-- In the list, removes all names without a dot (eg removes foo_bar.pyc and notes.txt). - 在列表中,删除没有点的所有名称(例如删除foo_bar.pyc和notes.txt)。 | paste -s -d ','
| paste -s -d ','
-- Changes the list from newline-separated to comma-separated. | paste -s -d ','
- 将列表从换行符分隔为逗号分隔。 I should also explain the sorting. 我还应该解释排序。
2>&1
-- Joins stderr and stdout. 2>&1
- 加入stderr和stdout。 | fgrep '%'
| fgrep '%'
-- Removes all output lines without a %
character. | fgrep '%'
- 删除没有%
字符的所有输出行。 | sort -nr -k 4
| sort -nr -k 4
-- Sorts the remaining lines in reverse numerical order by the 4th column (which is the column for coverage percentage). | sort -nr -k 4
- 按第四列(覆盖百分比列)的反向数字顺序对剩余行进行排序。 If you want normal order instead of reverse order, replace -nr
with -n
. 如果你想正常秩序,而不是相反的顺序,更换-nr
与-n
。 Hope this helps someone! 希望这有助于某人! =) =)
如果使用coverage:py 3.0 ,则默认情况下会忽略Python目录中的代码,包括标准库和所有已安装的软件包。
I would do this: 我会这样做:
nosetests --with-coverage --cover-package=foo,bar tests/*
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=.
Nadia的答案涉及更多的冗余打字,Stuart的答案使用sed并且仍然通过调用touch __init__.py
和--cover-package=.
创建一个包--cover-package=.
doesn't work for me. 不适合我。
For anyone trying to do this with setup.cfg, the following works. 对于尝试使用setup.cfg执行此操作的任何人,以下工作。 I had some trouble figuring out how to specify multiple packages. 我在确定如何指定多个包时遇到了一些麻烦。
[nosetests]
with-coverage=1
cover-html=1
cover-package=module1,module2
touch __init__.py; nosetests --with-coverage --cover-package=`pwd | sed 's@.*/@@g'`
您可以改进接受的答案,例如--cover-package=foo,bar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.