简体   繁体   English

Django 覆盖范围不包括 APITestCase 测试

[英]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:据我了解,这里可能有几个问题来源:

(1) The directory structure of our application is weird (1)我们应用的目录结构很奇怪

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)

(2) Coverage doesn't pick up tests that are accessed through API calls (2) Coverage 不选取通过 API 调用访问的测试

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测试显然工作得很好。

So to conclude, when I try to view a report for my coverage runs, it fails miserably and never counts the views, models and serializers that actually run for sure, I know it for a fact!!!所以总而言之,当我尝试查看我的覆盖率运行报告时,它惨遭失败,并且从不计算实际运行的视图、模型和序列化程序,我知道这是事实!!!

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.

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