接口测试方法:Spring boot Test、python、postman
一般的rest接口
在pom.xml中加入
新建测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {
private MockMvc mvc;
//初始化执行
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new TestIndexController()).build();
}
//验证controller是否正常响应并打印返回结果
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/getConfig").accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
/*//验证controller是否正常响应并判断返回结果是否正确
@Test
public void testHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World")));
}*/
}
有自动注入的类,可以采用如下:
采用python请求
脚本语言方便调试,循环调用 ,有单点登录的,可以在headers中写入Cookie
import requests, json
headers={'Cookie':'JSESSIONID=4FD8D3DC2E7031EDAB28F668C52E99BE'}
def getConfig():
ip = '10.11.67.203:8003'
dcms_url = '/hellowebtest/getConfig'
user_url = 'http://' + ip + dcms_url
print(user_url)
data = json.dumps([{}])
r = requests.post(user_url, data, headers=headers)
print(r.text)
getConfig()
采用postman
本身不支持传入cookie,需要下载postman Interceptor,安装在chrome浏览器,需要打开chrome浏览器,在postman中启用Interceptor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)