Can I restrict nose coverage output to directory (rather than package)?

前端 未结 8 2002
-上瘾入骨i
-上瘾入骨i 2021-02-05 08:45

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_test         


        
8条回答
  •  野性不改
    2021-02-05 08:48

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

提交回复
热议问题