2014-10-06 99 views
3

我已經在我的Django項目上安裝了覆蓋範圍。輸出是:如何在我的覆蓋測試中獲得100%的模型?

 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) 
+0

哪些線19-25在這個文件? – geoffspear 2014-10-06 15:39:02

回答

3

運行與html output覆蓋工具,它會告訴你你已經失敗線或分支機構進行測試。

如果您使用django-nose來運行您的測試,add the --cover-html and --cover-html-dir=<DIR> option to NOSE_ARGS settings

請參閱覆蓋範圍文檔中的example output

+0

因爲我使用的是Django,有一種方法可以在Django(django nose)下運行它,例如manage.py coverage x – Prometheus 2014-10-06 15:41:51

+1

「如果你想要一個漂亮的HTML報告,請加入--cover-html選項到你的NOSE_ARGS源代碼中缺少的行。「 – 2014-10-06 15:46:16

+0

只是想出了 - 覆蓋-HTML謝謝。 – Prometheus 2014-10-06 15:50:45

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

如果使用的是django-nose,有一個issue with wrong coverage for models

使用覆蓋面本身沒有Django的插件,顯示了覆蓋了不錯的成績:

coverage erase 
coverage run --branch ./manage.py test 
coverage report -m 
相關問題