0

I have a small Python 3 project that I intend to keep as a reusable library. Let's call it utils_common. The coverage report from coverage.py includes some coverage for actual unit tests code along "production" code and I think I'm doing something wrong here.

The directory structure is:

utils_common_prj             (root project directory)
    utils_common             (directory, actual code package)
        __init__.py          (empty file)
        data_convert.py      (some module with a class with static methods)
        ...                  (other modules with code)
    tests                    (directory, test code package)
        __init__.py.         (empty file)
        test_data_convert.py (unit test class, testing class DataConvert)
        ...                  (other unit tests)
    setup.py                 (file, used to create the .tar.gz dist package)
    .pylintrc
    .gitignore

So I run unit tests with code coverage using coverage.py like so: having current directory utils_common_prj I run:

coverage run --source=. --omit=setup.py -m unittest discover

Followed by

coverage report

What I get is:

Name                                      Stmts   Miss  Cover
-------------------------------------------------------------
utils_common/__init__.py                      0      0   100%
utils_common/data_convert.py                 10      0   100%
...
tests/__init__.py                             0      0   100%
tests/test_data_convert.py                   39      0   100%
...
-------------------------------------------------------------
TOTAL                                       222      0   100%

I am getting statement count and percentage from test code along with actual code and I don't think this is desirable.

Coming from a .NET and Java background this seems like an error. Is it an error? If so, how should I fix it?

7
  • 2
    Try omitting the test files from coverage. Commented Apr 1, 2020 at 19:57
  • @FredLarson: I tried to make the omit option like so: --omit=setup.py,tests/* but it doesn't accept the star and without the star it doesn't change anything... Any hints for the omit syntax? Commented Apr 1, 2020 at 20:00
  • I don't have anything other than what I linked. Commented Apr 1, 2020 at 20:00
  • coverage run --source=. --omit=setup.py,*test* -m unittest discover leads to zsh: no matches found: --omit=setup.py,*test*. However, placing the omit value in quotes does the trick! Like so: coverage run --source=. --omit='setup.py,*test*' -m unittest discover It works now! Thanks! Commented Apr 1, 2020 at 20:04
  • @FredLarson : You can put this above information as an answer and I will accept it. Commented Apr 1, 2020 at 20:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.