用例应用代码设计
case\models
设计
用例表我们也设计的比较简单,模块(外键)、用例名称、前置条件、用例步骤、预期结果、实际结果、执行状态
from django.db import models
# Create your models here.
class Testcases(models.Model):
# 设置4种状态
STATUS_CHOICES = (
('尚未执行', '尚未执行'),
('通过', '通过'),
('失败', '失败'),
('暂缓测试', '暂缓测试'),
)
module = models.ForeignKey('module.Modules', on_delete=models.PROTECT)
name = models.CharField('用例名称', max_length=50, unique=True)
preconditions = models.TextField('前置条件', max_length=5000, default="")
step = models.TextField('用例步骤', max_length=5000, default="")
expected_results = models.TextField('预期结果', max_length=5000, default="")
actual_results = models.TextField('实际结果', max_length=5000, default="")
status = models.CharField(max_length=50, choices=STATUS_CHOICES, default="尚未执行")
create_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
class Meta:
db_table = 'testcase'
verbose_name = '用例'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
生成表
生成迁移脚本
(env) D:\code\caseplatform>python manage.py makemigrations
生成数据表
(env) D:\code\caseplatform>python manage.py migrate
case\serializers.py
设计
from rest_framework import serializers
from .models import Testcases
class TestcaseSerializer(serializers.ModelSerializer):
class Meta:
model = Testcases
exclude = ('update_time',)
extra_kwargs = {
"create_time": {
"read_only": True,
"format": "%Y年%m月%d日 %H:%M:%S"
}
}
case\views
设计
from case.models import Testcases
from case.serializers import TestcaseSerializer
from rest_framework import viewsets, permissions
# Create your views here.
class TestCaseViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated]
search_fields = ['=name']
ordering_fields = ['name']
queryset = Testcases.objects.all()
serializer_class = TestcaseSerializer
case\urls
设计
from case import views
from rest_framework import routers
# 创建SimpleRouter路由对象
router = routers.SimpleRouter()
# 注册projects
router.register('cases', views.TestCaseViewSet)
urlpatterns = router.urls