软件测试之单元测试和自动化测试及UTF应用

软件测试之单元测试和自动化测试及UTF应用


######资源在最后

题目一: 测试方法及单元测试 (资源在最后)

1.1 单元测试概述

单元测试是软件开发中的一项关键技术,它是指对软件系统中的最小可测试单元进行测试,以确保其能够按照预期的方式工作。这些最小可测试单元通常是代码中的函数、方法或类,单元测试的目的是验证它们的行为是否正确、是否满足预期的要求和功能。

单元测试是一种自动化测试,它可以通过编写测试用例,执行代码,并验证代码的行为是否符合预期。在单元测试中,测试用例应该涵盖各种可能的输入和边界情况,以确保代码在所有情况下都能正确地运行。

单元测试是敏捷开发、测试驱动开发和持续集成的基础。通过进行单元测试,开发人员可以更快速地发现并解决代码中的问题,避免在后期阶段出现更加严重的问题。单元测试还可以提高代码质量,降低维护成本,并帮助开发人员更好地理解和修改代码。

1.2 问题

1.2.1 基于Java 8的Stream类中的distinct)方法,其功能需求如下:
/**
     * Returns a stream consisting of the distinct elements (according to
     * {@link Object#equals(Object)}) of this stream.
     *
     * <p>For ordered streams, the selection of distinct elements is stable
     * (for duplicated elements, the element appearing first in the encounter
     * order is preserved.)  For unordered streams, no stability guarantees
     * are made.
     *
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
     * intermediate operation</a>.
     *
     * @apiNote
     * Preserving stability for {@code distinct()} in parallel pipelines is
     * relatively expensive (requires that the operation act as a full barrier,
     * with substantial buffering overhead), and stability is often not needed.
     * Using an unordered stream source (such as {@link #generate(Supplier)})
     * or removing the ordering constraint with {@link #unordered()} may result
     * in significantly more efficient execution for {@code distinct()} in parallel
     * pipelines, if the semantics of your situation permit.  If consistency
     * with encounter order is required, and you are experiencing poor performance
     * or memory utilization with {@code distinct()} in parallel pipelines,
     * switching to sequential execution with {@link #sequential()} may improve
     * performance.
     *
     * @return the new stream
     */
    Stream<T> distinct();

完成:
(1) 基于黑盒测试方法-等价类与边界值等设计测试用例
测试项目名称测试用例编号输入数据预期输出实际输出测试状态测试人员编制日期功能特性
distinct1null11成功小龙龙2023.04.27返回一个由该流的不同元素组成的新流,其中判断元素是否相同使用Object.equals(Object)方法
distinct21,111成功小龙龙2023.04.27返回一个由该流的不同元素组成的新流,其中判断元素是否相同使用Object.equals(Object)方法
distinct31,222成功小龙龙2023.04.27返回一个由该流的不同元素组成的新流,其中判断元素是否相同使用Object.equals(Object)方法
distinct41000000个相同的111成功小龙龙2023.04.27返回一个由该流的不同元素组成的新流,其中判断元素是否相同使用Object.equals(Object)方法
distinct5999999个相同的111成功小龙龙2023.04.27返回一个由该流的不同元素组成的新流,其中判断元素是否相同使用Object.equals(Object)方法
distinct61000001个相同的111成功小龙龙2023.04.27返回一个由该流的不同元素组成的新流,其中判断元素是否相同使用Object.equals(Object)方法
distinct71000000个不同的110000001000000成功小龙龙2023.04.27返回一个由该流的不同元素组成的新流,其中判断元素是否相同使用Object.equals(Object)方法
distinct8999999个不同的1999999999999成功小龙龙2023.04.27返回一个由该流的不同元素组成的新流,其中判断元素是否相同使用Object.equals(Object)方法
distinct91000001个不同的110000001000001成功小龙龙2023.04.27返回一个由该流的不同元素组成的新流,其中判断元素是否相同使用Object.equals(Object)方法
(2)基于Junit5完成单元测试(至少基本测试与参数化测试)
其他要求:
(1) 测试数据应包含你的姓名简写、学号、出生日期等信息(可能的情况下);
(2) 测试类的名字Stream08(08是你的学号最后两位)
基本测试:
  //    单元测试(distinct)

    //    基本测试测试
    @Test
    @ParameterizedTest
    void testDistinct01(){
        /*
         * 下界 0 1 2 个
         * 中间 6 个
         * 上界 10 11 12 个*/

//        0个
        List<String> list_0 = new ArrayList<>();
        assertEquals(0l,list_0.stream().distinct().count());

//        1个
        List<String> list_1 = new ArrayList<>();
        list_1.add("xu");
        list_1.add("xu");
        list_1.add("xu");
        list_1.add("xu");
        assertEquals(1l,list_1.stream().distinct().count());
        
        //        2个
        List<String> list_2 = new ArrayList<>();
        list_2.add("xu");
        list_2.add("xiao");
        list_2.add("xu");
        list_2.add("xu");
        assertEquals(2l,list_2.stream().distinct().count());

//        6个
        List<String> list_6 = new ArrayList<>();
        list_6.add("xu");
        list_6.add("xiao");
        list_6.add("long");
        list_6.add("shi");
        list_6.add("da");
        list_6.add("shuai");
        list_6.add("shuai");
        list_6.add("shuai");
        list_6.add("shuai");
        assertEquals(6l,list_6.stream().distinct().count());

//        10个
        List<String> list_10 = new ArrayList<>();
        list_10.add("xu");
        list_10.add("xiao");
        list_10.add("long");
        list_10.add("shi");
        list_10.add("da");
        list_10.add("shuai");
        list_10.add("ge");
        list_10.add("yin");
        list_10.add("jun");
        list_10.add("feng");
        list_10.add("xu");
        list_10.add("xu");
        assertEquals(10l,list_10.stream().distinct().count());
//        11个
        List<String> list_11 = new ArrayList<>();
        list_11.add("xu");
        list_11.add("xiao");
        list_11.add("long");
        list_11.add("shi");
        list_11.add("da");
        list_11.add("shuai");
        list_11.add("ge");
        list_11.add("yin");
        list_11.add("jun");
        list_11.add("feng");
        list_11.add("sa");
        list_11.add("xu");
        list_11.add("xu");
        assertEquals(11l,list_11.stream().distinct().count());

        //        12个
        List<String> list_12 = new ArrayList<>();
        list_12.add("xu");
        list_12.add("xiao");
        list_12.add("long");
        list_12.add("shi");
        list_12.add("da");
        list_12.add("shuai");
        list_12.add("ge");
        list_12.add("yin");
        list_12.add("jun");
        list_12.add("liu");
        list_12.add("sa");
        list_12.add("feng");
        assertEquals(12l,list_12.stream().distinct().count());

    }


测试结果:

在这里插入图片描述

参数化测试:
  @Test
    @ParameterizedTest
    @MethodSource("streamDistinct02")
    void testDistinct02(long expected, Stream stream){
        /*
         * 下界 0 1 2 个
         *
         * 上界 4 5 6 个*/
        assertEquals(expected,stream.distinct().count());
    }
    static Stream<Arguments> streamDistinct02() {
        Stream<String> s0 = Stream.of();
        Stream<String> s1 = Stream.of("20010222","20010222");
        Stream<String> s2 = Stream.of("20010222", "20020222");

        Stream<String> s4 = Stream.of("20010222", "20020222", "20021126", "20010223");
        Stream<String> s5 = Stream.of("20010222", "20020222", "20021126", "20010223", "20010224");
        Stream<String> s6 = Stream.of("20010222", "20020222", "20021126", "20010223", "20010224", "20010225");
        return Stream.of(
                Arguments.of(0l,s0),
                Arguments.of(1l,s1),
                Arguments.of(2l,s2),

                Arguments.of(4l,s4),
                Arguments.of(5l,s5),
                Arguments.of(6l,s6)
        );
    }
测试结果:

在这里插入图片描述

1.2.2 以下为JDK中ArrayList的remove()方法,请应用白盒测试方法设计测试用例,并应用JUnit5完成测试
    /**
     * Removes the element at the specified position in this list.
     * Shifts any subsequent elements to the left (subtracts one from their
     * indices).
     *
     * @param index the index of the element to be removed
     * @return the element that was removed from the list
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    public E remove(int index) {
        rangeCheck(index);

        modCount++;
        E oldValue = elementData(index);

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work

        return oldValue;
    }

要求:
(1) 完成分别满足语句覆盖、判定覆盖、条件覆盖的测试用例;
语句覆盖:(白盒测试)
用例编号输入数据预期输出实际输出测试状态
1[null],0数组为空提示数组为空提示成功
2[xiao,xiao,long],022成功
3[xiao,xiao,long,20100232,2020123238],044成功
测试结果分析:符合要求。
判定覆盖:(白盒测试)
用例编号输入数据预期输出实际输出测试状态
1[20100232],0false0成功
2[20100232,2020123238],0true1成功
判定覆盖:(白盒测试)
用例编号输入数据预期输出实际输出测试状态
1[20100232],000成功
2[20100232,2020123238],011成功
(2) 完成满足路径覆盖的测试用例
用例编号输入数据预期输出实际输出测试状态
1[20100232],100000000索引超出异常提示索引超出异常提示成功
2[20100232],000成功
3[20100232,2020123238],011成功
(3) 基于Junit5完成测试

上面测试用例都是基于Junit5完成的

题目二: 自动化测试及UTF应用

2.1 下面是基于QTP自带的飞机订票(flight4b)录制过程:

(1)录制脚本:成功登陆订票窗口→在订票系统中输入航班日期→选择起飞地点→选择目的地→选择航班→输入顾客姓名→输入票的张数→选择航班级别→单击订票按钮;
(2)在日期栏输入日期,选择起始和目的地点:Denver和Frankfurt,选择航班号15819,确定订单中的航班,输入订票姓名”你的姓名简拼”,订票张数是2张,舱位选择first,确定当前订票信息,单击”insert order”按钮
(3)脚本录制完成后,脚本如下所示:
Dialog("Login").WinEdit("Agent Name:").Set "xuxiaolong"
Dialog("Login").WinEdit("Password:").SetSecure "644b1c4be81020850ad616b3a4ba391ecb23b0e6"
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").ActiveX("MaskEdBox").Type "050523"
Window("Flight Reservation").WinComboBox("Fly From:").Select "Denver"
Window("Flight Reservation").WinComboBox("Fly To:").Select "Frankfurt"
Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinList("From").Select "15829   DEN   08:00 AM   FRA   08:45 AM   AA     $100.50"
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinRadioButton("First").Set
Window("Flight Reservation").WinEdit("Customer Name:").Set "xuxiaolong"
Window("Flight Reservation").WinEdit("Tickets:").SetSelection 0,1
Window("Flight Reservation").WinEdit("Tickets:").Set "2"
Window("Flight Reservation").WinButton("Insert").Click

请分析现有脚本,采用参数化测试的方法,设计并实现自动化测试脚本,参数化内容包括:出发日期(date=2023年6月+你的出生日)、出发地{取前M个(M=你的学号mod6,如果值为0,取6)个},到达地{取前N个(N=M/2+1)},航班{取前K个(K=学号mod3;if K=0,K=1},订票数量(随机数1-3),其他默认。

脚本录制:

我直接贴源码了,上面的要求只需要修改for遍历和姓名,日期即可

Dialog("Login").WinEdit("Agent Name:").Set "xiaolonglong"
Dialog("Login").WinEdit("Password:").SetSecure "644b1c4be81020850ad616b3a4ba391ecb23b0e6"
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").ActiveX("MaskEdBox").Type "062223"

'遍历所有的出发地;
'1.出发地个数
flyFromCount = Window("Flight Reservation").WinComboBox("Fly From:").GetItemsCount
                   
'2.遍历过程
For IflyFrom = 0 To 1 Step 1
'第二次之后,把前面做的做一遍
    If IflyFrom > 0 Then            
        Window("Flight Reservation").WinButton("Button").Click
        Window("Flight Reservation").ActiveX("MaskEdBox").Type "062223"        
    End If
    
    '选择出发地
    Window("Flight Reservation").WinComboBox("Fly From:").Select IflyFrom
    
    '根据当前出发地,遍历所有的到达地;
    '1.到达地个数 前两个
    flyToCount = Window("Flight Reservation").WinComboBox("Fly To:").GetItemsCount
    If flyToCount >= 2 Then
        '2.遍历过程    
       For IflyTo = 0 To 1 Step 1    
        '第二次之后,把前面做的做一遍
        If IflyTo > 0 Then            
            Window("Flight Reservation").WinButton("Button").Click
            Window("Flight Reservation").ActiveX("MaskEdBox").Type "062223"
            Windo
w("Flight Reservation").WinComboBox("Fly From:").Select IflyFrom
        End If
       '选择到达地
       Window("Flight Reservation").WinComboBox("Fly To:").Select IflyTo
        '点击订票button
        Window("Flight Reservation").WinButton("FLIGHT").Click
        '取得当前出发地与到达地之间的所有航班
        flightsCount = Window("Flight Reservation").Dialog("Flights Table").WinList("From").GetItemsCount
        If flightsCount >=2 Then
            'b遍历所有的航班
        For Iflight = 0 To 1 Step 1
            '第二次之后,把前面做的做一遍
            If Iflight > 0 Then            
                Window("Flight Reservation").WinButton("Button").Click
                Window("Flight Reservation").ActiveX("MaskEdBox").Type "062223"
                Window("Flight Reservation").WinComboBox("Fly From:").Select IflyFrom
                Window("Flight Reservation").WinComboBox("Fly To:").Select IflyTo
                Window("Flight Reservation").WinButton("FLIGHT").Click
            End If
            '选择航班
            Window("Flight Reservation").Dialog("Flights Table").WinList("From").Select Iflight
            '确定上述订票信息
            Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
            '订票人
        
            Window("Flight Reservation").WinEdit("Customer Name:").Set "xuxiaolong"
            '票数
            Window("Flight Reservation").WinEdit("Tickets:").SetSelection 0,1
            Window("Flight Reservation").WinEdit("Tickets:").Set RandomNumber(1,3)
            '舱位
            Window("Flight Reservation").WinRadioButton("First").Set
            '生成订单
            
            Window("Flight Reservation").WinButton("Insert").Click
        Next
        End If
    Next
    End If
    
Next


实验结果

在这里插入图片描述
github源码下载点我

CSDN源码下载点我

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是一只小小小小龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值