webcompat.com icon indicating copy to clipboard operation
webcompat.com copied to clipboard

Python unittest coverage

Open karlcow opened this issue 5 years ago • 4 comments

I was testing the test coverage of our code for python. To experiment.

cd webcompat.com/
. env/bin/activate
pip install coverage

Then for the coverage

coverage run --source webcompat,tools -m nose tests 

which gives us an error (this to explore) while nosestests is ok.

...E....................................................................................................................................................
======================================================================
ERROR: Patching the issue is working only with certain circumstances.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1101, in _dot_lookup
    return getattr(thing, comp)
AttributeError: 'Blueprint' object has no attribute 'endpoints'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/karl/code/webcompat.com/tests/unit/test_api_urls.py", line 175, in test_api_patch_issue
    with patch('webcompat.api.endpoints.proxy_request') as github_data:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1257, in __enter__
    self.target = self.getter()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1427, in <lambda>
    getter = lambda: _importer(target)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1114, in _importer
    thing = _dot_lookup(thing, comp, import_path)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1104, in _dot_lookup
    return getattr(thing, comp)
AttributeError: 'Blueprint' object has no attribute 'endpoints'

----------------------------------------------------------------------
Ran 152 tests in 1.650s

FAILED (errors=1)

Here the nosetests results

nosetests
........................................................................................................................................................
----------------------------------------------------------------------
Ran 152 tests in 1.120s

OK

Once we ran coverage, we can display a report:

coverage report -m 

which is not too bad.

                                 
Name                                   Stmts   Miss  Cover   Missing
--------------------------------------------------------------------
tools/__init__.py                          0      0   100%
tools/changelog.py                        78     33    58%   50-52, 68-69, 94-134, 139
tools/daily_labels.py                     60     60     0%   6-108
tools/labels.py                           41     41     0%   7-74
tools/topsites.py                        138     50    64%   79-82, 90-112, 117-143, 256-276
webcompat/__init__.py                     24      0   100%
webcompat/api/__init__.py                  0      0   100%
webcompat/api/endpoints.py                99     31    69%   66-67, 77-89, 109-116, 127-129, 131-132, 162-168, 181-182, 200-202
webcompat/api/uploads.py                 107      4    96%   106, 118, 150, 187
webcompat/db/__init__.py                  41      2    95%   51-55
webcompat/error_handlers/__init__.py      34      7    79%   59, 62-64, 78-83
webcompat/form.py                        224     19    92%   174-189, 406, 408, 444, 503, 542, 546, 554
webcompat/helpers.py                     393     50    87%   102-104, 215-216, 229-233, 244-247, 377-378, 383-389, 404-423, 439, 458, 479, 498, 682-686, 694, 702, 712, 740, 767
webcompat/issues.py                       48      4    92%   99-104
webcompat/views.py                       310     71    77%   87-89, 96, 110-113, 119-121, 133-134, 137-138, 142, 153-162, 212, 262, 270-271, 288-289, 294, 305-307, 313-315, 322-334, 354, 356-357, 366-367, 384, 389, 412, 418-419, 427, 436, 445, 454, 463, 472, 481, 490, 499, 508, 517, 526, 575, 592, 615
webcompat/webhooks/__init__.py            19      0   100%
webcompat/webhooks/helpers.py            194      4    98%   265-266, 288-289
--------------------------------------------------------------------
TOTAL                                   1810    376    79%

if we focus on the ones which are less than 80%

  • [ ] tools/daily_labels.py will be fixed by #3176
  • [ ] tools/labels.py this was related to #557 when someone wants to create labels for a test repo. there is no test. Created #3202
  • [ ] tools/topsites.py There are still missing tests there. #3203
  • [ ] webcompat/api/endpoints.py is probably the most urgent. #3204
  • [ ] webcompat/views.py finally this one is also lacking tests. (77%)
  • [ ] webcompat/error_handlers/init.py

Probably helpers would benefit of 100% coverage.

karlcow avatar Feb 12 '20 07:02 karlcow

In the universe of pytest, running the test coverage if we were adding this later.

cd webcompat.com/
. env/bin/activate
pip install coverage
coverage run --source=webcompat,tools -m pytest tests/unit/
coverage report -m

as of today:

Name                                   Stmts   Miss  Cover   Missing
--------------------------------------------------------------------
tools/__init__.py                          0      0   100%
tools/changelog.py                        77     33    57%   50-52, 68-69, 96-136, 141
tools/daily_labels.py                     60     60     0%   6-108
tools/labels.py                           41     41     0%   7-74
tools/topsites.py                        138     50    64%   79-82, 90-112, 117-143, 256-276
webcompat/__init__.py                     25      0   100%
webcompat/api/__init__.py                  0      0   100%
webcompat/api/endpoints.py               100     30    70%   79-91, 110-117, 127-129, 131-132, 161-167, 181-184, 197, 209-211
webcompat/api/helpers.py                  16      0   100%
webcompat/api/uploads.py                 107      3    97%   106, 118, 187
webcompat/db/__init__.py                  41      2    95%   51-55
webcompat/error_handlers/__init__.py      34      7    79%   59, 62-64, 78-83
webcompat/form.py                        216      7    97%   388, 390, 426, 485, 524, 528, 536
webcompat/helpers.py                     392     31    92%   99-101, 217-218, 231-235, 246-249, 379-380, 389-390, 418-419, 431-433, 453, 473, 495, 514, 701, 710, 718, 728, 756
webcompat/issues.py                       48      4    92%   99-104
webcompat/templates/__init__.py           65      1    98%   85
webcompat/views.py                       308     68    78%   89-91, 105-108, 114-116, 128-129, 132-133, 137, 148-157, 207, 257, 277-278, 283, 294-296, 302-304, 311-323, 343, 345-346, 358-359, 376, 381, 404, 410-411, 419, 428, 437, 446, 455, 464, 473, 482, 491, 500, 509, 518, 567, 584, 607
webcompat/webhooks/__init__.py            19      0   100%
webcompat/webhooks/helpers.py            210      4    98%   265-266, 296-297
--------------------------------------------------------------------
TOTAL                                   1897    341    82%

karlcow avatar May 29 '20 01:05 karlcow

progress on the issues.py module with test_issues.py. Now 100% done in #3322

(env) ~/code/webcompat.com % COLUMNS=50 coverage report -m                                            
Name                                   Stmts   Miss  Cover   Missing
--------------------------------------------------------------------
tools/__init__.py                          0      0   100%
tools/changelog.py                        77     33    57%   50-52, 68-69, 96-136, 141
tools/daily_labels.py                     60     60     0%   6-108
tools/labels.py                           41     41     0%   7-74
tools/topsites.py                        138     50    64%   79-82, 90-112, 117-143, 256-276
webcompat/__init__.py                     25      0   100%
webcompat/api/__init__.py                  0      0   100%
webcompat/api/endpoints.py               100     30    70%   79-91, 110-117, 127-129, 131-132, 161-167, 181-184, 197, 209-211
webcompat/api/helpers.py                  16      0   100%
webcompat/api/uploads.py                 107      3    97%   106, 118, 187
webcompat/db/__init__.py                  41      2    95%   51-55
webcompat/error_handlers/__init__.py      34      7    79%   59, 62-64, 78-83
webcompat/form.py                        216      7    97%   388, 390, 426, 485, 524, 528, 536
webcompat/helpers.py                     392     37    91%   99-101, 217-218, 231-235, 246-249, 379-380, 389-390, 413, 418-419, 431-433, 453, 471-479, 495, 514, 701, 710, 718, 728, 756
webcompat/issues.py                       48      0   100%
webcompat/templates/__init__.py           65      1    98%   85
webcompat/views.py                       308     68    78%   89-91, 105-108, 114-116, 128-129, 132-133, 137, 148-157, 207, 257, 277-278, 283, 294-296, 302-304, 311-323, 343, 345-346, 358-359, 376, 381, 404, 410-411, 419, 428, 437, 446, 455, 464, 473, 482, 491, 500, 509, 518, 567, 584, 607
webcompat/webhooks/__init__.py            19      0   100%
webcompat/webhooks/helpers.py            210      4    98%   265-266, 296-297
--------------------------------------------------------------------
TOTAL                                   1897    343    82%

karlcow avatar May 29 '20 08:05 karlcow

Name                                   Stmts   Miss  Cover   Missing
--------------------------------------------------------------------
tools/__init__.py                          0      0   100%
tools/changelog.py                        77     33    57%   50-52, 68-69, 96-136, 141
tools/daily_labels.py                     60     60     0%   6-108
tools/labels.py                           41     41     0%   7-74
tools/topsites.py                        138     50    64%   79-82, 90-112, 117-143, 256-276
webcompat/__init__.py                     25      0   100%
webcompat/api/__init__.py                  0      0   100%
webcompat/api/endpoints.py               108     30    72%   79-91, 110-117, 127-129, 131-132, 161-167, 181-184, 197, 209-211
webcompat/api/helpers.py                  16      0   100%
webcompat/api/uploads.py                 108      3    97%   106, 118, 187
webcompat/db/__init__.py                  41      2    95%   51-55
webcompat/error_handlers/__init__.py      36      6    83%   62-64, 78-83
webcompat/form.py                        216      7    97%   395, 397, 433, 492, 531, 535, 543
webcompat/helpers.py                     396     36    91%   99-101, 217-218, 231-235, 246-249, 379-380, 389-390, 413, 418-419, 431-433, 453, 471-479, 495, 701, 710, 718, 728, 756
webcompat/issues.py                       48      0   100%
webcompat/templates/__init__.py           78      1    99%   98
webcompat/views.py                       350     69    80%   89-91, 105-108, 114-116, 128-129, 132-133, 137, 148-157, 210, 268, 288-289, 294, 305-307, 313-315, 322-334, 358, 360-361, 373-374, 391, 396, 419, 425-426, 434, 443, 452, 461, 470, 479, 488, 497, 506, 515, 524, 533, 542, 591, 608, 631
webcompat/webhooks/__init__.py            20      0   100%
webcompat/webhooks/helpers.py            210      5    98%   107, 265-266, 296-297
--------------------------------------------------------------------
TOTAL                                   1968    343    83%

karlcow avatar Jul 01 '20 07:07 karlcow

% coverage run --source=webcompat,tools,config -m pytest
% coverage report -m
(env) ~/code/webcompat.com-karlcow % coverage report -m                                    
Name                                   Stmts   Miss  Cover   Missing
--------------------------------------------------------------------
config/__init__.py                       117     23    80%   57-72, 76-79, 94-95, 163-164, 181, 187, 259
config/environment.py                     65     26    60%   17, 28-39, 44-55, 144
tools/__init__.py                          0      0   100%
tools/archive/__init__.py                  0      0   100%
tools/archive/model.py                    61      9    85%   74-90
tools/changelog.py                        77     33    57%   50-52, 68-69, 96-136, 141
tools/daily_labels.py                     60     60     0%   6-108
tools/labels.py                           41     41     0%   7-74
tools/topsites.py                        138     50    64%   79-82, 90-112, 117-143, 256-276
webcompat/__init__.py                     25      0   100%
webcompat/api/__init__.py                  0      0   100%
webcompat/api/endpoints.py               108     30    72%   79-91, 110-117, 127-129, 131-132, 161-167, 181-184, 197, 209-211
webcompat/api/helpers.py                  16      0   100%
webcompat/api/uploads.py                 108      3    97%   106, 118, 187
webcompat/db/__init__.py                  41      2    95%   51-55
webcompat/error_handlers/__init__.py      36      6    83%   62-64, 78-83
webcompat/form.py                        221      7    97%   381, 383, 419, 500, 539, 543, 551
webcompat/helpers.py                     395     38    90%   95-100, 216-217, 230-234, 245-248, 378-379, 388-389, 412, 417-418, 430-432, 452, 470-478, 494, 700, 709, 717, 727, 755
webcompat/issues.py                       48      0   100%
webcompat/templates/__init__.py           82      1    99%   102
webcompat/views.py                       336     81    76%   65, 88-90, 96-107, 113-115, 124-138, 147-156, 184, 209, 267, 287-288, 293, 304-306, 312-314, 321-333, 357, 359-360, 390, 396-397, 405, 414, 423, 432, 441, 450, 459, 468, 477, 486, 495, 504, 513, 562, 579, 602
webcompat/webhooks/__init__.py            20      0   100%
webcompat/webhooks/helpers.py            210      5    98%   107, 265-266, 296-297
--------------------------------------------------------------------
TOTAL                                   2205    415    81%

karlcow avatar Aug 12 '20 02:08 karlcow