Django rest框架的覆盖率错误报告

2024-04-19 10:42:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Django Rest框架创建简单的api,并使用类似于书Two Scopes of Django 3.x中的结构。我尝试在我的应用程序中创建简单的测试和覆盖报告,几乎覆盖了所有python源代码。即使我完全取消了那个测试。我尝试了pytest、nose和我在这里发现的任何东西,结果仍然一样

我的项目结构如下:

.
├── api
│   ├── apps.py
│   ├── __init__.py
│   ├── urls.py
│   └── v1
│       ├── admin.py
│       ├── __init__.py
│       ├── migrations
│       ├── models.py
│       ├── serializers.py
│       ├── tests
│       │   ├── __init__.py
│       │   ├── test_api.py
│       │   ├── test_models.py
│       │   ├── test_serializers.py
│       │   └── test_views.py
│       ├── urls.py
│       └── views.py
├── config
│   ├── asgi.py
│   ├── __init__.py
│   ├── settings
│   │   ├── base.py
│   │   ├── dev.py
│   │   ├── __init__.py
│   │   ├── production.py
│   │   └── test.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── README.md
├── requirements
│   ├── base.txt
│   ├── dev.txt
│   ├── production.txt
│   └── test.txt
└── venv

我还尝试通过以下更改来修改manage.py

    is_testing = 'test' in sys.argv
    if is_testing:
        import coverage
        cov = coverage.coverage(source=['api'], omit=['*/tests/*'])
        cov.erase()
        cov.start()
    execute_from_command_line(sys.argv)

    if is_testing:
        cov.stop()
        cov.save()
        cov.report()

但这也无济于事

以下是覆盖率的示例结果:

(venv) ➜  project ./manage.py test
System check identified no issues (0 silenced).

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
Name                                Stmts   Miss  Cover
-------------------------------------------------------
api/__init__.py                         0      0   100%
api/apps.py                             3      3     0%
api/urls.py                             2      0   100%
api/v1/__init__.py                      0      0   100%
api/v1/admin.py                         4      0   100%
api/v1/migrations/0001_initial.py      10     10     0%
api/v1/migrations/__init__.py           0      0   100%
api/v1/models.py                       38      3    92%
api/v1/serializers.py                  23      0   100%
api/v1/urls.py                         10      0   100%
api/v1/views.py                        19      0   100%
-------------------------------------------------------
TOTAL                                 109     16    85%

测试是:

from django.urls import reverse
from rest_framework import status

from rest_framework.test import APITestCase
from rest_framework_simplejwt.tokens import RefreshToken

from api.v1.models import User


class BrandTests(APITestCase):
    def setUp(self) -> None:
        self.user = User.objects.create(username='testUser', password='testPass')
        self.token = RefreshToken.for_user(self.user).access_token
        self.client.credentials(HTTP_AUTHORIZATION='JWT ' + str(self.token))

    def test_create_brand(self):
        """
        Ensure that we can create brand
        """
        url = reverse('brand-list')
        response = self.client.get(url)
        self.assertEqual(status.HTTP_200_OK, response.status_code)

编辑:我在django中创建了一个简单的应用程序,我得到了相同的结果。因此,它既不是由项目结构引起的,也不是由django+Djangorest框架组合引起的

有人面临同样的问题吗


Tags: frompytestimportselftxtapiinit
2条回答

请记住,导入模块将运行该模块的所有顶级语句。因此,仅导入一个模块就可以覆盖该模块中100%的导入/class/def行。如果您从一个几乎为空的项目开始,那么其中的大多数行都是这类行,覆盖率将非常高

忽略您不希望覆盖的内容(或您认为不应该覆盖的内容),例如迁移和设置。A.coveragerc可以帮助您控制要覆盖的内容。示例.coveragerc文件可能包含以下内容

[run]
source = '.'
omit =
    *tests*
    *apps.py
    *manage.py
    *__init__.py
    *migrations*
    *asgi*
    *wsgi*
    *admin.py
    *urls.py
    */settings/*

[report]
omit =
    *tests*
    *apps.py
    *manage.py
    *__init__.py
    *migrations*
    *asgi*
    *wsgi*
    *admin.py
    *urls.py
    */settings/*

fail_under = 98
show_missing = True
skip_covered = True

相关问题 更多 >