Python Forum
how to get improve coverage while using coverage.py
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get improve coverage while using coverage.py
#1
Lightbulb 
Hello,

I am having a hard time getting a grasp of coverage.py
I've made the mistake to start writing code one month ago and almost completed the application with zero tests written.
I have started writing tests this monday and I am trying to get an understanding of how it works.

I am trying to get a better test coverage before delivering this application but it seems that I can't get
any of the methods that are not covered with tests to pass from red to green.

The following code is showing the test and as well as the model manager. As my understanding goes, testing a method
is about calling this method and verifying that the expected output is as expected.


p.s. This is my fifth week with django/python and this project is due in two weeks.

#models.py 
class CitiesManager(models.Manager): 
    def top_cities(self): 
        return CModel.objects.values('location'). \ 
        annotate(c=Count('location')).order_by('-c')[:30] 

#tests.py I am using FactoryBoy to create mock models
class CitiesManagerTestCase(TestCase): 
    def setUp(self):
        self.CModel = CModel
        self.c_models = RandomCModelFactory.create_batch(400)
    
    def test_instance_of_manager(self):
        self.assertTrue(isinstance(self.CModel.objects, CitiesManager))

    #Top 30 cities with most casting candidates.
    def test_top_cities(self):
        self.assertEqual(list(self.CModel.objects.top_cities()),
            list(CModel.objects.values('location').annotate(c=Count('location')).order_by('-c')[:30]))
Here is the strange thing.

In the following code, age property is said to be covered, whilst
weight that I've tested with the asserstion below is said not to pass and I can't get it to change its state
from uncovered to covered. P.S. I didn't test age property, it was covered right off the bat.

#models.py
    @property
    def age(self):
        today = date.today()
        return today.year - self.date_of_birth.year - \
                ((today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day))
                
    @property
    def weight_lbs(self):
        return int(int(self.weight) * 2.2)

#tests.py I am not testing age and yet it is said to have coverage
  self.assertEqual(self.cmodel.weight_lbs, int(int(self.cmodel.weight)*2.2))
First time since I've started django that I've got stuck for so long. I searched the web, stackoverflow and this same forum but I can't get my head around this coverage.py. I am stuck since Monday and I don't really understand what's going on. If anyone can point me in the right direction, I'll be really thankful.

UPDATE:
Magic happens.
1 minute after writing this question, I've got an 'aha' moment.
I was running:
python manage.py test
instead of:
coverage run --source='.' manage.py test cmodels

Three days and only when I took the time to write this question that I've found the answer.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Looking for good doc on Scraping coverage algorithms Larz60+ 0 1,903 Jan-05-2019, 03:22 PM
Last Post: Larz60+
  Calculate the fewest zip codes, for the largest coverage nilamo 4 6,997 Mar-23-2017, 01:31 PM
Last Post: Bass

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020