Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get 100% in my coverage tests for a model?

Tags:

python

django

I have installed coverage on my Django project. The output is:

     Name                               Stmts   Miss  Cover   
    ----------------------------------------------------------------
    test.apps.testapp.models.company     15      5    67%   2, 19-25
    ------------------------------------------------------------

I have tested everything I can think of for that model, what is the 5 missed referring to?

Here is my model:

class Company(models.Model):
    """
    Describes a Company within the system.
    """
    name = models.CharField(max_length=60)
    address_line1 = models.CharField(max_length=120)
    address_line2 = models.CharField(max_length=120, null=True, blank=True)
    address_city = models.CharField(max_length=120)
    address_county = models.CharField(max_length=120, null=True, blank=True)
    address_country = models.CharField(max_length=4, choices=COUNTRY_CHOICES, default="US")
    address_postcode = models.CharField(max_length=12)

    class Meta:
        app_label = "testapp"

    def company_user_count(self):
        """
        Return count of the numbers of users that belong to a company.
        :return: int
        """
        return self.users.count()

My Tests:

class CompanyModel(TestCase):

    def setUp(self):
        self.company = CompanyFactory.create()

    def tearDown(self):
        pass

    def test_create_new_company_creation(self):
        """
        Ensure that a new company can be created.
        """
        company = CompanyFactory(name="Test Co")
        noz.assert_equal(company.name, "Test Co")

    def test_user_is_company(self):
        """
        Test relationship on user to company method is_company_user().
        """
        company = CompanyFactory.create()
        company_user = UserFactory.create(company=company)
        noz.assert_equal(company_user.is_company_user(), True)

    def test_company_user_relationship(self):
        """
        Test correct relationship on company is made to user.
        """
        company = CompanyFactory.create()
        user = UserFactory.create(company=company)
        noz.assert_equal(user.company.name, "Valhalla Ltd")


    def test_one_to_many_company_relationship(self):
        """
        Test company relationship of one-to-many with users.
        """
        company = CompanyFactory.create()
        user1 = UserFactory.create(company=company)
        user2 = UserFactory.create(company=company)
        company.company_user_count()
        noz.assert_equal(company.company_user_count(), 2)
like image 717
Prometheus Avatar asked Oct 06 '14 15:10

Prometheus


2 Answers

Run the coverage utility with html output and it will tell you what lines or branches you've failed to test.

If you're using django-nose to run your tests, add the --cover-html and --cover-html-dir=<DIR> option to NOSE_ARGS settings.

See the example output from the coverage documentation.

like image 177
Joe Holloway Avatar answered Sep 27 '22 19:09

Joe Holloway


There are 5 statements that your test doesn't cover, typically behind a conditional. For example, if you have:

if val == 3:
    return 0
else:
    return 1

And you don't try val = 3, then there will be a statement that will not be covered by the test.

Your tests are irrelevant. What is important is what they are doing to your project being tested. You should check the coverage, I recommend doing a cov.html_report because I am used to the coverage module. The html_report will yield a nice html folder full of data from the project being tested, and you could see what is missing.

Example:

import coverage
cov = coverage.coverage( ...)
cov.start()

# do something about unittest and running the test

cov.stop()
cov.save()
cov.html_report(directory='./html_folder')
like image 42
MariusSiuram Avatar answered Sep 27 '22 19:09

MariusSiuram