简体   繁体   English

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

[英]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.pybar.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: 笔记:

  • This is from my .bashrc file. 这是我的.bashrc文件。 Modify appropriately if you don't use bash. 如果不使用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 (ie instead of telling you your --cover-package is incorrect, it will act like you didn't supply the option at all). 否则,包名称将是不正确的,并且覆盖将无声地处理它们(即,而不是告诉您--cover-package不正确,它将表现得像你根本没有提供选项)。
  • I'm currently using Python 2.7.6 on Ubuntu 13.10, with nose version 1.3.0 and coverage version 3.7.1. 我目前在Ubuntu 13.10上使用Python 2.7.6,鼻子版本为1.3.0,覆盖版本为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. 在您的使用中,如果它们不符合您的需求,请删除--cover-erase和--cover-inclusive。
  • If you want to sort in normal order instead of reverse order, replace -nr with -n in the sort command. 如果你想在正常的秩序,而不是相反的顺序进行排序,更换-nr-n在sort命令。
  • These commands assume that all of your top-level Python files/packages are named without a dot (other than the dot in ".py"). 这些命令假设您的所有顶级Python文件/包都没有点(不是“.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 = 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.

相关问题 django-nose-在覆盖范围内排除目录 - django-nose - Exclude directory in coverage 我可以在本地目录而不是〜/ local中安装pip吗? - Can I install pip in my local directory rather than ~/local? 编辑:Ghostscript 可以给我二进制数据而不是输出到目录吗? - EDIT: Can Ghostscript give me the binary data rather than output to a directory? 如何使用nose2从给定的包中获取覆盖率报告 - How to get coverage report from a given package using nose2 如何更改此代码以输出颜色取决于高度的直方图,而不是我选择的默认“酷”值 - How can I change this code to output an histogram with colors depending on height rather than the default 'cool' I chose 我可以将TestCases与鼻子套在一起吗? - Can I nest TestCases with Nose? 鼻子报告累积覆盖率 - nose reports cumulative coverage 鼻子覆盖的可疑差距 - Suspicious gaps in Nose coverage 我想使用要使用的目录名而不是文件名 [on hold] - I want to use the directory name to use rather than the filename [on hold] 如何在Windows的Python 3中仅安装Autobahn(仅用于asyncio而不是Twisted),而没有整个Crossbar程序包膨胀? - How can I install Autobahn only (for use only with asyncio rather than Twisted), without the entire Crossbar package bloat, in Python 3 on Windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM