自动化测试之使用postman测试

本次更新,使用springboot+postman来进行接口测试,同时也包含idea+junit+mevam进行函数测试的总结。

Postman的介绍:用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具。今天给大家介绍的这款网页调试工具不仅可以调试简单的css、html、脚本等简单的网页基本信息,它还可以发送几乎所有类型的HTTP请求!Postman在发送网络HTTP请求方面可以说是Chrome插件类产品中的代表产品之一。

如需了解更多,传送门:Postman使用详解

想法:因为需要使用postman来进行测试,所以需要一个本地项目,像以前只写一个单页面的Java程序是不行的(我觉得),所以用比较熟悉的springboot框架来搭建本地项目。在通过,postman来进行接口测试。


准备工作

本次单元测试,被测函数体可以自己选择,我选择黑盒测试时候写的饮料贩卖机。
测试题目如下:
有一个处理单价为5角钱饮料的自动售货机,相应规格说明如下:
(1)若投入5角或1元钱的硬币,按下“橙汁”或 “啤酒”的按钮,则相应的饮料就送出(每次只投入一个硬币,只按下一种饮料的按钮)。
(2)如投入5角的硬币,按下按钮总有饮料送出。
(3)若售货机没有零钱,则显示“零钱找完”的红灯会亮,这时再投入1元硬币并按下按钮后,饮料不送出来而且1元硬币也退出来。
(4)若有零钱,则“零钱找完”的红灯不会亮,若投入1元硬币并按饮料按钮后,则送出饮料同时找回5角硬币。

流程图如下:
在这里插入图片描述
被测函数代码如下:

//下面是在idea里面的实现代码
package com.company;
import java.util.*;
public class Main {


    public static void main(String[] args) {
	// write your code here
        Scanner kb = new Scanner(System.in);
        int FiveCoin_counts = 0;
        int OneCoin_counts = 0;
        int orangejuice = 2;
        int beer = 2;
        boolean flag = true;
        while (flag) {
            System.out.println("请选择你投入的是一元硬币还是五角硬币");
            System.out.println("1.一元硬币    2.五角硬币");
            int coin = kb.nextInt();
            System.out.println("请选择你要的饮料");
            System.out.println("1.橙汁   2.啤酒");
            int drink = kb.nextInt();
            if (drink == 1){
                if (coin == 1) {
                    if (orangejuice > 0) {
                        OneCoin_counts += 1;
                        if (FiveCoin_counts != 0) {
                            System.out.println("橙汁已掉落,同时注意找零");
                            FiveCoin_counts -= 1;
                            orangejuice -= 1;
                        } else {
                            System.out.println("已经没有零钱找零啦");
                        }
                    }
                    else {

                        System.out.println("已经没有橙汁了");
                    }

                } else {
                    if (orangejuice > 0) {
                        FiveCoin_counts += 1;
                        System.out.println("橙汁已掉落");
                        orangejuice -= 1;
                    }
                    else {
                        System.out.println("已经没有橙汁了");
                    }
                }
            }
            else if (drink == 2){
                if (coin == 1) {
                    if (beer > 0) {
                        OneCoin_counts += 1;
                        if (FiveCoin_counts != 0) {
                            System.out.println("啤酒已掉落,同时注意找零");
                            FiveCoin_counts -= 1;
                            beer -= 1;
                        } else {
                            System.out.println("已经没有零钱找零啦");
                        }
                    }
                    else {

                        System.out.println("已经没有啤酒了");
                    }

                } else {
                    if (beer > 0) {
                        FiveCoin_counts += 1;
                        System.out.println("啤酒已掉落");
                        beer -= 1;
                    }
                    else {
                        System.out.println("已经没有啤酒了");
                    }
                }
            }
            System.out.println("是否继续交易");
            System.out.println("1.是    2.否");
            int answer = kb.nextInt();
            if (answer == 2) flag = false;
        }
        System.out.println("欢迎下次光临");
    }
}

测试用例数据:
在这里插入图片描述
以上都是准备工作,准备完以后,可以开始搭框架了

正文

idea内springboot搭建:

单击新建项目,选中如下选中款内信息
在这里插入图片描述
接着填入项目包名等
在这里插入图片描述
下一步是选择依赖,这里选一个web包就足够了,其他需要的时候在mevan的pom内进行添加
在这里插入图片描述
接着填入信息
在这里插入图片描述
点击完成后,路径如下:
在这里插入图片描述
一个基本的springboot框架就搭好了,然后写入具体代码,下面是添加的几个文件夹和文件,详情看注解。
在这里插入图片描述
startController的接口信息:

@PostMapping("/drink")
    public String begin(@RequestParam int coin,@RequestParam int drink,@RequestParam int beer,@RequestParam int orangejuice,@RequestParam int OneCoin_counts,@RequestParam int FiveCoin_counts){
        goodsinfo.setBeer(beer);
        goodsinfo.setOrangejuice(orangejuice);
        goodsinfo.setFiveCoin_counts(FiveCoin_counts);
        goodsinfo.setOneCoin_counts(OneCoin_counts);
        cm.drinking(coin,drink);
        return cm.getresult();
    }

goodsinfo:

package com.unittest.entity;

public class goodsInfo {

    /*
    桩模块
    模拟数据库存储信息
    */

    private static int FiveCoin_counts;//五角硬币的数量

    private static int OneCoin_counts;//一元硬币的数量

    private static int orangejuice;//橙汁的数量

    private static int beer;//啤酒的数量


    public static int getFiveCoin_counts() {
        return FiveCoin_counts;
    }

    public static int getOneCoin_counts() {
        return OneCoin_counts;
    }

    public static void setOneCoin_counts(int oneCoin_counts) {
        OneCoin_counts = oneCoin_counts;
    }

    public static int getOrangejuice() {
        return orangejuice;
    }

    public static void setFiveCoin_counts(int fiveCoin_counts) {
        FiveCoin_counts = fiveCoin_counts;
    }

    public static void setOrangejuice(int orangejuice) {
        goodsInfo.orangejuice = orangejuice;
    }

    public static int getBeer() {
        return beer;
    }

    public static void setBeer(int beer) {
        goodsInfo.beer = beer;
    }

    //商品名称
    public String goodsname(int goodsid){
        switch (goodsid){
            case 1:
                return "橙汁";
            case 2:
                return "啤酒";
            default:
                return "参数溢出";
        }
    }

    //商品价格
    public int goodsprice(int goodsid) {
        switch (goodsid) {
            case 1:
                return 2;
            case 2:
                return 3;
            default:
                return 0;
        }
    }

    //商品数量
    public int goodsnumber(int goodsid){
            switch (goodsid) {
                case 1:
                    return 2;
                case 2:
                    return 2;
                case 3:
                    return 2;
                case 4:
                    return 2;
                default:
                    return 0;
            }
    }



}

CoinMachine:

package com.unittest.service;

import com.unittest.entity.goodsInfo;

public class CoinMachine {
    static int balance = 0;//余额
    static int total = 0;//总计
    goodsInfo goodsinfo = new goodsInfo();
    public int Machine(int goodsid,int money,int number){
        if (goodsid >= 1 && goodsid <= 4) {
            int goodsnumber = goodsinfo.goodsnumber(goodsid);// 调用桩模块获取库存数量
            if (number <= goodsnumber && number >= 1){// 进行库存判断
                int price = goodsinfo.goodsprice(goodsid);// 调用桩模块获取货品价格
                String name = goodsinfo.goodsname(goodsid);// 调用桩模块获取货品名称
                total = price * number;
                if (money >= total) {
                    System.out.println("您购买的商品是" + name + "X" + number + "合计"
                            + price);
                    balance = money - total;
                    if (balance != 0) {
                        System.out.println("找您" + balance + "元");
                    }
                    // 返回提示代码结束购买
                    return 3;
                }
                else {
                    // 返回错误代码显余额不足
                    return 2;
                }
            } else {
                    // 错误代码 1 表示库存溢出
                return 1;
            }
        }
        else {
            // 返回错误代码 0 表示为货号溢出
            return 0;
        }
    }

    private static String result ;
    /*
     * coin为投入硬币的面额,1为一元,2为五角
     * drink为选择的饮料,1为橙汁,2为啤酒
     */

    public void drinking(int coin , int drink){
        int orangejuice = goodsinfo.getOrangejuice();
        int beer = goodsinfo.getBeer();
        int FiveCoin_counts = goodsinfo.getFiveCoin_counts();
        int OneCoin_counts = goodsinfo.getOneCoin_counts();
        if (drink == 1){
            if (coin == 1) {
                if (orangejuice > 0) {
                    OneCoin_counts += 1;
                    if (FiveCoin_counts != 0) {
                        result = "橙汁已掉落,同时注意找零";
                        System.out.println("橙汁已掉落,同时注意找零");
                        FiveCoin_counts -= 1;
                        orangejuice -= 1;
                    } else {
                        result = "已经没有零钱找零啦";
                        System.out.println("已经没有零钱找零啦");
                    }
                }
                else {
                    result = "已经没有橙汁了";
                    System.out.println("已经没有橙汁了");
                }

            } else if (coin == 2){
                if (orangejuice > 0) {
                    FiveCoin_counts += 1;
                    result = "橙汁已掉落";
                    System.out.println("橙汁已掉落");
                    orangejuice -= 1;
                }
                else {
                    result = "已经没有橙汁了";
                    System.out.println("已经没有橙汁了");
                }
            }else {
                result = "参数溢出";
            }
        }
        else if (drink == 2){
            if (coin == 1) {
                if (beer > 0) {
                    OneCoin_counts += 1;
                    if (FiveCoin_counts != 0) {
                        result = "啤酒已掉落,同时注意找零";
                        System.out.println("啤酒已掉落,同时注意找零");
                        FiveCoin_counts -= 1;
                        beer -= 1;
                    } else {
                        result = "已经没有零钱找零啦";
                        System.out.println("已经没有零钱找零啦");
                    }
                }
                else {
                    result = "已经没有啤酒了";
                    System.out.println("已经没有啤酒了");
                }

            } else if (coin == 2){
                if (beer > 0) {
                    FiveCoin_counts += 1;
                    result = "啤酒已掉落";
                    System.out.println("啤酒已掉落");
                    beer -= 1;
                }
                else {
                    result = "已经没有啤酒了";
                    System.out.println("已经没有啤酒了");
                }
            }else {
                result = "参数溢出";
            }
        }else {
            result = "参数溢出";
        }


    }
    public String getresult(){
        return result;
    }
}

pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com</groupId>
    <artifactId>unittest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>unittest</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

然后启动,没有问题,本地项目就整好了,接下来是,postman的配合使用。

如果没有肯定得去下载。我下载的是桌面版,也可以下载谷歌插件版。
下载地址:https://www.postman.com/downloads/
打开如下:

在这里插入图片描述
点击加号,显示如下在这里插入图片描述
修改如下:
在这里插入图片描述
在这里插入图片描述

点击sent,会把coin,drink等值传入接口,接口在调用被测函数,被测函数中调用桩模块来进行模拟数据,通过模拟数据进行被测函数中的判断,被测函数有返回值,通过返回值进行断言判断

可以查询是否通过断言判断:
在这里插入图片描述
断言语句写法:

pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("啤酒已掉落");
});

一个postman测试用例就完成了,然后就根据需求进行添加用例

可以新建一个Collections来存储这些测试用例,下面是我的
在这里插入图片描述
接下来进行Collection操作
在这里插入图片描述
点击run图标,可以对这个collection进行一起测试。在这里插入图片描述

晒一下我的测试用例

在这里插入图片描述
这样子用postman进行接口测试就是基本上完成了。

接下来是idea+junit的测试
选中CoinMachine类,按住alt+enter,生成测试类,如图
在这里插入图片描述
然后在CoinMachineTest类里面新添对drinking()函数的测试代码。

package com.unittest.service;

import com.unittest.entity.goodsInfo;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class CoinMachineTest {
    CoinMachine coinMachine = new CoinMachine();
    goodsInfo Drink = new goodsInfo();

    @Test
    /*
     * 机器里五角硬币数量为1,一元硬币数量为0,橙汁大于0,啤酒大于0
     * 投进去的硬币面额为五角,选择的饮料为橙汁
     */
    public void testdrinking1() {
        Drink.setFiveCoin_counts(1);
        Drink.setOneCoin_counts(0);
        Drink.setOrangejuice(2);
        Drink.setBeer(2);
        coinMachine.drinking(2, 1);
        assertEquals("橙汁已掉落",coinMachine.getresult());
    }

    @Test
    /*
     * 机器里五角硬币数量为0,一元硬币数量为0,橙汁为0,啤酒为0
     * 投进去的硬币面额为五角,选择的饮料为啤酒
     */
    public void testdrinking2() {
        Drink.setFiveCoin_counts(0);
        Drink.setOneCoin_counts(0);
        Drink.setOrangejuice(0);
        Drink.setBeer(0);
        coinMachine.drinking(2, 2);
        assertEquals("已经没有啤酒了",coinMachine.getresult());
    }

    @Test
    /*
     * 机器里五角硬币数量为1,一元硬币数量为0,橙汁大于0,啤酒大于0
     * 投进去的硬币面额为一元,选择的饮料为橙汁
     */
    public void testdrinking3() {
        Drink.setFiveCoin_counts(1);
        Drink.setOneCoin_counts(0);
        Drink.setOrangejuice(2);
        Drink.setBeer(2);
        coinMachine.drinking(1, 1);
        assertEquals("橙汁已掉落,同时注意找零",coinMachine.getresult());
    }

    @Test
    /*
     * 机器里五角硬币数量为0,一元硬币数量为0,橙汁大于0,啤酒大于0
     * 投进去的硬币面额为一元,选择的饮料为橙汁
     */
    public void testdrinking4() {
        Drink.setFiveCoin_counts(0);
        Drink.setOneCoin_counts(0);
        Drink.setOrangejuice(2);
        Drink.setBeer(2);
        coinMachine.drinking(1, 1);
        assertEquals("已经没有零钱找零啦",coinMachine.getresult());
    }

    @Test
    /*
     * 机器里五角硬币数量为0,一元硬币数量为0,橙汁为0,啤酒为0
     * 投进去的硬币面额为一元,选择的饮料为橙汁
     */
    public void testdrinking5() {
        Drink.setFiveCoin_counts(0);
        Drink.setOneCoin_counts(0);
        Drink.setOrangejuice(0);
        Drink.setBeer(0);
        coinMachine.drinking(1, 1);
        assertEquals("已经没有橙汁了",coinMachine.getresult());
    }

    @Test
    /*
     * 机器里五角硬币数量为1,一元硬币数量为0,橙汁大于0,啤酒大于0
     * 投进去的硬币面额为五角,选择的饮料为啤酒
     */
    public void testdrinking6() {
        Drink.setFiveCoin_counts(1);
        Drink.setOneCoin_counts(0);
        Drink.setOrangejuice(2);
        Drink.setBeer(2);
        coinMachine.drinking(2, 2);
        assertEquals("啤酒已掉落",coinMachine.getresult());
    }

    @Test
    /*
     * 机器里五角硬币数量为1,一元硬币数量为0,橙汁大于0,啤酒大于0
     * 投进去的硬币面额为五角,选择的饮料为啤酒
     */
    public void testdrinking7() {
        Drink.setFiveCoin_counts(1);
        Drink.setOneCoin_counts(0);
        Drink.setOrangejuice(0);
        Drink.setBeer(0);
        coinMachine.drinking(2, 2);
        assertEquals("已经没有啤酒了",coinMachine.getresult());
    }

    @Test
    /*
     * 机器里五角硬币数量为1,一元硬币数量为0,橙汁大于0,啤酒大于0
     * 投进去的硬币面额为一元,选择的饮料为啤酒
     */
    public void testdrinking8() {
        Drink.setFiveCoin_counts(1);
        Drink.setOneCoin_counts(0);
        Drink.setOrangejuice(2);
        Drink.setBeer(2);
        coinMachine.drinking(1, 2);
        assertEquals("啤酒已掉落,同时注意找零",coinMachine.getresult());
    }

    @Test
    /*
     * 机器里五角硬币数量为0,一元硬币数量为0,橙汁大于0,啤酒大于0
     * 投进去的硬币面额为一元,选择的饮料为啤酒
     */
    public void testdrinking9() {
        Drink.setFiveCoin_counts(0);
        Drink.setOneCoin_counts(0);
        Drink.setOrangejuice(2);
        Drink.setBeer(2);
        coinMachine.drinking(1, 2);
        assertEquals("已经没有零钱找零啦",coinMachine.getresult());
    }

    @Test
    /*
     * 机器里五角硬币数量为0,一元硬币数量为0,橙汁为0,啤酒为0
     * 投进去的硬币面额为一元,选择的饮料为啤酒
     */
    public void testdrinking10() {
        Drink.setFiveCoin_counts(0);
        Drink.setOneCoin_counts(0);
        Drink.setOrangejuice(0);
        Drink.setBeer(0);
        coinMachine.drinking(1, 2);
        assertEquals("已经没有啤酒了",coinMachine.getresult());
    }

}

然后跑一下测试类,只要能通过就是完成了。

这样子,idea+junit也测试完了。


参考了许多博客才做出这么简单的东西,感谢曾经的前行者,其实本文还是有很多问题的,比如参数化进行测试,有些懒惰,没有彻底完成,就不放上来误导别人了。

https://blog.csdn.net/fxbin123/article/details/80428216

https://blog.csdn.net/cai_iac/article/details/81030619

https://www.cnblogs.com/PeterZhang1520389703/p/11856301.html

踏实一些,不要着急,你想要的,岁月都会给你!小吴加油

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值