1.测试用例的app名字:Testcase
2.Testcase文件夹下各个文件的代码:
2.1. __init__.py:
default_app_config = "TestCase.apps.TestcaseConfig"
2.2. adminx.py
import xadmin from xadmin import views #导入xadmin中的views,用于和 BaseSettings类绑定 from .models import TestCase #导入测试用例模型 from .models import TestProject #导入测试项目模型 from .models import MoveView #导入动态视图 class TestProjectAdmin(object): diduan = ['test_project', 'testCase_nums', 'add_time' ] list_display = diduan #定义显示的字段 search_fields = diduan #定义搜索的字段 list_filter = ['test_project', 'testCase_nums'] #定义筛选的字段 xadmin.site.register(TestProject, TestProjectAdmin) #在xadmin中注册TestCase class TestCaseAdmin(object): diduan = ['test_project', 'test_module', 'test_function','case_priority','case_title', 'case_precondition', 'case_step', 'case_expected_result', 'case_test_result', 'case_comments', 'add_time' ] list_display = diduan #定义显示的字段 search_fields = diduan #定义搜索的字段 list_filter = ['test_project', 'test_module', 'case_priority', 'case_test_result'] #定义筛选的字段 xadmin.site.register(TestCase, TestCaseAdmin) #在xadmin中注册TestCase class MoveViewAdmin(object): diduan = ['test_project','Num'] list_display = diduan #定义显示的字段 search_fields = diduan #定义搜索的字段 list_filter = diduan #定义筛选的字段 xadmin.site.register(MoveView, MoveViewAdmin) #在xadmin中注册MoveView # class BaseSettings(object): #全站的配置类, 配置主题 # enable_themes = True #主题功能,enable_themes=True 表示要使用它的主题功能,xadmin默认是取消掉的 # use_bootswatch = True #xadmin默认是取消掉的 # # xadmin.site.register(views.BaseAdminView, BaseSettings) #注册BaseSettings # # # class GlobalSettings(object): ##全站的配置类 # site_title = "接口测试后台管理系统" #页面左上角的标题名称 # site_footer = "接口测试网" #页面底部的文字显示内容 # menu_style = "accordion" # 将一个app下的内容收起来 # # xadmin.site.register(views.CommAdminView, GlobalSettings) #注册GlobalSettings
2.3. apps.py
from django.apps import AppConfig class TestcaseConfig(AppConfig): name = 'TestCase' verbose_name = u"测试用例"
2.4. models.py
from datetime import datetime #导入获取时间包 from django.db import models # Create your models here. # class TestProject(models.Model): # 定义测试项目 test_project = models.CharField(max_length=10, verbose_name=u"测试项目") testCase_nums = models.IntegerField(default=0, verbose_name=u"测试用例总数") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"创建项目时间") # datetime.now记录实例化时间,datetime.now()记录模型创建时间 class Meta: verbose_name = u"测试项目" verbose_name_plural = verbose_name def __str__(self): return self.test_project class TestCase(models.Model): #定义测试用例 test_project = models.ForeignKey(TestProject,verbose_name=u"测试项目",on_delete=models.CASCADE) test_module = models.CharField(max_length=15, verbose_name=u"测试模块") test_function = models.CharField(max_length=30, verbose_name=u"测试功能点") case_priority = models.CharField(max_length=10, choices=(("P0", u"冒烟用例"), ("P1", u"系统的重要功能用例") , ("P2", u"系统的一般功能用例"), ("P3", "极低级别的用例")), default="P1", verbose_name=u"用例优先级") case_title = models.CharField(max_length=20,verbose_name=u"测试用例_名称") case_precondition = models.CharField(max_length=100,verbose_name=u"测试用例_前置条件") case_step = models.TextField(default="",verbose_name=u"测试用例_操作步骤") case_expected_result = models.TextField(default="", verbose_name=u"测试用例_预期结果") case_test_result = models.CharField(max_length=5, choices=(("Pass", u"测试通过"), ("Fail", u"测试失败"), ("Block", u"用例锁定"), ("NoRun", "未测试")), default="NoRun", verbose_name=u"测试用例_测试结果") case_comments = models.TextField(default=u"测试备注", verbose_name=u"测试备注") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"编写用例时间") #datetime.now记录实例化时间,datetime.now()记录模型创建时间 class Meta: verbose_name = u"测试用例" verbose_name_plural = verbose_name class MoveView(models.Model): # 定义动态视图表 test_project = models.CharField(max_length=10, verbose_name=u"测试项目") Num = models.IntegerField(default=0, verbose_name=u"测试用例总数") class Meta: verbose_name = u"动态视图" verbose_name_plural = verbose_name managed = False db_table = "count_casesnum_project"
2.5. urls.py
from django.urls import path urlpatterns = [ #path('testcase/', OrgView.as_view(), name="test_case"), # 配置课程列表页面的访问路径 ] app_name = 'testcast'
3.动态视图数据库表的设计视图语句:
select count(`testcase_testcase`.`case_title`) AS `Num`,`testcase_testproject`.`id` AS `id`,`testcase_testproject`.`test_project` AS `test_project` from (`testcase_testcase` join `testcase_testproject`) where (`testcase_testcase`.`test_project_id` = `testcase_testproject`.`id`) group by `testcase_testproject`.`test_project`