为什么coverage不报告任何有关Djangos views.py的内容?

时间:2017-04-11 09:52:36

标签: django python-2.7 unit-testing coverage.py

我在Django应用程序中有一个基于类的视图,看起来像这样:

class CBView(View):

    def get(self, request, client, *args, **kwargs):
        output1 = self.method1(argument1)
        output2 = self.method2(argument2)
        # Rest of the method implementation
        ...

        return response

    def method1(self, argument1):
        # Implementation
        ... 

        return output1

    def method2(self, argument2):
        # Implementation
        ...

        return output2 

我正在尝试为'简单'类方法编写单元测试,即method1method2。测试看起来像这样:

class TestCBView(TestCase):

    def setUp(self):
        self.view = CBView()

    def test_method1(self):
        # Testing that output1 is as expected
        ...
        output1 = self.view.method1(argument1)
        ...
        self.assertEquals(output1, expected_output1)

    def test_method2(self):
        # Testing that output2 is as expected
        ...
        output2 = self.view.method2(argument2)
        ...
        self.assertEquals(output2, expected_output2)

之后,我跑:

coverage run ./manage.py test django_app.tests.test_cbview

成功运行所有测试,然后我尝试运行:

coverage report -m django_app/views.py

我得到了:

Name    Stmts   Miss  Cover   Missing
-------------------------------------
No data to report.

我做错了吗?

我正在使用Coverage.py,版本4.0.3。,Django 1.8.15和Python 2.7.13。

1 个答案:

答案 0 :(得分:-1)

使用coverage运行测试时,它会生成.coverage文件,该文件为私有格式,不打算直接读取。此文件包含原始报告,用于以控制台或html格式coverage html为您显示报告。因此,通常如果运行了测试,.coverage文件必须位于启动命令coverage run ./manage.py test django_app.tests.test_cbview的目录中,然后在该目录下,您可以点击coverage report ...并且应该工作