Python搭建接口自动化测试框架-Golang-Gin

2021-09-05 09:12

阅读:798

标签:mode   roo   nbsp   war   run   生成   import   windows   init   项目介绍 AutoApiTest 基于python的接口自动化测试框架 Test部分基于yingoja开源的DemoApi优化修改而来 API部分将继续完善,提供基于C#,Go,Java,Python版本的Api服务程序,目的是为了学习接口测试的同学不需要去搭建其他语言的运行环境,顺便我也复习一下这几门语言的基础语法。 项目仓库 https://github.com/dwBurning/AutoApiTest.git API部分-Golang-Gin cobra 简介 Cobra是一个用于创建强大的现代CLI应用程序的库,也是一个用于生成应用程序和命令文件的程序。 安装 go get -v github.com/spf13/cobra/cobra 使用cobra生成应用程序 假设现在我们要开发一个基于CLIs的命令程序,名字为AutoApiTest。首先打开CMD,切换到GOPATH的src目录下,执行如下指令: D:\GOPATH\src>..\bin\cobra.exe init AutoApiTest --pkg-name api Your Cobra application is ready at D:\GOPATH\src/AutoApiTest 在src目录下会生成一个AutoApiTest的文件夹,如下: D:\GOPATH\SRC\AutoApiTest | LICENSE | main.go | ---cmd root.go D:\GOPATH\src\go-blog>go mod init api go: creating new go.mod: module api 运行 Microsoft Windows [版本 6.3.9600] (c) 2013 Microsoft Corporation。保留所有权利。 D:\GOPATH\src\AutoApiTest>go run main.go Api服务正在启动 [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. using env: export GIN_MODE=release using code: gin.SetMode(gin.ReleaseMode) [GIN-debug] GET /api/person --> api/controller.Get (1 handlers) [GIN-debug] GET /api/person/:id --> api/controller.GetById (1 handlers) [GIN-debug] POST /api/person --> api/controller.Add (1 handlers) [GIN-debug] PATCH /api/person/:id --> api/controller.Patch (1 handlers) [GIN-debug] DELETE /api/person/:id --> api/controller.Delete (1 handlers)http://127.0.0.1:5000 Enter Control + C Shutdown Server 核心代码 package controller import (     "api/model"     "api/response"     "net/http"     "strconv"     "github.com/gin-gonic/gin" ) var persons = make(map[int]model.Person) func Get(ctx *gin.Context) {     var ps []model.Person     for _, v := range persons {         ps = append(ps, v)     }     ctx.JSON(http.StatusOK, response.New(0, "获取成功", ps)) } func GetById(ctx *gin.Context) {     id := ctx.Param("id")     key, _ := strconv.Atoi(id)     person, ok := persons[key]     if ok {         ctx.JSON(http.StatusOK, response.New(0, "获取成功", person))     } else {         ctx.JSON(http.StatusOK, response.New(-1, "人事资料不存在", nil))     } } func Delete(ctx *gin.Context) {     id := ctx.Param("id")     key, _ := strconv.Atoi(id)     person, ok := persons[key]     if ok {         delete(persons, key)         ctx.JSON(http.StatusOK, response.New(0, "删除成功", person))     } else {         ctx.JSON(http.StatusOK, response.New(-1, "人事资料不存在", nil))     } } func Patch(ctx *gin.Context) {     id := ctx.Param("id")     key, _ := strconv.Atoi(id)     person, ok := persons[key]     if ok {         var p model.Person         ctx.ShouldBindJSON(&person)         persons[key] = p         ctx.JSON(http.StatusOK, response.New(0, "修改成功", nil))     } else {         ctx.JSON(http.StatusOK, response.New(-1, "人事资料不存在", nil))     } } func Add(ctx *gin.Context) {     var p model.Person     ctx.ShouldBindJSON(&p)     _, ok := persons[p.Id]     if ok {         ctx.JSON(http.StatusOK, response.New(-1, "人事资料已存在", nil))     } else {         persons[p.Id] = p         ctx.JSON(http.StatusOK, response.New(0, "添加成功", nil))     } } Python搭建接口自动化测试框架-Golang-Gin标签:mode   roo   nbsp   war   run   生成   import   windows   init   原文地址:https://www.cnblogs.com/dwBurning/p/autoapitest-go.html


评论


亲,登录后才可以留言!