4

I have a Django project with multiple applications. In one application, I have a library already tested using unittest + coverage. When I run the test of the django project, I would like to omit this folder.

My project architecture is :

project/
├── application1/
│   ├── tests.py
│   ├── views.py
│   ├── ...
│   └── my_lib/ << The lib I want to omit
│       ├── tests.py
│       ├── script.py
│       ├── __init__.py
│       └── ...
├── application2/
│   ├── tests.py
│   ├── views.py
│   └── ...
├── application3/
│   ├── tests.py
│   ├── views.py
│   └── ...
├── .coveragerc
├── runtest.bat
├── manage.py
└── ...

runtest.bat is :

CALL activate ./venv
coverage erase
coverage run manage.py test
coverage html
PAUSE

Based on several tutorials and SO questions, I've tried several .coveragerc files but none of them properly skipped the library. Testing it creates a fail because it tries to load with the incorrect relative path.

.coveragerc is :

[run]
omit =
    */application1/my_lib/*
    
[report]

exclude_lines =
    if __name__ == .__main__.:
show_missing = True

I also tried :

[run]
source = .
omit =
    /application1/my_lib/*
[run]
source = project/*
omit =
    */application1/my_lib/*
[run]
source = .
omit =
    */application1/my_lib/*

Do you have any clue what am I doing wrong ?

For information :

  • Django version is 2.2.5
  • Coverage version is 4.5.4
  • Python version is 3.7

Few sources :

Thanks in advance


EDIT 1:

Just as a background. my_lib is mainly a file containing a class. This code is just embedded in the application to be used "like" a standard library.

In application1/views.py, I simply have a from . import my_lib.

In application1/my_lib/__init__.py, I have from .script import MyClass

The objective is simply to be able then to use in my view with my_lib.MyClass.do_something()

Now the reason why I would like to exclude this "library" from the coverage is because this was developped out of the application. It has his own unittest in applications1/my_lib/tests/py starting with from script import MyClass.

When I run the coverage in the root of the project, Python cannot find script.py in the root of the project so it triggers the error

File "path_to/project/application1/my_lib/tests.py", line 3

from script import MyClass

ModuleNotFoundError: No module named script.

In the worst case scenario, I could put this library to site-packages of my virtual environment bu I would like to avoid it (there will be probably multiple similar my_lib with at most 1 per application)


EDIT 2:

As a temporary solution, I simply renamed application1/my_lib/tests.py by application1/my_lib/script_tests.py. There is no file starting by test so the coverage does not care about this folder anymore.

The other alternative to run the test at the same time as the project required quite a lot of updates due to relative path used to several files

Community
  • 1
  • 1
Nicolas M.
  • 1,472
  • 1
  • 13
  • 26
  • Could you try: ```ini [run] omit = application1/my_lib/* ``` Without leading start and without `source`. It should do the trick. – matt.LLVW Dec 09 '19 at 14:40
  • Thanks for your time and proposal. Unfortunately, I still have the error `from my_lib import Foo ModuleNotFoundError: No module named my_lib` I have no clue if there is a way to ensure that the config file is properly considered – Nicolas M. Dec 09 '19 at 14:56
  • Your problem is not that the file isn't omitted from the test coverage, It's that you have an import problem. omiting it from the coverage won't make it go away. – matt.LLVW Dec 09 '19 at 15:04
  • Can you show the full error report? What is causing that import error? – Ned Batchelder Dec 09 '19 at 17:14
  • I've added a part in the question to explain more in details the error. The idea behind omitting the folder is to avoid testing *tests.py* in my_lib leading to this import error – Nicolas M. Dec 10 '19 at 05:56

0 Answers0