自动化测试

Posted 虫儿aqa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动化测试相关的知识,希望对你有一定的参考价值。

import com.choosefine.automatedtest.cases.usercenter.model.UserList; // model层的包
import com.choosefine.automatedtest.cases.usercenter.service.UserCenterService; // service 层的
import com.choosefine.automatedtest.common.controller.BaseController;    //control层集成的包封装了JSON 数据
import io.swagger.annotations.ApiImplicitParam;   // 请求参数注解
import io.swagger.annotations.ApiImplicitParams;  // params 注解
import io.swagger.annotations.ApiOperation;     // 描述HTTP 方法类型对应的一个操作 CRUD
import org.springframework.beans.factory.annotation.Autowired; //自动扫描包
import org.springframework.web.bind.annotation.*;   // requestMapping  requestparam   绑定参数
 

@RestController             //RestController下面的action返回都默认为ResponseBody  
@RequestMapping("/automated/user")   // 参数路径
public class UserCenterController extends BaseController {   // controller 继承了BaseContriller 层 
    @Autowired                            //自动加载
    UserCenterService userCenterService;            //service层

    @ApiOperation(value = "获取user列表")            //描述http方法类型对应的查找    
    @ApiImplicitParams({                     //参数s  
            @ApiImplicitParam(paramType = "query", name = "realName", dataType = "String", required = false, value = "真实姓名"),
       //query 参数放在requsetParam 中         
            @ApiImplicitParam(paramType = "query", name = "userName", dataType = "String", required = false, value = "登陆名"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", required = true, value = "分页大小,默认10", defaultValue = "10"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", required = true, value = "页码,默认1", defaultValue = "1"),
    })
    @GetMapping         //对应的查找注解
    public String getUserList(String realName, String userName, Integer pageSize, Integer pageNum) {    //获取4个参数
        return responseSuccess(userCenterService.getUserList(realName, userName, pageSize, pageNum));
    }
  //修改说明和注解
    @ApiOperation(value = "修改user信息")
    @PutMapping
    public String updateUsr(@RequestBody UserList userList) {  //用@RequestBody 提交json数据  对应的SQL语句
        return responseSuccess(userCenterService.updateUser(userList));
    }
  //删除说明
    @ApiOperation(value = "删除user")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "int", required = true, value = "user自增id"),
    })
    @DeleteMapping
    public String deleteUser(Integer id) {
        return responseSuccess(userCenterService.deleteUser(id));
    }


    @ApiOperation(value = "新增user")
    @PostMapping
    public String addUser(@RequestBody UserList userList) {
        return responseSuccess(userCenterService.addUser(userList));
    }
}

service 层

import com.choosefine.automatedtest.cases.usercenter.dao.UserCenterDao; //dao层
import com.choosefine.automatedtest.cases.usercenter.model.UserList;  // model层
import com.choosefine.automatedtest.cases.usercenter.vo.UserListPage;  //service对应的显示层
import com.github.pagehelper.PageHelper;    //分页插件
import com.github.pagehelper.PageInfo;     //分页信息 
import org.springframework.beans.factory.annotation.Autowired;  //自动加载
import org.springframework.stereotype.Service;  //用于@Service注解

import java.util.List;

/**
 * Created by Administrator on 2017/7/20.
 */
@Service  
public class UserCenterService {
    @Autowired
    UserCenterDao userCenterDao;
    //用户列表类 4个参数 真实姓名,用户名,页面大小,页面数量  这个类的类型是UserListPage 
    public UserListPage getUserList(String realName,String userName,Integer pageSize,Integer pageNum){
     //分页的核心代码 
        PageHelper.startPage(pageNum,pageSize);
     //UserList 是model类 对应数据库的字段 list是个列表 DAO层的真实 姓名和 用户名两个参数传递到dao层 里面的方法findUserList
        List<UserList> list = userCenterDao.findUserList(realName,userName);
      PageInfo pageInfo = new PageInfo<>(list);      //实例化一个PageInfo并把list放入
        UserListPage userListPage = new UserListPage();  //实例一个userListPage 
  //把pageInfo类下的列表,分页等信息传到展示界面userListPage 
        userListPage.setList(pageInfo.getList());      //把pageInfo的列表放到list中 
        userListPage.setPageSize(pageInfo.getPageSize());  //大小
        userListPage.setPageNum(pageInfo.getPageNum());    //数量
        userListPage.setTotalRows(pageInfo.getTotal());    //总量
        userListPage.setTotalPage(pageInfo.getPages());    //页数
        return userListPage;                    //返回  
    }
  //更新user 把model层的列表传给DAO层的 mybatise有着通用的接口查询直接调用
    public int updateUser(UserList userList){
        return userCenterDao.updateByPrimaryKeySelective(userList);
    }
  //更新是dao层自定义的upadteValid 
    public int deleteUser(Integer id){
        return userCenterDao.updateValid(id);
    }
  //新增  调用通用方法
    public int addUser(UserList userList){ return  userCenterDao.insertSelective(userList);}

 
}

 

VO层 是展示界面

import com.choosefine.automatedtest.cases.usercenter.model.UserList;
import com.choosefine.automatedtest.common.vo.PageVo;

import java.util.List;

public class UserListPage extends PageVo<UserList>{

}

 model 层 

import io.swagger.annotations.ApiModelProperty;

import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

/**
 * 对应数据库字段
 */
@Table(name = "automated_user")
public class UserList {
    @Id
    @ApiModelProperty(value = "user自增id")
    private Integer id;
    @ApiModelProperty(value = "真实姓名")
    private String realName;
    @ApiModelProperty(value = "登录名")
    private String userName;
    @ApiModelProperty(value = "登录密码")
    private String password;
    @ApiModelProperty(value = "是否有效,不用传此参数,默认为1")
    private Integer isValid;
    @ApiModelProperty(value = "创建时间,新增时不用传此参数")
    private Date createTime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getIsValid() {
        return isValid;
    }

    public void setIsValid(Integer isValid) {
        this.isValid = isValid;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

这段是comm类的pageVo类

public class PageVo<T> {
    private List<T> list;
    private Integer pageSize;
    private Integer pageNum;
    private long totalRows;
    private Integer totalPage;

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public long getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(long totalRows) {
        this.totalRows = totalRows;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }
}
View Code

  vo 层代码 

public class UserListPage extends PageVo<UserList>{
//所有分页的代码都继承 PageVo<model类>
}

dao 层代码

 

import com.choosefine.automatedtest.cases.usercenter.dynamicsql.DynamicSql; //动态sql语句 
import com.choosefine.automatedtest.cases.usercenter.model.UserList;  //实体类
import com.choosefine.automatedtest.common.dao.BaseMapper;  // baseMapper类 该类继承了 Mapper<T>,mysqlMapper<T>
import org.apache.ibatis.annotations.Mapper;  //注解mapper  后面跟着Sql 
import org.apache.ibatis.annotations.Param;   //注解 param
import org.apache.ibatis.annotations.SelectProvider;  // 注解SelectProvider
import org.apache.ibatis.annotations.Update;  //  注解Upadte

import java.util.List;

/**
 * Created by Administrator on 2017/7/20.
 */
@Mapper
public interface UserCenterDao extends BaseMapper<UserList>{ // baseMapper 见上
    @SelectProvider(    //定义拼装sql 
            type = DynamicSql.class,
            method = "dynamicSql"
    )      
    public List<UserList> findUserList(@Param("realName") String realName, @Param("userName") String userName);    //查询
    @Update("update automated_user set is_valid = 0 where id = #{id}")    //注解方式sql语句
    int updateValid(@Param("id") Integer id);


}

 

动态Sql

import org.apache.ibatis.annotations.Param;  //参数注解
import org.apache.ibatis.jdbc.SQL;  // Jdbc

/**
 *select * from automated_user where real_name like  \'%周%\' and  user_name like \'%z%\' and is_valid = 1 order by \'creat_time\' desc  
*/ 
public class DynamicSql {      // 动态注解类
  //方法 动态SQL 两参数 真实姓名和用户名
    public String dynamicSql(@Param("realName") String realName, @Param("userName") String userName){
        SQL sql = new SQL();            // 实例化SQL 
        sql.SELECT("*");          // select *
        sql.FROM("automated_user");   //  select * from automated_user  
        if(realName != null && !realName.equals("")){          //当真实姓名不为空且真实姓名等于""
            sql.WHERE("real_name LIKE CONCAT(CONCAT(\'%\', #{realName}), \'%\')");     // select * from automated_user where real_name like  \'%周%\' and  user_name like \'%z%\' and is_valid = 1 order by \'creat_time\' desc  
         }
        if(userName != null && !userName.equals("")){
            sql.WHERE("user_name LIKE CONCAT(CONCAT(\'%\', #{userName}), \'%\')");
        }
        sql.WHERE("is_valid = 1");
        sql.ORDER_BY("create_time desc");
        return sql.toString();
    }
}

 

  表

 

 

.

以上是关于自动化测试的主要内容,如果未能解决你的问题,请参考以下文章

postman 自动生成 curl 代码片段

如何设置 vscode 的代码片段,以便在自动完成后自动触发 vscode 的智能感知?

CTS测试CtsWindowManagerDeviceTestCases模块的testShowWhenLockedImeActivityAndShowSoftInput测试fail项解决方法(代码片段

;~ 小部分AutoHotkey源代码片段测试模板2019年10月9日.ahk

VSCode 配置 用户自定义代码片段 自定义自动代码补充

浅析Minium,微信小程序自动化测试框架