接口自动化测试二:补充testng,测试用例中的发送和校验

接口自动化测试二:补充testng,测试用例中的发送和校验

补充loginTest

package com.course.cases;

import com.course.config.TestConfig;
import com.course.model.IntegerfaceName;
import com.course.model.LoginCase;
import com.course.utils.ConfigFile;
import com.course.utils.DatabaseUtil;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.SqlSession;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

public class LoginTest {
    @BeforeTest(groups = "loginTrue",description = "测试准备工作,获取httpclient对象等")
    public void beforeTest(){
        TestConfig.getUserInfoUrl = ConfigFile.getUrl(IntegerfaceName.GETUSERINFO);
        TestConfig.getUserListUrl = ConfigFile.getUrl(IntegerfaceName.GETUSERLIST);
        TestConfig.loginUrl = ConfigFile.getUrl(IntegerfaceName.LOGIN);
        TestConfig.addUserUrl = ConfigFile.getUrl(IntegerfaceName.ADDUSER);
        TestConfig.updateUserInfoUrl = ConfigFile.getUrl(IntegerfaceName.UPDATEUSERINFO);
    }

    @Test(groups = "loginTrue",description = "用户登录成功测试")
    public void loginTrue() throws IOException {
        SqlSession sqlSession = DatabaseUtil.getSqlSession();
        LoginCase loginCase = sqlSession.selectOne("loginCase",1);
        System.out.println(loginCase.toString());
        System.out.println(TestConfig.loginUrl);

        //发送请求,获取数据
        String result = getResult(loginCase);
        Assert.assertEquals(loginCase.getExpected(),result);
        //验证数据
    }
    @Test(groups = "loginFalse",description = "用户登录失败测试")
    public void loginFalse() throws IOException {
        SqlSession sqlSession = DatabaseUtil.getSqlSession();
        LoginCase loginCase = sqlSession.selectOne("loginCase",2);
        System.out.println(loginCase.toString());
        System.out.println(TestConfig.loginUrl);
        //发送请求,获取数据
        String result = getResult(loginCase);
        Assert.assertEquals(loginCase.getExpected(),result);
        //验证数据
    }


    public String getResult(LoginCase loginCase) throws IOException {
        HttpPost post = new HttpPost(TestConfig.loginUrl);
        JSONObject param = new JSONObject();
        param.put("userName",loginCase.getUserName());
        param.put("password",loginCase.getPassword());
        TestConfig.cookieStore = new BasicCookieStore();
        post.setHeader("Content-Type", "application/json");
        StringEntity entity = new StringEntity(param.toString(),"utf-8");
        post.setEntity(entity);
        TestConfig.cookieStore = new BasicCookieStore();
        TestConfig.closeableHttpClient = HttpClients.custom().setDefaultCookieStore(TestConfig.cookieStore).build();
        HttpResponse response = TestConfig.closeableHttpClient.execute(post);
        String result = EntityUtils.toString(response.getEntity(),"utf-8");
        return result;
    }
}

补充adduserTest

package com.course.cases;

import com.course.config.TestConfig;
import com.course.model.AddUserCase;
import com.course.model.User;
import com.course.utils.DatabaseUtil;
import org.apache.commons.codec.StringEncoder;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.SqlSession;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;

import javax.swing.text.html.parser.Entity;
import java.io.IOException;
import java.util.List;

public class AddUserTest {
    @Test(dependsOnGroups = "loginTrue",description = "添加用户接口测试")
    public void addUser() throws IOException {
        SqlSession sqlSession = DatabaseUtil.getSqlSession();
        AddUserCase addUserCase = sqlSession.selectOne("addUserCase",1);
        System.out.println(addUserCase.toString());
        System.out.println(TestConfig.addUserUrl);
        //发送请求,获取结果
        String result = getResult(addUserCase);
        //验证结果
//        User user = sqlSession.selectOne("addUser",addUserCase);
//        System.out.println(user.toString());
        System.out.println(addUserCase.getExpected());
        System.out.println(result);
        Assert.assertEquals(addUserCase.getExpected(),result);
    }

    private String getResult(AddUserCase addUserCase) throws IOException {
        HttpPost post = new HttpPost(TestConfig.addUserUrl);
        JSONObject param = new JSONObject();
        param.put("userName",addUserCase.getUserName());
        param.put("password",addUserCase.getPassword());
        param.put("age",addUserCase.getAge());
        param.put("sex",addUserCase.getSex());
        param.put("permission",addUserCase.getPermission());
        param.put("isDelete",addUserCase.getIsDelete());
        //设置头信息
        post.setHeader("Content-Type", "application/json");
        StringEntity entity = new StringEntity(param.toString(),"utf-8");
        post.setEntity(entity);
        //设置cookies
        TestConfig.closeableHttpClient = HttpClients.custom().setDefaultCookieStore(TestConfig.cookieStore).build();
        HttpResponse response = TestConfig.closeableHttpClient.execute(post);
        String result = EntityUtils.toString(response.getEntity(),"utf-8");

        return result;
    }
}

补充getuserinfoTest

package com.course.cases;

import com.course.config.TestConfig;
import com.course.model.GetUserInfoCase;
import com.course.model.User;
import com.course.utils.DatabaseUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.SqlSession;
import org.json.JSONArray;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONAssert;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GetUserInfoTest {

    @Test(dependsOnGroups = "loginTrue",description = "获取用户ID为1的用户信息")
    public void getUserInfo() throws IOException {
        SqlSession sqlSession = DatabaseUtil.getSqlSession();
        GetUserInfoCase getUserInfoCase = sqlSession.selectOne("getUserInfoCase",1);
        System.out.println(getUserInfoCase.toString());
        System.out.println(TestConfig.getUserInfoUrl);
        //发送请求
        JSONArray resultJson = getJsonResult(getUserInfoCase);

        //验证
        User user = sqlSession.selectOne(getUserInfoCase.getExpected(),getUserInfoCase);
        List userList = Arrays.asList(user);
        //userList.add(user);
        System.out.println(userList.toString());
        JSONArray jsonArray = new JSONArray(userList);

        //Assert.assertEquals(jsonArray.toString(),resultJson.toString());

        JSONAssert.assertEquals(jsonArray,resultJson,true);
    }

    private JSONArray getJsonResult(GetUserInfoCase getUserInfoCase) throws IOException {
        HttpPost post = new HttpPost(TestConfig.getUserInfoUrl);
        JSONObject param = new JSONObject();
        param.put("id",getUserInfoCase.getUserId());
        post.setHeader("Content-Type", "application/json");
        StringEntity entity = new StringEntity(param.toString(),"utf-8");
        post.setEntity(entity);
        TestConfig.closeableHttpClient = HttpClients.custom().setDefaultCookieStore(TestConfig.cookieStore).build();
        HttpResponse response = TestConfig.closeableHttpClient.execute(post);
        String result = EntityUtils.toString(response.getEntity(),"utf-8");
       // List resultlist = Arrays.asList(result);
       // System.out.println(resultlist.toString());
        JSONArray jsonArray = new JSONArray(result);

        return jsonArray;
    }
}

补充getUserListTest

package com.course.cases;

import com.course.config.TestConfig;
import com.course.model.GetUserListCase;
import com.course.model.User;
import com.course.utils.DatabaseUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.SqlSession;
import org.json.JSONArray;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;

public class GetUserListTest {
    @Test(dependsOnGroups = "loginTrue",description = "获取性别为男的用户信息")
    public void getUsrList() throws IOException {
        SqlSession sqlSession = DatabaseUtil.getSqlSession();
        GetUserListCase getUserListCase = sqlSession.selectOne("getUserListCase",1);
        System.out.println(getUserListCase.toString());
        System.out.println(TestConfig.getUserListUrl);

        //发送请求获取结果
        JSONArray resultJson = getJsonResult(getUserListCase);

        //验证
        List<User> userList = sqlSession.selectList(getUserListCase.getExpected(),getUserListCase);
        for(User u : userList){
            System.out.println("获取的user:+"+u.toString());
        }
        JSONArray userListJson = new JSONArray(userList);
        Assert.assertEquals(userListJson.length(),resultJson.length());
        for(int i = 0 ;i<resultJson.length();i++){
            JSONObject expect = (JSONObject) resultJson.get(i);
            JSONObject actual = (JSONObject) userListJson.get(i);
            Assert.assertEquals(expect,actual);
        }

    }

    private JSONArray getJsonResult(GetUserListCase getUserListCase) throws IOException {
        HttpPost post = new HttpPost(TestConfig.getUserListUrl);
        JSONObject param = new JSONObject();
        param.put("userName",getUserListCase.getUserName());
        param.put("sex",getUserListCase.getSex());
        param.put("age",getUserListCase.getAge());
        post.setHeader("Content-Type", "application/json");
        StringEntity entity = new StringEntity(param.toString(),"utf-8");
        post.setEntity(entity);
        TestConfig.closeableHttpClient = HttpClients.custom().setDefaultCookieStore(TestConfig.cookieStore).build();
        HttpResponse response = TestConfig.closeableHttpClient.execute(post);
        String result = EntityUtils.toString(response.getEntity(),"utf-8");
        JSONArray jsonArray = new JSONArray(result);
        return jsonArray;
    }

}

补充updateuserinfoTest

package com.course.cases;

import com.course.config.TestConfig;
import com.course.model.UpdateUserInfoCase;
import com.course.model.User;
import com.course.utils.DatabaseUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.SqlSession;
import org.json.JSONArray;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class UpdateUserInfoTest {
    @Test(dependsOnGroups = "loginTrue",description = "更新用户信息")
    public void updateUserInfo() throws IOException {
        SqlSession sqlSession = DatabaseUtil.getSqlSession();
        UpdateUserInfoCase updateUserInfoCase = sqlSession.selectOne("updateUserInfoCase",1);
        System.out.println(updateUserInfoCase.toString());
        System.out.println(TestConfig.updateUserInfoUrl);
        //发送请求
        int resultJson = getResult(updateUserInfoCase);
        //验证
        User user = sqlSession.selectOne(updateUserInfoCase.getExpected(),updateUserInfoCase);
        Assert.assertNotNull(user);
        Assert.assertNotNull(resultJson);



    }


    @Test(dependsOnGroups = "loginTrue",description = "删除用户信息")
    public void deleteUser() throws IOException {
        SqlSession sqlSession = DatabaseUtil.getSqlSession();
        UpdateUserInfoCase updateUserInfoCase = sqlSession.selectOne("updateUserInfoCase",2);
        System.out.println(updateUserInfoCase.toString());
        System.out.println(TestConfig.updateUserInfoUrl);
        //发送请求
        int resultJson = getResult(updateUserInfoCase);
        //验证
        User user = sqlSession.selectOne(updateUserInfoCase.getExpected(),updateUserInfoCase);
        Assert.assertNotNull(user);
        Assert.assertNotNull(resultJson);


    }

    private int getResult(UpdateUserInfoCase updateUserInfoCase) throws IOException {
        HttpPost post = new HttpPost(TestConfig.updateUserInfoUrl);
        JSONObject param = new JSONObject();
        param.put("id",updateUserInfoCase.getUserId());
        param.put("userName",updateUserInfoCase.getUserName());
        param.put("password",updateUserInfoCase.getPassword());
        param.put("age",updateUserInfoCase.getAge());
        param.put("sex",updateUserInfoCase.getSex());
        param.put("permission",updateUserInfoCase.getPermission());
        param.put("isDelete",updateUserInfoCase.getIsDelete());
        post.setHeader("Content-Type", "application/json");
        StringEntity entity = new StringEntity(param.toString(),"utf-8");
        post.setEntity(entity);
        TestConfig.closeableHttpClient = HttpClients.custom().setDefaultCookieStore(TestConfig.cookieStore).build();
        HttpResponse response = TestConfig.closeableHttpClient.execute(post);
        String result = EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);
        return Integer.parseInt(result);
    }
}

补充sql

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.course.model">

    <!--获取登录接口case-->
    <select id="loginCase" parameterType="Integer" resultType="com.course.model.LoginCase">
        select * from loginCase where id=#{id};
    </select>
    <!--添加用户接口case-->
    <select id="addUserCase" parameterType="Integer" resultType="com.course.model.AddUserCase">
        select * from addUserCase where id=#{id};
    </select>
    <!--获取用户信息接口case-->
    <select id="getUserInfoCase" parameterType="Integer" resultType="com.course.model.GetUserInfoCase">
        select * from getUserInfoCase where id=#{id};
    </select>
    <!--获取用户列表接口case-->
    <select id="getUserListCase" parameterType="Integer" resultType="com.course.model.GetUserListCase">
        select * from getUserListCase where id=#{id};
    </select>
    <!--获取更新删除用户信息接口case-->
    <select id="updateUserInfoCase" parameterType="Integer" resultType="com.course.model.UpdateUserInfoCase">
        select * from updateUserInfoCase where id=#{id};
    </select>

    <select id="AddUser" parameterType="com.course.model.AddUserCase" resultType="com.course.model.User">
        select * from user where
        userNaem = #{userName} and age = #{age} and sex = #{sex} and permission = #{permission} and isDelete = #{isDelete}

    </select>

    <!--获取用户信息,预期结果获取-->
    <select id="getUserInfo" parameterType="com.course.model.GetUserInfoCase" resultType="com.course.model.User">
        select * from user where id = #{userId};
    </select>

    <!--获取用户列表信息,预期结果获取-->
    <select id="getUserList" parameterType="com.course.model.GetUserListCase" resultType="com.course.model.User">
        select * from user
        <trim  prefix="WHERE" prefixOverrides="and">
            <if test="null != userName and ''!=userName">
                AND userNaem = #{userName}
            </if>
            <if test="null != age and ''!=age">
                AND age = #{age}
            </if>
            <if test="null != sex and ''!=sex">
                AND sex = #{sex}
            </if>
        </trim>
        ;
    </select>
    
    <select id="getUpdateUserInfo" parameterType="com.course.model.GetUserInfoCase" resultType="com.course.model.User">
        select * from user
        <trim prefix="WHERE" prefixOverrides="and">
            <if test="null != userName and ''!=userName">
                AND userName = #{userName}
            </if>
            <if test="null != age and ''!=age">
                AND age = #{age}
            </if>
            <if test="null != sex and ''!=sex">
                AND sex = #{sex}
            </if>
            <if test="null != permission and ''!=permission">
                AND permission = #{permission}
            </if>
            <if test="null != isDelete and ''!=isDelete">
                AND isDelete = #{isDelete}
            </if>
        </trim>
        and id = #{userId};
    </select>
</mapper>
posted @   77的小白  阅读(127)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示