接口测试用例后台开发
一、创建接口测试用例及测试步骤后台管理
python manage.py startapp testapi
二、在settings.py中注册该app
项目/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'product.apps.ProductConfig',
'testapi.apps.TestapiConfig',
]
三、创建数据模型
testapi/models.py
from django.db import models
# Create your models here.
class Apicase(models.Model):
Product = models.ForeignKey('product.Product', on_delete=models.CASCADE, null=True)
api_case_name = models.CharField('测试用例名称', max_length=60)
api_case_desc = models.CharField('测试用例描述', max_length=100)
api_case_result = models.BooleanField('测试结果')
api_case_create_time = models.DateTimeField('创建时间', auto_now=True)
api_case_run_time = models.DateTimeField('用例执行时间', null=True, blank=True)
class Meta:
verbose_name = 'API测试用例'
verbose_name_plural = 'API测试用例'
db_table = 'api_case'
def __str__(self):
return self.api_case_name
class Apistep(models.Model):
Apicase = models.ForeignKey(Apicase, on_delete=True)
api_step = models.CharField('操作步骤', max_length=10)
api_name = models.CharField('接口名称', max_length=60)
api_desc = models.CharField('接口描述', max_length=100)
api_url = models.CharField("URL地址", max_length=500)
api_header = models.CharField("接口header", max_length=500)
api_param = models.CharField("接口参数", max_length=500)
Request_Method = (('get', 'get'), ('post', 'post'), ('put', 'put'), ('delete', 'delete'),
('patch', 'patch'))
api_method = models.CharField(verbose_name='请求方法', choices=Request_Method, default='post',
max_length=10, null=True)
api_expect = models.CharField('预期结果', max_length=50)
api_result = models.CharField('实际结果', max_length=500) # 实际结果保存
api_create_time = models.DateTimeField('创建时间', auto_now=True)
api_run_time = models.DateTimeField('步骤执行时间') # 最近一次执行该步骤的时间
api_run_status = models.IntegerField('运行结果', max_length=1) # 0 未运行; 1 成功; 2 失败;
class Meta:
db_table = 'api_test'
verbose_name = '接口'
verbose_name_plural = '接口'
def __str__(self):
return self.api_name
四、admin.py中注册使用建立的数据模型,并规定及显示方式
from django.contrib import admin
# Register your models here.
from testapi.models import Apicase, Apistep
class ApistepAdmin(admin.TabularInline):
list_display = [
'api_step', 'api_name', 'api_desc', 'api_url', 'api_header', 'api_param', 'api_method'
]
model = Apistep
extra = 1
@admin.register(Apicase)
class ApicaseAdmin(admin.ModelAdmin):
list_display = ['api_case_name', 'api_case_desc', 'api_case_create_time']
inlines = [ApistepAdmin]
五、生成迁移文件并迁移到数据库中
python manage.py makemigrations
python manage.py migrate