Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Testing Model Attributes

Tags:

python

django

Below is an screenshot of my current coverage.py report.

enter image description here

I am unsure how to get 100% coverage for this model. How do I test for lines 13-20? In my tests_model.py I assumed by creating an instance this would be covered. But this is not the case.

# Core Django imports
from django.test import TestCase

# Third-party app imports
import nose.tools as noz
from model_mommy import mommy
from model_mommy.recipe import Recipe, foreign_key


# app imports
from ..models.company import Company
from testapp.apps.profiles.models.appUserModel import AppUser

class CompanyModel(TestCase):
    def setUp(self):
        self.company = mommy.make(Company)
        noz.assert_true(isinstance(self.company, Company))


    def test_company_user_count_is_0(self):
        company = mommy.make(Company)
        noz.assert_equal(company.company_user_count(), 0)

    def test_company_user_count(self):
        # Relationship can be one-to-many with users.
        company = mommy.make(Company)
        user1, user2 = mommy.make(AppUser, _quantity=2)
        company.users.add(user1)
        company.users.add(user2)
        noz.assert_equal(company.company_user_count(), 2)


    def test_company_unicode(self):
        noz.assert_equal(self.company.__unicode__(), self.company.name)

I have tried to test each attributes own its own in the same test file, for example...

def test_name(self):
    company = mommy.make(Company, name="Test Name")
    noz.assert_equal(company.name, "Test Name")

But this has no impact whatsoever on my coverage score.

Based on comments I have also tried this:

def test_name(self):
    company = mommy.make(Company)
    company.name = "Test"
    company.save()
    noz.assert_equal(company.name, "Test")

But again, this had no impact on score.

These are my settings for tests...

INSTALLED_APPS += (

    'django_nose',
    'django_coverage',
    'django_extensions',

)

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

NOSE_ARGS = [
    '--with-coverage',
    '--cover-package=testapp.apps.profiles,testapp.apps.referrals',
    '--cover-html'

]

Console output:

> python manage.py test
.........
Name                                                               Stmts   Miss  Cover   Missing
------------------------------------------------------------------------------------------------
testapp.apps.referrals.models                                       1      1     0%   1
testapp.apps.referrals.models.company                               17     15    12%   1-24, 27
------------------------------------------------------------------------------------------------
TOTAL                                                                136     73    46%
----------------------------------------------------------------------

Directory:

testapp/
       manage.py
       testapp/
              __init__.py
              apps/
                  __init__.py
                  referrals/
                           __init__.py
                           tests/
                                __init__.py
                                model_tests.py
like image 886
Prometheus Avatar asked Jul 28 '26 15:07

Prometheus


1 Answers

See this issue on django_coverage project page.

Also, take a look to coverage.py official FAQ, in particular this one:

Q: Why do the bodies of functions (or classes) show as executed,
   but the def lines do not?

Seems that the coverage machinery is started after your model is actually imported.

Try to back to a standard Django testing scenario (using a builtin test runner) and run coverage manually by issuing these commands:

coverage run --source='.' ./manage.py test
coverage report
coverage html

See if the report is different.

like image 62
Augusto Destrero Avatar answered Jul 30 '26 04:07

Augusto Destrero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!