[英]Django coverage doesn't cover APITestCase tests
I have a relatively large Django + DRF project with over 400 tests, but I fail to get coverage metrics of over 40%.我有一个相对较大的 Django + DRF 项目,有超过 400 次测试,但我未能获得超过 40% 的覆盖率指标。
Here are the results after running tests.这是运行测试后的结果。
From my understanding, there could be a few sources of issues here:据我了解,这里可能有几个问题来源:
We've tinkered a bit with our directory structure, here is what it looks like today:我们已经对目录结构进行了一些修改,这是今天的样子:
core_app
- apis
- businessapi
- models
- migrations
- serializers
- views
- tests
- business_tests.py
- admin.py
- urls.py
- apps.py
- __init__.py
- userapi
- transactionapi
- ...
- settings
- production.py
- celery_apps
- ...
- ...
and here is what our business_tess.py file looks like:这是我们的 business_tess.py 文件的样子:
class TestBusiness(APITestCase):
def setUp(self):
self.businessA = BusinessFactory(...)
self.businessB = BusinessFactory(...)
self.primary_adminA = ProfileFactory(...)
self.primary_adminB = ProfileFactory(...)
# Create 10 spenders
self.spendersA = ProfileFactory.create_batch(...)
self.spendersB = ProfileFactory.create_batch(...)
# Create 5 admins
self.adminsA = ProfileFactory.create_batch(...)
self.adminsB = ProfileFactory.create_batch(...)
# Authorize
self.primary_admin_client = APIClient()
self.primary_admin_client.credentials(Accept="application/json")
self.primary_admin_client.force_authenticate(self.primary_adminA)
...
def test_business_permissions(self):
expected_response = {...}
sorted_expected = OrderedDict(sorted(expected_response.items()))
# Try to access as primaryadmin
_, client = authenticate_user(self.primary_adminA.id)
response = client.get("url_to_test")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json(), sorted_expected)
I personally don't think this is super likely because there are examples of tests like this one https://dev.to/lucasmiguelmac/pytest-with-django-rest-framework-from-zero-to-hero-8c4 Where api tests clearly work just fine.我个人认为这不太可能,因为有这样的测试示例https://dev.to/lucasmiguelmac/pytest-with-django-rest-framework-from-zero-to-hero-8c4其中 api测试显然工作得很好。
You must create an __init__.py
in your tests
folder to Django see that as a module.您必须在您的
tests
文件夹中创建一个__init__.py
到 Django 将其视为一个模块。 Then by running python manage.py test
Django runs these tests.然后通过运行
python manage.py test
Django 运行这些测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.