接口测试方法:Spring boot Test、python、postman

一般的rest接口

在pom.xml中加入

<dependency>

                            <groupId>org.springframework.boot</groupId>

                            <artifactId>spring-boot-starter-test</artifactId>

                            <scope>test</scope>

                   </dependency>

新建测试类

 

 

@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

 

 

猜你喜欢

转载自blog.csdn.net/luansj/article/details/108011366
今日推荐