自动化测试平台
API
Product
from django.db import models class Product(models.Model): product_name = models.CharField('产品名称', max_length=64) product_desc = models.CharField('产品描述', max_length=200) producer = models.CharField('产品负责人', max_length=200, null=True) create_time = models.DateTimeField('创建时间', auto_now=True) class Meta: verbose_name = '产品管理' verbose_name_plural = '产品管理' def __str__(self): return self.product_name
api
from django.db import models
from product.models import Product
__all__ = ['ApiTest', 'ApiMulti', 'ApiOne']
class ApiTest(models.Model):
product = models.ForeignKey('Product', on_delete=models.CASCADE, null=True)
api_name = models.CharField('接口名称', max_length=64)
api_desc = models.CharField('接口描述', max_length=64, null=True, blank=True)
api_executor = models.CharField('执行人', max_length=16)
api_result = models.BooleanField('测试结果')
create_time = models.DateTimeField('创建时间', auto_now=True)
class Meta:
verbose_name = '流程场景接口'
db_table = verbose_name
verbose_name_plural = verbose_name
def __str__(self):
return self.api_name
class ApiMulti(models.Model):
api_test = models.ForeignKey('ApiTest', on_delete=models.CASCADE, )
api_step = models.CharField('测试步聚', max_length=100, null=True)
api_name = models.CharField('接口名称', max_length=100)
api_url = models.CharField('url地址', max_length=200)
requests_method = (('get', 'get'), ('post', 'post'), ('put', 'put'), ('delete', 'delete'), ('patch', 'patch'))
api_method = models.CharField(verbose_name='请求方法', choices=requests_method, default='get', max_length=200,
null=True)
api_header = models.CharField('请求头', max_length=400)
api_body = models.CharField('请求体', max_length=800)
api_result = models.CharField('预期结果', max_length=200)
api_response = models.CharField('响应数据', max_length=5000, null=True)
api_status = models.BooleanField('是否通过') # 测试结果
create_time = models.DateTimeField('创建时间', auto_now=True)
def __str__(self):
return self.api_step
class ApiOne(models.Model):
Product = models.ForeignKey('Product', on_delete=models.CASCADE, null=True) # 关联产品id
api_name = models.CharField('接口名称', max_length=100)
api_url = models.CharField('url地址', max_length=200)
requests_method = (('get', 'get'), ('post', 'post'), ('put', 'put'), ('delete', 'delete'), ('patch', 'patch'))
api_method = models.CharField(verbose_name='请求方法', choices=requests_method, default='get', max_length=200,
null=True)
api_header = models.CharField('请求头', max_length=400)
api_body = models.CharField('请求体', max_length=800)
api_result = models.CharField('预期结果', max_length=200)
api_response = models.CharField('响应数据', max_length=5000, null=True)
api_status = models.BooleanField('是否通过') # 测试结果
create_time = models.DateTimeField('创建时间', auto_now=True)
api_executor = models.CharField('执行人', max_length=16)
class Meta:
verbose_name = '单一场景接口'
db_table = verbose_name
verbose_name_plural = verbose_name
def __str__(self):
return self.api_name
views
from django.shortcuts import render from django.http import HttpResponse,HttpResponseRedirect from django.contrib.auth.decorators import login_required from django.contrib import auth from django.contrib.auth import authenticate, login from apitest.models import Apitest,Apistep,Apis from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger import pymysql from .tasks import hello_world from .tasks import apisauto_testcase from .tasks import apitest_testcase from .webtasks import webauto_testcase from .webtasks import webauto_testcase2 from .apptasks import appauto_testcase from .apptasks import appauto_testcase2 from djcelery.models import PeriodicTask,CrontabSchedule,IntervalSchedule # Create your views here. def task_apis(request): hello_world.delay() apisauto_testcase.delay() return HttpResponse("已运行") def task_apitest(request): hello_world.delay() apitest_testcase(request) return HttpResponse("已运行") def task_webtest(request): webauto_testcase(request) return HttpResponse("已运行") def task_webtest2(request): webauto_testcase2(request) return HttpResponse("已运行") def task_apptest(request): appauto_testcase(request) return HttpResponse("已运行") def task_apptest2(request): appauto_testcase2(request) return HttpResponse("已运行") def welcome(request): return render(request,"welcome.html") def home(request): return render(request,"home.html") def left(request): return render(request,"left.html") def login(request): if request.POST: username = password = '' username = request.POST.get('username') password = request.POST.get('password') user = auth.authenticate(username=username, password=password) if user is not None and user.is_active: auth.login(request, user) request.session['user'] = username response = HttpResponseRedirect('/home/') return response # return redirect('/') else: return render(request,'login.html', {'error': 'username or password error'}) return render(request,'login.html') def logout(request): auth.logout(request) return render(request,'login.html') # 流程接口管理 @login_required def apitest_manage(request): apitest_list = Apitest.objects.all() #获取所有接口测试用例 apitest_count = Apitest.objects.all().count() #统计产品数 username = request.session.get('user', '') # 读取浏览器登录session paginator = Paginator(apitest_list, 8) #生成paginator对象,设置每页显示8条记录 page = request.GET.get('page',1) #获取当前的页码数,默认为第1页 currentPage=int(page) #把获取的当前页码数转换成整数类型 try: apitest_list = paginator.page(page)#获取当前页码数的记录列表 except PageNotAnInteger: apitest_list = paginator.page(1)#如果输入的页数不是整数则显示第1页的内容 except EmptyPage: apitest_list = paginator.page(paginator.num_pages)#如果输入的页数不在系统的页数中则显示最后一页的内容 return render(request, "apitest_manage.html", {"user": username,"apitests": apitest_list,"apitestcounts": apitest_count}) #把值赋给apitestcounts这个变量 # 接口步聚管理 @login_required def apistep_manage(request): username = request.session.get('user', '') apitestid = request.GET.get('apitest.id',None) apitest = Apitest.objects.get(id=apitestid) #获取所有接口测试用例 apistep_list = Apistep.objects.all() return render(request, "apistep_manage.html", {"user": username,"apitest": apitest,"apisteps": apistep_list}) # 单一接口管理 @login_required def apis_manage(request): username = request.session.get('user', '') apis_list = Apis.objects.all() apis_count = Apis.objects.all().count() #统计产品数 paginator = Paginator(apis_list, 8) #生成paginator对象,设置每页显示8条记录 page = request.GET.get('page',1) #获取当前的页码数,默认为第1页 currentPage=int(page) #把获取的当前页码数转换成整数类型 try: apis_list = paginator.page(page)#获取当前页码数的记录列表 except PageNotAnInteger: apis_list = paginator.page(1)#如果输入的页数不是整数则显示第1页的内容 except EmptyPage: apis_list = paginator.page(paginator.num_pages)#如果输入的页数不在系统的页数中则显示最后一页的内容 return render(request, "apis_manage.html", {"user": username,"apiss": apis_list,"apiscounts": apis_count}) #把值赋给apiscounts这个变量 # 搜索功能 @login_required def apisearch(request): username = request.session.get('user', '') # 读取浏览器登录session search_apitestname = request.GET.get("apitestname", "") apitest_list = Apitest.objects.filter(apitestname__icontains=search_apitestname) return render(request,'apitest_manage.html', {"user": username,"apitests":apitest_list}) # 搜索功能 @login_required def apistepsearch(request): username = request.session.get('user', '') # 读取浏览器登录session search_apiname = request.GET.get("apiname", "") apistep_list = Apistep.objects.filter(apiname__icontains=search_apiname) return render(request,'apistep_manage.html', {"user": username,"apisteps":apistep_list}) # 搜索功能 @login_required def apissearch(request): username = request.session.get('user', '') # 读取浏览器登录session search_apiname = request.GET.get("apiname", "") apis_list = Apis.objects.filter(apiname__icontains=search_apiname) return render(request,'apis_manage.html', {"user": username,"apiss":apis_list}) # 测试报告 @login_required def test_report(request): username = request.session.get('user', '') apis_list = Apis.objects.all() apis_count = Apis.objects.all().count() #统计接口数 db = pymysql.connect(user='root', db='autotest', passwd='test123456', host='127.0.0.1') cursor = db.cursor() sql1 = 'SELECT count(id) FROM apitest_apis WHERE apitest_apis.apistatus=1' aa=cursor.execute(sql1) apis_pass_count = [row[0] for row in cursor.fetchmany(aa)][0] sql2 = 'SELECT count(id) FROM apitest_apis WHERE apitest_apis.apistatus=0' bb=cursor.execute(sql2) apis_fail_count = [row[0] for row in cursor.fetchmany(bb)][0] db.close() return render(request, "report.html", {"user": username,"apiss": apis_list,"apiscounts": apis_count,"apis_pass_counts": apis_pass_count,"apis_fail_counts": apis_fail_count}) #把值赋给apiscounts这个变量 # 测试报告 @login_required def apitest_report(request): username = request.session.get('user', '') return render(request, "apitest_report.html") # 任务计划 @login_required def periodic_task(request): username = request.session.get('user', '') task_list = PeriodicTask.objects.all() task_count = PeriodicTask.objects.all().count() #统计数 periodic_list = IntervalSchedule.objects.all() # 周期任务 (如:每隔1小时执行1次) crontab_list = CrontabSchedule.objects.all() # 定时任务 (如:某年月日的某时,每天的某时) paginator = Paginator(task_list, 8) #生成paginator对象,设置每页显示8条记录 page = request.GET.get('page',1) #获取当前的页码数,默认为第1页 currentPage=int(page) #把获取的当前页码数转换成整数类型 try: task_list = paginator.page(page)#获取当前页码数的记录列表 except PageNotAnInteger: task_list = paginator.page(1)#如果输入的页数不是整数则显示第1页的内容 except EmptyPage: task_list = paginator.page(paginator.num_pages)#如果输入的页数不在系统的页数中则显示最后一页的内容 return render(request, "periodic_task.html", {"user": username,"tasks": task_list,"taskcounts": task_count, "periodics": periodic_list,"crontabs": crontab_list }) # 搜索功能 @login_required def tasksearch(request): username = request.session.get('user', '') # 读取浏览器登录session search_name = request.GET.get("task", "") task_list = PeriodicTask.objects.filter(task__icontains=search_name) periodic_list = IntervalSchedule.objects.all() # 周期任务 (如:每隔1小时执行1次) crontab_list = CrontabSchedule.objects.all() # 定时任务 (如:某年月日的某时,每天的某时) return render(request,'periodic_task.html', {"user": username,"tasks":task_list,"periodics": periodic_list,"crontabs": crontab_list })
web
from django.db import models from product.models import Product # Create your models here. class Webcase(models.Model): Product = models.ForeignKey('product.Product',on_delete=models.CASCADE,null=True) # 关联产品id webcasename = models.CharField('用例名称',max_length=200) # 测试用例名称 webtestresult = models.BooleanField('测试结果') # 测试结果 webtester = models.CharField('测试负责人',max_length=16) # 测试负责人 create_time = models.DateTimeField('创建时间',auto_now=True) # 创建时间-自动获取当前时间 class Meta: verbose_name = 'web测试用例' verbose_name_plural = 'web测试用例' def __str__(self): return self.webcasename class Webcasestep(models.Model): Webcase = models.ForeignKey(Webcase,on_delete=models.CASCADE) # 关联接口id webteststep = models.CharField('测试步聚',max_length=200) # 测试步聚 webtestobjname = models.CharField('测试对象名称描述',max_length=200) # 测试对象名称描述 webfindmethod = models.CharField('定位方式',max_length=200) # 定位方式 webevelement = models.CharField('控件元素',max_length=800) # 控件元素 weboptmethod = models.CharField('操作方法',max_length=200) # 操作方法 webtestdata = models.CharField('测试数据',max_length=200,null=True) # 测试数据 webassertdata = models.CharField('验证数据',max_length=200) # 验证数据 webtestresult = models.BooleanField('测试结果') # 测试结果 create_time = models.DateTimeField('创建时间',auto_now=True) # 创建时间-自动获取当前时间 def __str__(self): return self.webteststep
web-view
from django.shortcuts import render from django.http import HttpResponse,HttpResponseRedirect from django.contrib.auth.decorators import login_required from django.contrib import auth from django.contrib.auth import authenticate, login from webtest.models import Webcase,Webcasestep from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger # Create your views here. # web用例管理 @login_required def webcase_manage(request): webcase_list = Webcase.objects.all() webcase_count = Webcase.objects.all().count() #统计产品数 username = request.session.get('user', '') # 读取浏览器登录session paginator = Paginator(webcase_list, 8) #生成paginator对象,设置每页显示8条记录 page = request.GET.get('page',1) #获取当前的页码数,默认为第1页 currentPage=int(page) #把获取的当前页码数转换成整数类型 try: webcase_list = paginator.page(page)#获取当前页码数的记录列表 except PageNotAnInteger: webcase_list = paginator.page(1)#如果输入的页数不是整数则显示第1页的内容 except EmptyPage: webcase_list = paginator.page(paginator.num_pages)#如果输入的页数不在系统的页数中则显示最后一页 return render(request, "webcase_manage.html", {"user": username,"webcases":webcase_list,"webcasecounts": webcase_count}) # web用例测试步聚 @login_required def webcasestep_manage(request): username = request.session.get('user', '') webcaseid = request.GET.get('webcase.id',None) webcase = Webcase.objects.get(id=webcaseid) webcasestep_list = Webcasestep.objects.all() return render(request, "webcasestep_manage.html", {"user": username,"webcase": webcase,"webcasesteps": webcasestep_list}) # 搜索功能 @login_required def websearch(request): username = request.session.get('user', '') # 读取浏览器登录session search_webcasename = request.GET.get("webcasename", "") webcase_list = Webcase.objects.filter(webcasename__icontains=search_webcasename) return render(request,'webcase_manage.html', {"user": username,"webcases":webcase_list}) # 搜索功能 @login_required def webstepsearch(request): username = request.session.get('user', '') # 读取浏览器登录session search_webcasename = request.GET.get("webcasename", "") webcasestep_list = Webcasestep.objects.filter(webcasename__icontains=search_webcasename) return render(request,'webcasestep_manage.html', {"user": username,"webcasesteps":webcasestep_list}) # web测试报告 @login_required def webtest_report(request): username = request.session.get('user', '') return render(request, "webtest_report.html")
幻想毫无价值,计划渺如尘埃,目标不可能达到。这一切的一切毫无意义——除非我们付诸行动。