Coverage 错误地报告了 Django rest 框架

问题描述

我尝试使用 Django Rest Framework 创建简单的 api,并使用书籍 Two Scopes of Django 3.x 中的结构。我尝试创建简单的测试和覆盖率报告,几乎 100% 覆盖了我的应用程序中的所有 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+djangorestframework组合的项目结构造成的

有人面临同样的问题吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)