Detect missing unittests

I am refactoring a legacy Python project, which did not have any unittests.
Assuming a directory structure like this:

C:.
├───scripts
│       schema.sql
├───src
│   │   .coverage
│   │   bar.py
│   │   baz.py
│   │   foo.py
│   │   __init__.py
│   └───utils
│           __init__.py
└───tests
        .coverage
        test_foo.py

foo.py looks like:

import bar
import baz
import requests
import psycopg2
import os
import sys

print(requests.__file__)


def foo():
    return 'foo'


if __name__ == '__main__':
    print(foo())

test_foo.py looks like:

import unittest
import foo


class Test_Foo(unittest.TestCase):
    def test_foo(self):
        result = foo.foo()
        self.assertEqual(result, 'foo')

I’ve just defined tests for foo.py, running the unittests:

python.exe -m unittest discover -s . && coverage report -m
C:data...venvlibsite-packagesrequests__init__.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
Name                           Stmts   Miss  Cover   Missing
------------------------------------------------------------
C:datamyProjectsrcbar.py       2      1    50%   2
C:datamyProjectsrcfoo.py      11      4    64%   6-8, 12, 16
------------------------------------------------------------
TOTAL                             13      5    62%

Using nose, the output shows missing tests for packages within ‘site-packages’ as well which I dont want to see:

nosetests -w ..src --cover-inclusive  --cover-tests --with-coverage --cover-erase --cover-html

Name                                    Stmts   Miss  Cover
-----------------------------------------------------------
bar.py                                      2      1    50%
baz.py                                      2      1    50%
certifi__init__.py                         2      2     0%
...
decimal.py                                 10     10     0%
encodingsidna.py                         180    180     0%
hmac.py                                    60     60     0%
httpclient.py                            756    756     0%
httpclient.py                            756    756     0%
httpcookiejar.py                        1102   1102     0%
httpcookies.py                           248    248     0%
idna__init__.py                            4      4     0%
idna__init__.py                            4      4     0%
idnacore.py                              291    291     0%
idnacore.py                              291    291     0%
idnaidnadata.py                            4      4     0%
idnaidnadata.py                            4      4     0%
idnaintranges.py                          30     30     0%
idnaintranges.py                          30     30     0%
idnapackage_data.py                        1      1     0%
idnapackage_data.py                        1      1     0%
mimetypes.py                              194    194     0%
numbers.py                                134    134     0%
psycopg2__init__.py                       22     22     0%
...
src__init__.py                             0      0   100%
srcbar.py                                  2      1    50%
srcbaz.py                                  2      1    50%
srcfoo.py                                 11      2    82%
srcutils__init__.py                       0      0   100%
stringprep.py                              65     65     0%
urllib3__init__.py                        34     34     0%
...
-----------------------------------------------------------
TOTAL                                   14613  14600     1%
----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

How do I see a regular unittest/coverage report and additionally files within the working directory which haven’t been tested ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You could use pytest.
Recreating your directory structure and optionally removing import baz in foo.py:

import bar                    
# import baz                  
import requests               
import psycopg2               
import os                     
import sys                    
                              
print(requests.__file__)      
                              
                              
def foo():                    
    return 'foo'              
                              
                              
if __name__ == '__main__':    
    print(foo())

Executing pytest:

pytest --cov-report html:cov_html --cov-report term-missing --cov=src tests/

Output:

===== test session starts 
... omitted output ...
collected 1 item

teststest_foo.py .                                                                                                                                                                              [100%]

----------- coverage: platform win32, python 3.7.9 -----------
Name                    Stmts   Miss  Cover   Missing
-----------------------------------------------------
src__init__.py             0      0   100%
srcbar.py                  2      1    50%   2
srcbaz.py                  2      2     0%   2-3
srcfoo.py                 10      1    90%   16
srcutils__init__.py       0      0   100%
-----------------------------------------------------
TOTAL                      14      4    71%
Coverage HTML written to dir cov_html

pytest shows 0% coverage for baz.py


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x