下面是我当前的coverage.py报告的截图。
我不确定如何获得此模型的100%覆盖率。如何测试第13-20行?在我tests_model.py
我假设创建一个实例时,这将被覆盖。但事实并非如此。
# 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)
我试图在同一个测试文件中测试每个属性拥有自己的属性,例如......
def test_name(self):
company = mommy.make(Company, name="Test Name")
noz.assert_equal(company.name, "Test Name")
但这对我的报道分数没有任何影响。
根据评论,我也试过这个:
def test_name(self):
company = mommy.make(Company)
company.name = "Test"
company.save()
noz.assert_equal(company.name, "Test")
但同样,这对得分没有影响。
这些是我的测试设置......
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'
]
控制台输出:
> 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%
----------------------------------------------------------------------
目录:
testapp/
manage.py
testapp/
__init__.py
apps/
__init__.py
referrals/
__init__.py
tests/
__init__.py
model_tests.py
答案 0 :(得分:2)
请参阅django_coverage项目页面上的this issue。
另外,请查看coverage.py official FAQ,特别是这个:
Q: Why do the bodies of functions (or classes) show as executed,
but the def lines do not?
似乎在实际导入模型之后启动了覆盖机制。
尝试回到标准 Django测试场景(使用内置测试运行器)并通过发出以下命令手动运行coverage:
coverage run --source='.' ./manage.py test
coverage report
coverage html
查看报告是否不同。