feldroy / django-crash-course

The issue tracker and code repository for Django Crash Course
https://www.feldroy.com/products/django-crash-course
144 stars 30 forks source link

Chapter 53 - Test All the Cheese Views - test_views.py coverage goes up after each test #340

Closed firepol closed 4 years ago

firepol commented 4 years ago

Location within the Book

Description

I noticed that after adding the test_good_cheese_list_view test, the code coverage decreases. In theory if you add more tests, either the % coverage should go up or stay at least the same (if it's a useless test) no?

Check this out:

(everycheese) firepol@hercules:~/everycheese$ coverage run -m pytest
Test session starts (platform: linux, Python 3.8.5, pytest 5.4.2, pytest-sugar 0.9.3)
django: settings: config.settings.test (from option)
rootdir: /home/firepol/everycheese, inifile: pytest.ini
plugins: sugar-0.9.3, django-test-plus-1.4.0, Faker-4.1.1, django-3.9.0
collecting ... 
 everycheese/cheeses/tests/test_models.py ✓✓                                                                                  17% █▋        
 everycheese/users/tests/test_forms.py ✓                                                                                      25% ██▌       
 everycheese/users/tests/test_models.py ✓                                                                                     33% ███▍      
 everycheese/users/tests/test_urls.py ✓✓✓                                                                                     58% █████▉    
 everycheese/users/tests/test_views.py ✓✓✓✓✓                                                                                 100% ██████████

Results (0.59s):
      12 passed
Can't add file tracer data for unmeasured file '/home/firepol/everycheese/everycheese/templates/cheeses/cheese_list.html'
(everycheese) firepol@hercules:~/everycheese$ coverage run -m pytest
Test session starts (platform: linux, Python 3.8.5, pytest 5.4.2, pytest-sugar 0.9.3)
django: settings: config.settings.test (from option)
rootdir: /home/firepol/everycheese, inifile: pytest.ini
plugins: sugar-0.9.3, django-test-plus-1.4.0, Faker-4.1.1, django-3.9.0
collecting ... 
 everycheese/cheeses/tests/test_models.py ✓✓                                                                                  15% █▋        
 everycheese/users/tests/test_forms.py ✓                                                                                      23% ██▍       
 everycheese/users/tests/test_models.py ✓                                                                                     31% ███▏      
 everycheese/users/tests/test_urls.py ✓✓✓                                                                                     54% █████▍    
 everycheese/users/tests/test_views.py ✓✓✓✓✓✓                                                                                100% ██████████

Results (0.58s):
      13 passed
Can't add file tracer data for unmeasured file '/home/everycheese/everycheese/templates/cheeses/cheese_list.html'

Note 1st: from 17% goes to 15%. Note 2nd: from 25% goes to 23% etc.

After adding the test_good_cheese_detail_view the % coverage goes down even more... why?

Actually, after adding each test and running the coverage command, the % of all the other files except test_views.py (this one stays 100% all the time) goes down... why?

Possible Solutions

I'd like to hear a possible solution from the experts for this... also it would be nice to explain this in the book, becaus I'm sure many attentive readers will notice this behavior and wonder why.

I guess that by adding code, the amount of code goes up, but I thought there should be some smart way to make coverage understand to ignore tests, I mean code in tests in not code that need to be tested.

Note: all tests in test_views.py that have been added in this chapter are not inside a class, like the others in the same file, see class TestUserUpdateView and class TestUserRedirectView can this be the reason?

Your full name so we can provide accurate credit within the book

Paolo Brocco

pydanny commented 4 years ago

I'm not able to replicate this issue. If I am in error, please reopen this issue.