用例应用代码设计

case\models设计

用例表我们也设计的比较简单,模块(外键)、用例名称、前置条件、用例步骤、预期结果、实际结果、执行状态

  1. from django.db import models
  2. # Create your models here.
  3. class Testcases(models.Model):
  4. # 设置4种状态
  5. STATUS_CHOICES = (
  6. ('尚未执行', '尚未执行'),
  7. ('通过', '通过'),
  8. ('失败', '失败'),
  9. ('暂缓测试', '暂缓测试'),
  10. )
  11. module = models.ForeignKey('module.Modules', on_delete=models.PROTECT)
  12. name = models.CharField('用例名称', max_length=50, unique=True)
  13. preconditions = models.TextField('前置条件', max_length=5000, default="")
  14. step = models.TextField('用例步骤', max_length=5000, default="")
  15. expected_results = models.TextField('预期结果', max_length=5000, default="")
  16. actual_results = models.TextField('实际结果', max_length=5000, default="")
  17. status = models.CharField(max_length=50, choices=STATUS_CHOICES, default="尚未执行")
  18. create_time = models.DateTimeField(auto_now_add=True)
  19. update_time = models.DateTimeField(auto_now=True)
  20. class Meta:
  21. db_table = 'testcase'
  22. verbose_name = '用例'
  23. verbose_name_plural = verbose_name
  24. ordering = ('id',)
  25. def __str__(self):
  26. return self.name

生成表

生成迁移脚本

(env) D:\code\caseplatform>python manage.py makemigrations

生成数据表

(env) D:\code\caseplatform>python manage.py migrate

case\serializers.py设计

  1. from rest_framework import serializers
  2. from .models import Testcases
  3. class TestcaseSerializer(serializers.ModelSerializer):
  4. class Meta:
  5. model = Testcases
  6. exclude = ('update_time',)
  7. extra_kwargs = {
  8. "create_time": {
  9. "read_only": True,
  10. "format": "%Y年%m月%d日 %H:%M:%S"
  11. }
  12. }

case\views 设计

  1. from case.models import Testcases
  2. from case.serializers import TestcaseSerializer
  3. from rest_framework import viewsets, permissions
  4. # Create your views here.
  5. class TestCaseViewSet(viewsets.ModelViewSet):
  6. permission_classes = [permissions.IsAuthenticated]
  7. search_fields = ['=name']
  8. ordering_fields = ['name']
  9. queryset = Testcases.objects.all()
  10. serializer_class = TestcaseSerializer

case\urls 设计

  1. from case import views
  2. from rest_framework import routers
  3. # 创建SimpleRouter路由对象
  4. router = routers.SimpleRouter()
  5. # 注册projects
  6. router.register('cases', views.TestCaseViewSet)
  7. urlpatterns = router.urls