上一章:
Django实现接口自动化平台(六)httprunner(2.x)基本使用【持续更新中】_做测试的喵酱的博客-CSDN博客
下一章:
Django实现接口自动化平台(八)测试报告reports序列化器及视图【持续更新中】_做测试的喵酱的博客-CSDN博客
接口自动化平台,内置引擎,使用的是httprunner2,所以数据库设计与httprunner2的数据结构相似。
登录相关:
接口自动化相关:
1、一个项目,(项目表tb_projects)对应一个内置函数( tb_debugtalks 函数表 )
2、一个项目,对应一个测试套
3、一个项目,对应多个接口。
4、一个接口,对应多个用例
5、一个接口,对应一套环境
tb_reports:
tb_projects:
tb_interfaces:
tb_testcases:
tb_debugtalks
tb_envs:
tb_testsuites:
tb_configures:
注意:
用户表已经在二、三章实现了。
Django实现接口自动化平台(二)认证&授权&登录【持续更新中】_做测试的喵酱的博客-CSDN博客
Django实现接口自动化平台(三)实现注册功能【持续更新中】_做测试的喵酱的博客-CSDN博客
python manage.py startapp projects
python manage.py startapp interfaces
python manage.py startapp testcases
python manage.py startapp testsuits
python manage.py startapp configures
python manage.py startapp envs
python manage.py startapp reports
python manage.py startapp debugtalks
在setting.py 下配置INSTALLED_APPS
'projects',
'interfaces',
'users',
'testcases',
'configures',
'reports',
'debugtalks',
'testsuites',
'envs',
我们整个项目,会涉及到大量的应用。如果应用文件夹都在根目录下,会显得比较乱。
所以我们可以在根目录下,创建一个名为apps的package,将所有应用移动到apps下。
1、在项目根目录下,创建 package,apps
2、将所有的应用,用鼠标拖动到apps下,
注意,在拖动应用的时候,弹窗中,不要进行任何勾选。
最终效果:
3、设置应用apps路径
在setting.py下,配置apps的绝对路径
# apps绝对路径
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
注意:
a、BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR是项目的根路径
b、apps的绝对路径:os.path.join(BASE_DIR, 'apps')
c、配置文件,在寻找应用文件时,是在sys.path中寻找的。
sys.path是一个list
为了项目配置文件,能找到我们apps的路径,需要将路径插入到sys.path中。
sys.path.insert(0,)插入到第一位,为了寻找快一点。
4、右键,设置apps文件。Mark Directory as ------Sources Root
将apps文件,设置为资源文件。(非必需操作)
这样以后在pycharm中,在引用apps中的应用时,会自动联想出来,方便我们写代码。
在utils文件夹下,创建基础模型类。utils/base_models.py
# -*- coding:utf-8 -*-
# @Author: 喵酱
# @time: 2023 - 05 -25
# @File: base_model.py
# desc:
from django.db import models
class BaseModel(models.Model):
# id = models.AutoField(primary_key=True, verbose_name='id主键', help_text='id主键')
create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', help_text='创建时间')
update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间', help_text='更新时间')
class Meta:
# 在内部类Meta中,一旦指定abstract = True,那么当前模型类为抽象模型类,在迁移时不会创建表,仅仅是为了供其他类继承
abstract = True
注意:
在内部类Meta中,一旦指定abstract = True,那么当前模型类为抽象模型类,在迁移时不会创建表,仅仅是为了供其他类继承。
apps/reports/models.py
from django.db import models
from utils.base_models import BaseModel
class Reports(BaseModel):
id = models.AutoField(verbose_name='id主键', primary_key=True, help_text='id主键')
name = models.CharField('报告名称', max_length=200, unique=True, help_text='报告名称')
result = models.BooleanField('执行结果', default=1, help_text='执行结果') # 1为成功, 0为失败
count = models.IntegerField('用例总数', help_text='总用例数')
success = models.IntegerField('成功总数', help_text='成功总数')
html = models.TextField('报告HTML源码', help_text='报告HTML源码', null=True, blank=True, default='')
summary = models.TextField('报告详情', help_text='报告详情', null=True, blank=True, default='')
class Meta:
db_table = 'tb_reports'
verbose_name = '测试报告'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
apps/envs/models.py
from django.db import models
from utils.base_models import BaseModel
class Envs(BaseModel):
id = models.AutoField(verbose_name='id主键', primary_key=True, help_text='id主键')
name = models.CharField(verbose_name='环境名称', max_length=200, unique=True, help_text='环境名称')
base_url = models.URLField(verbose_name='请求base url', max_length=200, help_text='请求base url')
desc = models.CharField(verbose_name='简要描述', max_length=200, help_text='简要描述')
class Meta:
db_table = 'tb_envs'
verbose_name = '环境信息'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
apps/projects/models.py
from django.db import models
from utils.base_models import BaseModel
class Projects(BaseModel):
id = models.AutoField(verbose_name='id主键', primary_key=True, help_text='id主键')
name = models.CharField('项目名称', max_length=200, unique=True, help_text='项目名称')
leader = models.CharField('负责人', max_length=50, help_text='项目负责人')
tester = models.CharField('测试人员', max_length=50, help_text='项目测试人员')
programmer = models.CharField('开发人员', max_length=50, help_text='开发人员')
publish_app = models.CharField('发布应用', max_length=100, help_text='发布应用')
desc = models.CharField('简要描述', max_length=200, null=True, blank=True, default='', help_text='简要描述')
class Meta:
db_table = 'tb_projects'
verbose_name = '项目信息'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
apps/interfaces/models.py
from django.db import models
from utils.base_models import BaseModel
class Interfaces(BaseModel):
id = models.AutoField(verbose_name='id主键', primary_key=True, help_text='id主键')
name = models.CharField('接口名称', max_length=200, unique=True, help_text='接口名称')
project = models.ForeignKey('projects.Projects', on_delete=models.CASCADE,
related_name='interfaces', help_text='所属项目')
tester = models.CharField('测试人员', max_length=50, help_text='测试人员')
desc = models.CharField('简要描述', max_length=200, null=True, blank=True, help_text='简要描述')
class Meta:
db_table = 'tb_interfaces'
verbose_name = '接口信息'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
apps/debugtalks/models.py
from django.db import models
from utils.base_models import BaseModel
class DebugTalks(BaseModel):
id = models.AutoField(verbose_name='id主键', primary_key=True, help_text='id主键')
name = models.CharField('debugtalk文件名称', max_length=200, default='debugtalk.py', help_text='debugtalk文件名称')
debugtalk = models.TextField(null=True, default='#debugtalk.py', help_text='debugtalk.py文件')
project = models.OneToOneField('projects.Projects', on_delete=models.CASCADE,
related_name='debugtalks', help_text='所属项目')
class Meta:
db_table = 'tb_debugtalks'
verbose_name = 'debugtalk.py文件'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
apps/testsuites/models.py
from django.db import models
from utils.base_models import BaseModel
class Testsuits(BaseModel):
id = models.AutoField(verbose_name='id主键', primary_key=True, help_text='id主键')
name = models.CharField('套件名称', max_length=200, unique=True, help_text='套件名称')
project = models.ForeignKey('projects.Projects', on_delete=models.CASCADE,
related_name='testsuits', help_text='所属项目')
include = models.TextField('包含的接口', null=False, help_text='包含的接口')
class Meta:
db_table = 'tb_testsuits'
verbose_name = '套件信息'
verbose_name_plural = verbose_name
ordering = ('id', )
def __str__(self):
return self.name
apps/testcases/models.py
from django.db import models
from utils.base_models import BaseModel
class Testcases(BaseModel):
id = models.AutoField(verbose_name='id主键', primary_key=True, help_text='id主键')
name = models.CharField('用例名称', max_length=50, unique=True, help_text='用例名称')
interface = models.ForeignKey('interfaces.Interfaces', on_delete=models.CASCADE,
help_text='所属接口')
include = models.TextField('前置', null=True, help_text='用例执行前置顺序')
author = models.CharField('编写人员', max_length=50, help_text='编写人员')
request = models.TextField('请求信息', help_text='请求信息')
class Meta:
db_table = 'tb_testcases'
verbose_name = '用例信息'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
apps/configures/models.py
from django.db import models
from utils.base_models import BaseModel
class Configures(BaseModel):
id = models.AutoField(verbose_name='id主键', primary_key=True, help_text='id主键')
name = models.CharField('配置名称', max_length=50, help_text='配置名称')
interface = models.ForeignKey('interfaces.Interfaces',
on_delete=models.CASCADE,
related_name='configures',
help_text='所属接口')
author = models.CharField('编写人员', max_length=50, help_text='编写人员')
request = models.TextField('请求信息', help_text='请求信息')
class Meta:
db_table = 'tb_configures'
verbose_name = '配置信息'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name