简体   繁体   English

Nosetests 和 Coverage 不排除线路

[英]Nosetests and Coverage not excluding lines

Running into a small problem with some code coverage using nosetests and coverage with a Django web application.使用 Django Web 应用程序使用鼻子测试和覆盖率遇到一些代码覆盖率的小问题。 I have created a .coveragerc file to exclude a huge amount of code (things like class declarations) but I'm still getting some weird results.我创建了一个 .coveragerc 文件来排除大量代码(比如类声明),但我仍然得到一些奇怪的结果。

Here is my .coveragerc file:这是我的 .coveragerc 文件:

[run]
omit = ../*migrations*, ../*admin.py

[report]
show_missing = True
exclude_lines =
         pragma: no cover
         from
         = models\.

This is an example of one of the models.py files:这是 models.py 文件之一的示例:

from django.db import models

class Query(models.Model):
    variable1 = models.CharField(max_length=100)
    variable2 = models.CharField(max_length=100)
    variable3 = models.CharField(max_length=100)
    variable4 = models.CharField(max_length=100)
    variable5 = models.CharField(max_length=100)
    id = models.AutoField(primary_key=True)

def some_function(self):
     self.variable1 = self.variable2 + self.variable3 + self.variable4 + self.variable 5
     return self.variable1

So when I run code coverage, the issue I run into is that despite me telling coverage to explicitly exclude anything with the string "= models.", it still says the lines are missing in the report given through the command line.因此,当我运行代码覆盖率时,我遇到的问题是,尽管我告诉覆盖率明确排除带有字符串“= models.”的任何内容,但它仍然表示通过命令行给出的报告中缺少这些行。 This is making it very hard to determine which lines I'm actually failing to cover in my test cases.这使得很难确定我的测试用例中实际上没有涵盖哪些行。 Can anyone offer some insight to this?任何人都可以对此提供一些见解吗?

Your .coveragerc file should list things to exclude starting from the root of your directory.您的.coveragerc文件应列出要从目录根目录开始排除的内容。

For example:例如:

proj
|-- app1
   |
   -- models.py
   -- migrations.py
|-- app2

Then your coverage.rc file should look like:然后您的coverage.rc文件应该如下所示:

[run]
omit = app1/migrations.py, app1/admin.py

or

[run]
omit = proj/*/migrations.py, proj/*/admin.py

Found the solution to my problem.找到了我的问题的解决方案。 It turns out I don't need to use nosetests at all.事实证明我根本不需要使用nosetests I can simply run the coverage.py with manage.py test and pass in the test modules.我可以简单地使用manage.py测试运行coverage.py并传入测试模块。 The code coverage worked great and I'm up to 96% coverage :-)代码覆盖率很好,我的覆盖率高达 96% :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM