如何获得100%的模型覆盖率测试?

时间:2014-10-06 15:36:04

标签: python django

我已经安装了我的Django项目。输出是:

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

我已经测试了我能想到的那个模型,5错过了什么?

这是我的模特:

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()

我的测试:

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)

3 个答案:

答案 0 :(得分:4)

使用html output运行coverage实用程序,它会告诉您未能测试的行或分支。

如果你使用django-nose来运行测试,add the --cover-html and --cover-html-dir=<DIR> option to NOSE_ARGS settings

请参阅承保文档中的example output

答案 1 :(得分:1)

您的测试无法涵盖5个陈述,通常在条件之后。例如,如果您有:

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

如果你没有尝试val = 3,那么将会有一个不会被测试覆盖的声明。

您的测试无关紧要。重要的是他们对您正在测试的项目所做的工作。您应该检查覆盖范围,我建议您执行cov.html_report,因为我已经习惯了覆盖模块。 html_report将生成一个很好的html文件夹,其中包含正在测试的项目中的数据,您可以看到缺少的内容。

示例:

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')

答案 2 :(得分:1)

如果您使用的是django-nose,则会有issue with wrong coverage for models

在没有Django插件的情况下使用coverage本身会显示正确的覆盖结果:

coverage erase
coverage run --branch ./manage.py test
coverage report -m