python - 如何在我的模型覆盖率测试中获得 100%?

标签 python django

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

     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)

最佳答案

使用 html output 运行覆盖实用程序它会告诉您哪些线路或分支没有测试成功。

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

参见 example output来自覆盖文档。

关于python - 如何在我的模型覆盖率测试中获得 100%?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26219736/

相关文章:

python - 流量控制和失败 : Database access not allowed, 使用 "django_db".... 错误

django - 通过 Django Rest Framework 将有效的 JSON 保存到 JSONField

python - Django /Jinja2 : How to use the index value in a for-loop statement to display two lists?

python - 使用 get() django 返回多个对象

c# - Python os.walk 返回的文件少于 C# Directory.GetFiles

python - `numpy.log1p( )` 的用途?

Django FormView : distinguishing between create and update

python - 从 Django 登录表单中的电子邮件中删除区分大小写

python - 使用 sqlalchemy 库时如何捕获访问被拒绝错误?

python - 如何根据字符串(如果存在)将列拆分为单独的列