Monday 15 September 2014

python - Django Testing Model Attributes -



python - Django Testing Model Attributes -

below screenshot of current coverage.py report.

i unsure how 100% coverage model. how test lines 13-20? in tests_model.py assumed creating instance covered. not case.

# core django imports django.test import testcase # third-party app imports import nose.tools noz model_mommy import mommy model_mommy.recipe import recipe, foreign_key # app imports ..models.company import company 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 one-to-many 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 test each attributes own own in same test file, example...

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

but has no impact whatsoever on coverage score.

based on comments have tried this:

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

but again, had no impact on score.

these settings 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

see this issue on django_coverage project page.

also, take coverage.py official faq, in particular one:

q: why bodies of functions (or classes) show executed, def lines not?

seems coverage machinery started after model imported.

try standard django testing scenario (using builtin test runner) , run coverage manually issuing these commands:

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

see if study different.

python django

No comments:

Post a Comment