我可以将鼻子覆盖输出限制到目录(而不是包)吗?

发布于 2024-07-19 09:00:27 字数 983 浏览 13 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

纸伞微斜 2024-07-26 09:00:27

你可以像这样使用它:

--cover-package=foo --cover-package=bar

我快速查看了nose源代码以确认:这是行

    if options.cover_packages:
        for pkgs in [tolist(x) for x in options.cover_packages]:

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]:
你是暖光i 2024-07-26 09:00:27

您可以使用:

--cover-package=.

或者甚至设置环境变量

NOSE_COVER_PACKAGE=.

用nose 1.1.2测试

You can use:

--cover-package=.

or even set environment variable

NOSE_COVER_PACKAGE=.

Tested with nose 1.1.2

辞别 2024-07-26 09:00:27

我有很多顶级 Python 文件/包,并且发现使用 --cover-package 手动列出它们很烦人,所以我为自己创建了两个别名。 Alias nosetests_cover 将对 --cover-package 中列出的所有顶级 Python 文件/包进行覆盖。 Alias 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"

注意:

  • 这是来自我的 .bashrc 文件。 如果不使用bash则适当修改。
  • 这些必须从您的顶级目录运行。 否则,包名称将不正确,并且覆盖范围将默默地无法处理它们(即,它不会告诉您 --cover-package 不正确,而是会表现得好像您根本没有提供该选项)。
  • 我目前在 Ubuntu 13.10 上使用 Python 2.7.6,nose 版本为 1.3.0,coverage 版本为 3.7.1。 这是我测试这些命令的唯一设置。
  • 在您的使用中,如果 --cover-erase 和 --cover-inclusive 不符合您的需求,请删除它们。
  • 如果要按正常顺序而不是逆序排序,请将排序命令中的 -nr 替换为 -n
  • 这些命令假设所有顶级 Python 文件/包的命名都不带点(“.py”中的点除外)。 如果您的情况并非如此,请阅读下面的详细信息部分以了解命令部分,然后根据需要修改命令。

详细信息:

我并不声称这些是实现我想要的结果的最有效的命令。 它们只是我碰巧想出的命令。 =P

主要要解释的是 --cover-package 的参数。 它构建以逗号分隔的顶级 Python 文件/包名称列表(从文件名中删除“.py”),如下所示:

  • \$ -- 转义 $ 双引号字符串中的字符。
  • $( ) -- 插入其中包含的命令的结果。
  • ls -- 列出当前目录中的所有名称(必须是顶级 Python 目录)。
  • <代码>| sed -r 's/[.]py$//' -- 在列表中,将“foo_bar.py”替换为“foo_bar”。
  • <代码>| fgrep -v '.' -- 在列表中,删除所有不带点的名称(例如删除 foo_bar.pyc 和notes.txt)。
  • <代码>| Paste -s -d ',' -- 将列表从换行符分隔更改为逗号分隔。

我还应该解释一下排序。

  • 2>&1 -- 连接 stderr 和 stdout。
  • <代码>| fgrep '%' -- 删除所有不带 % 字符的输出行。
  • <代码>| sort -nr -k 4 -- 按第四列(即覆盖百分比列)以相反的数字顺序对剩余行进行排序。 如果您想要正常顺序而不是逆序,请将 -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. Alias nosetests_cover_sort will do the same and additionally sort your results by coverage percentage.

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:

  • This is from my .bashrc file. Modify appropriately if you don't use bash.
  • These must be run from your top-level directory. Otherwise, the package names will be incorrect and coverage will silently fail to process them (i.e. instead of telling you your --cover-package is incorrect, it will act like you didn't supply the option at all).
  • I'm currently using Python 2.7.6 on Ubuntu 13.10, with nose version 1.3.0 and coverage version 3.7.1. This is the only setup in which I've tested these commands.
  • In your usage, remove --cover-erase and --cover-inclusive if they don't match your needs.
  • If you want to sort in normal order instead of reverse order, replace -nr with -n in the sort command.
  • These commands assume that all of your top-level Python files/packages are named without a dot (other than the dot in ".py"). If this is not true for you, read Details section below to understand the command parts, then modify the commands as appropriate.

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! =)

不如归去 2024-07-26 09:00:27

如果您使用 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.

静水深流 2024-07-26 09:00:27

我会这样做:

nosetests --with-coverage --cover-package=foo,bar tests/*

与其他建议的解决方案相比,我更喜欢这个解决方案; 它很简单,但您可以明确了解您希望承保哪些套餐。 Nadia 的答案涉及更多冗余输入,Stuart 的答案使用 sed 并且仍然通过调用 touch __init__.py 创建一个包,并且
--cover-package=. 对我不起作用。

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=. doesn't work for me.

终遇你 2024-07-26 09:00:27

对于尝试使用 setup.cfg 执行此操作的任何人,以下方法有效。 我在弄清楚如何指定多个包时遇到了一些麻烦。

[nosetests]
with-coverage=1
cover-html=1
cover-package=module1,module2

For anyone trying to do this with setup.cfg, the following works. I had some trouble figuring out how to specify multiple packages.

[nosetests]
with-coverage=1
cover-html=1
cover-package=module1,module2
烂柯人 2024-07-26 09:00:27
touch __init__.py; nosetests --with-coverage --cover-package=`pwd | sed 's@.*/@@g'`
touch __init__.py; nosetests --with-coverage --cover-package=`pwd | sed 's@.*/@@g'`
凉风有信 2024-07-26 09:00:27

您可以像这样改进已接受的答案 --cover-package=foo,bar

You can improve the accepted answer like so --cover-package=foo,bar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文