Java教程

Python搭建接口自动化测试框架-Java-SpringBoot

本文主要是介绍Python搭建接口自动化测试框架-Java-SpringBoot,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

项目介绍

AutoApiTest

基于python的接口自动化测试框架
Test部分基于yingoja开源的DemoApi优化修改而来
API部分将继续完善,提供基于C#,Go,Java,Python版本的Api服务程序,目的是为了学习接口测试的同学不需要去搭建其他语言的运行环境,顺便我也复习一下这几门语言的基础语法。

项目仓库

https://github.com/dwBurning/AutoApiTest.git

API部分-Java-SpringBoot

1.下载项目=>戳我
2.pom.xml

org.springframework.boot

                  spring-boot-starter-web

具体的步骤不写了,可以参考网上其他的资料

3.核心代码

package auto.test.api.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import auto.test.api.models.OutPut;
import auto.test.api.models.Person;

@RestController
@RequestMapping(value = "api/person")
public class PersonController {

	static List<Person> persons;

	public PersonController() {
		persons = new ArrayList<Person>();
	}

	@RequestMapping(method = RequestMethod.GET)
	public OutPut<List<Person>> get() {
		OutPut<List<Person>> outPut = new OutPut<List<Person>>();
		outPut.setCode(0);
		outPut.setData(persons);
		outPut.setMessage("获取成功");
		return outPut;
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public OutPut<Person> Get(@PathVariable("id") int id) {
		Person person = persons.stream().filter(x -> x.getId() == id).findFirst().orElse(null);
		if (person != null) {
			OutPut<Person> outPut = new OutPut<Person>();
			outPut.setCode(0);
			outPut.setData(person);
			outPut.setMessage("获取成功");
			return outPut;
		} else {
			OutPut<Person> outPut = new OutPut<Person>();
			outPut.setCode(-1);
			outPut.setData(person);
			outPut.setMessage("人事资料不存在");
			return outPut;
		}
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public OutPut<String> delete(@PathVariable("id") int id) {
		Person person = persons.stream().filter(x -> x.getId() == id).findFirst().orElse(null);
		if (persons.remove(person)) {
			OutPut<String> outPut = new OutPut<String>();
			outPut.setCode(0);
			outPut.setMessage("删除成功");
			return outPut;
		} else {
			OutPut<String> outPut = new OutPut<String>();
			outPut.setCode(-1);
			outPut.setMessage("人事资料不存在");
			return outPut;
		}
	}

	@RequestMapping(method = RequestMethod.PATCH)
	public OutPut<String> Patch(@RequestBody Person person) {
		Person p = persons.stream().filter(x -> x.getId() == person.getId()).findFirst().orElse(null);
		if (p != null) {
			p.setName(person.getName());
			p.setAge(person.getAge());
			OutPut<String> outPut = new OutPut<String>();
			outPut.setCode(0);
			outPut.setMessage("修改成功");
			return outPut;
		} else {
			OutPut<String> outPut = new OutPut<String>();
			outPut.setCode(-1);
			outPut.setMessage("人事资料不存在");
			return outPut;
		}
	}

	@RequestMapping(method = RequestMethod.POST)
	public OutPut<String> post(@RequestBody Person person) {
		Person p = persons.stream().filter(x -> x.getId() == person.getId()).findFirst().orElse(null);
		if (p != null) {
			OutPut<String> outPut = new OutPut<String>();
			outPut.setCode(-1);
			outPut.setMessage("人事资料已存在");
			return outPut;
		} else {
			persons.add(person);
			OutPut<String> outPut = new OutPut<String>();
			outPut.setCode(0);
			outPut.setMessage("添加成功");
			return outPut;

		}
	}
}

运行:

这篇关于Python搭建接口自动化测试框架-Java-SpringBoot的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!