django rest_framework使用pytest单元测试

2023-03-06 19:19:05 浏览数 (1)

# djang自带测试

django本身自带了测试框架库,是基于unittest的。

执行 python manager.py test 会对路径所有test*.py 进行测试

代码语言:javascript复制
from django.test import TestCase
from event_track.models.app import Appclass 

AppTestCase(TestCase):    
    def setUp(self):        
        App.objects.create(name="app1", package_name="package1")        
        App.objects.create(name="app2", package_name="package2")    
    def test_app(self):        
        app1 = App.objects.get(name="app1")        
        self.assertEqual(app1.package_name, "package1")        
        app1 = App.objects.get(name="app2")        
        self.assertEqual(app1.package_name, "package3")

# rest framework

下面是一个简单的测试案例。使用pytest对rest framework进行测试

1. 添加一个配置文件 具体看pytest-django官网 (opens new window)

代码语言:javascript复制
[pytest]
DJANGO_SETTINGS_MODULE=event_track_root.settings
python_files = tests.py test_*.py *_tests.py

2. 创建一个model app.py

代码语言:javascript复制
from django.db import models
class App(models.Model):    
    name = models.CharField(max_length=24)    
    package_name = models.CharField(max_length=50, unique=True)

对app的model类进行增删改查的测试 model测试必须添加@pytest.mark.django_db才可以启用数据库。 使用APITestCase对接口进行测试

3. 编写测试用例 test_app.py

代码语言:javascript复制
@pytest.mark.django_db
@pytest.fixture(scope="module")
def init_app_data():
    App.objects.create(name="app1", package_name="package1")
    App.objects.create(name="app2", package_name="package2")
    App.objects.create(name="app3", package_name="package3")
    App.objects.create(name="app4", package_name="package4")

class AppTests(APITestCase):

    def test_create_app(self):
        url = reverse('event_track:App-list')
        data_list = [{"name": "app1", "package_name": "package1"},
                     {"name": "app2", "package_name": "package2"},
                     {"name": "app3", "package_name": "package3"},
                     {"name": "app4", "package_name": "package4"}
                     ]

        for data in data_list:
            response = self.client.post(url, data)
            self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        self.assertEqual(App.objects.count(), 4)
        self.assertEqual(App.objects.get(name="app1").package_name, "package1")

    @pytest.mark.usefixtures('init_app_data')
    def test_delete_app(self):
        app = App.objects.get(package_name="package3")
        url = reverse('event_track:App-detail', [app.id])
        response = self.client.delete(url)

        self.assertEqual(App.objects.count(), 3)
        with pytest.raises(App.DoesNotExist):
            App.objects.get(package_name="package3")
        self.assertEqual(response.status_code, 204)

    @pytest.mark.usefixtures('init_app_data')
    def test_update_app(self):
        app = App.objects.get(name="app4")
        url = reverse('event_track:App-detail', [app.id])

        app.package_name = "package_update"
        response = self.client.put(url, AppSerializer(app).data)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['package_name'], 'package_update')
        self.assertEqual(App.objects.get(name="app4").package_name, 'package_update')

    @pytest.mark.usefixtures('init_app_data')
    def test_list_app(self):
        url = reverse('event_track:App-list')
        response = self.client.get(url, {'limit': 2, 'offset': 2})

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data['results']), 2)

0 人点赞