三角形的问题进行单元测试和coverage覆盖检测。

软件测试实验报告

实验目的与要求

对三角形的问题进行单元测试的编写和coverage覆盖检测。

实验环境

操作系统:Ubuntu 10.10

编译器: CMake2.8.1 gcc 4.0

开发工具:Code::Blocks 10.05

前端图形展示工具: lcov 1.7

编程语言:C++

实验内容

输入3个整数a、b和c分别作为三角形的三条边,根据输入的数值判断三角形的形状,有以下情况

  1. 非法输入:任意一边的值均小于0或者任意两边之和小于第三边
  2. 直线:任意两边之和等于第三边;
  3. 等腰三角形:任意两边的值相等且与第三边不等;
  4. 等边三角形:三边的值都相等;
  5. 直角三角形:任意两边的平方之和等于第三边;
  6. 钝角三角形:较小的两边的平方之和小于第三边;
  7. 锐角三角形:较小的两边的平方之和大于第三边。

一、单元测试:

(1).根据问题编写C++代码

  1.主函数main.cpp

#include "function/function.h"

using namespace std;

int main(int argc, char* argv[])

{

    int a,b,c,temp;

    char* mesg;

    if(argc<4){

        printf("Usage:%s must need Three parameters",argv[0]);

        return 1;

        }

    a=atoi(argv[1]);

    b=atoi(argv[2]);

    c=atoi(argv[3]);

    //sort (a,b,c)将输入的数据排序a<b<c

    if(a>b){

        temp=b; b=a;a=temp;}

    if(c<b &&c>a){//c->(a,c)

            temp=c; c=b;b=temp;

    }

    else if(c<=a){//c->(0,a]

        temp=a; a=c;c=b,b=temp;

    }

    mesg=JudgeTriangle(a,b,c);

    cout<<mesg<<endl;

    return 0;

}

   2.被调用的头文件function.h

#include <iostream>

#include <math.h>

#include <stdio.h>

#include <stdlib.h>

char* JudgeTriangle(int &a,int &b,int &c);

   3.被调用的文件function.cpp

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

char* JudgeTriangle(int &a,int &b,int &c){

    char* str="";

    if(a<0||b<0||c<0||a+b<c)

        str="this is illegal input";

    else{

        if(a+b==c)//if no sort,then judgement as(a+b<=c||a+c<=b||c+b<=a)

            str="this only construct a line";

        else{

            if( a==b||b==c){// a==b|| b==c ||a==c

                str="this only constuct a isosceles triangle";

                if(a==c)//a==b&& b==c

                    str="this only constuct a equilateral triangle";

                else if(pow(a,2)+pow(b,2)==pow(c,2))

                    str="this only constuct a right triangle";

            }

            else{

                if(pow(a,2)+pow(b,2)==pow(c,2))

                    str="this only constuct a right triangle";

                else if(pow(a,2)+pow(b,2)>pow(c,2))//

                    str="this only constuct a acute triangle";

                else

                    str="this only constuct a obtuse triangle";

            }

            }

    }

    return str;

}

(2)测试用例

   输入特定三角形三边的值,根据打印输出是否与实际一致,若一致即通过。

1.cmake 项目根目录CMakeLists.txt文件测试用例

#test启用

 enable_testing()

# 定义一个宏,用来简化测试工作

macro (do_test arg1 arg2 arg3 result)

add_test (test_abc_${arg1}_${arg2}_${arg3} Demo ${arg1} ${arg2} ${arg3})

set_tests_properties (test_abc_${arg1}_${arg2}_${arg3} PROPERTIES PASS_REGULAR_EXPRESSION ${result})

endmacro (do_test)

# 使用该宏进行一系列的数据测试

#钝角

do_test (2 3 4 "obtuse triangle")

#直角

do_test (3 4 5 "right triangle")

#锐角

do_test (4 5 6 "acute triangle")

#等边

do_test (3 3 3 "equilateral triangle")

#等腰

do_test (2 5 5 "isosceles triangle")

#非法

do_test (2 2 5 "illegal input")

#线段

do_test (2 3 5 "a line")

(3)make项目

结构

-CMakeList.txt

-main.cpp

-function

-------function.h

-------funcion.cpp

-------CMakeList.txt

命令行输入:

$Cmake .

$make

$make test

第一个CMakeList.txt

# CMake 最低版本号要求

cmake_minimum_required (VERSION 2.8)

# 项目信息

project (Triangle_problem1)

# 查找当前目录下的所有源文件

# 并将名称保存到 DIR_SRCS 变量

aux_source_directory(. DIR_SRCS)

# 添加 math 子目录

add_subdirectory(function)

# 指定生成目标

add_executable(Demo ${DIR_SRCS})

# 添加链接库

target_link_libraries(Demo Functions)

#test

 enable_testing()

# 定义一个宏,用来简化测试工作

macro (do_test arg1 arg2 arg3 result)

add_test (test_abc_${arg1}_${arg2}_${arg3} Demo ${arg1} ${arg2} ${arg3})

set_tests_properties (test_abc_${arg1}_${arg2}_${arg3} PROPERTIES PASS_REGULAR_EXPRESSION ${result})

endmacro (do_test)

# 使用该宏进行一系列的数据测试

#钝角

do_test (2 3 4 "obtuse triangle")

#直角

do_test (3 4 5 "right triangle")

#锐角

do_test (4 5 6 "acute triangle")

#等边

do_test (3 3 3 "equilateral triangle")

#等腰

do_test (2 5 5 "isosceles triangle")

#非法

do_test (2 2 5 "illegal input")

#线段

do_test (2 3 5 "a line")

第二个CMakeList.txt

# 查找当前目录下的所有源文件

# 并将名称保存到 DIR_LIB_SRCS 变量

aux_source_directory(. DIR_LIB_SRCS)

# 生成链接库

add_library (Functions ${DIR_LIB_SRCS})

二、Covery覆盖率检测(创建新的项目)

  (1)修改部分代码,将测试的数据放入主函数中

1.main.cpp

#include <iostream>

#include "head.h"

using namespace std;

int main()

{

    JudgeTriangle(2,3,4);

    JudgeTriangle(3,4,5);

    JudgeTriangle(4,5,6);

    JudgeTriangle(3,3,3);

    JudgeTriangle(2,5,5);

    JudgeTriangle(-2,5,5);

    JudgeTriangle(2,3,5);

   return 0;

}

2.被调用函数head.h、head.cpp(基本不变,由于字符串问题有所改动)

#include <string>//引入库

#include <iostream>

#include <math.h>

#include <stdio.h>

#include <stdlib.h>

using namespace std;

void JudgeTriangle(int r1,int r2,int r3){

        int temp,a,b,c;

        string str ="";

        a=r1;b=r2;c=r3;

        //sort (a,b,c)将输入的数据排序a<b<c

        if(a>b){

            temp=b; b=a;a=temp;}

        if(c<b &&c>a){//c->(a,c)

                temp=c; c=b;b=temp;

        }

        else if(c<=a){//c->(0,a]

            temp=a; a=c;c=b,b=temp;

        }

    if(a<0||b<0||c<0||a+b<c)

        str="this is illegal input";

    else{

        if(a+b==c)//if no sort,then judgement as(a+b<=c||a+c<=b||c+b<=a)

            str="this only construct a line";

        else{

            if( a==b||b==c){// a==b|| b==c ||a==c

                str="this only constuct a isosceles triangle";

………………………………

……………………

    cout<<str<<endl;//直接打印输出

}

 

(2)项目结构

-main.cpp

-head.cpp

-head.h

后面lcov后会生产reslut文件夹里面有覆盖的html

(3)使用lcov 进行coverage覆盖截图检测。

   1.准备工作,生成html统计信息

#第一步

install lcov

#第二步

g++ main.cpp h.cpp -fprofile-arcs -ftest-coverage -lgcov -o test_cover

lcov -c -i -d ./ -o init.info

./test_cover

lcov -c -d ./ -o cover.info

genhtml -o result cover.info

  2.使用浏览器打开reslut目录下的index.html

  • 单元测试

 

LastTest.log:(没啥用,在项目后生成的文件夹可以找到)

Start testing: May 06 19:58 CST

----------------------------------------------------------

1/7 Testing: test_abc_2_3_4

1/7 Test: test_abc_2_3_4

Command: "/home/unsp/mydocument/Triangle_problem1/build/Demo" "2" "3" "4"

Directory: /home/unsp/mydocument/Triangle_problem1/build

"test_abc_2_3_4" start time: May 06 19:58 CST

Output:

----------------------------------------------------------

this only constuct a obtuse triangle

<end of output>

Test time =   0.00 sec

----------------------------------------------------------

Test Pass Reason:

Required regular expression found.Regex=[obtuse triangle

]

"test_abc_2_3_4" end time: May 06 19:58 CST

"test_abc_2_3_4" time elapsed: 00:00:00

----------------------------------------------------------

2/7 Testing: test_abc_3_4_5

2/7 Test: test_abc_3_4_5

Command: "/home/unsp/mydocument/Triangle_problem1/build/Demo" "3" "4" "5"

Directory: /home/unsp/mydocument/Triangle_problem1/build

"test_abc_3_4_5" start time: May 06 19:58 CST

Output:

----------------------------------------------------------

this only constuct a right triangle

<end of output>

Test time =   0.00 sec

----------------------------------------------------------

Test Pass Reason:

Required regular expression found.Regex=[right triangle

]

"test_abc_3_4_5" end time: May 06 19:58 CST

"test_abc_3_4_5" time elapsed: 00:00:00

----------------------------------------------------------

3/7 Testing: test_abc_4_5_6

3/7 Test: test_abc_4_5_6

Command: "/home/unsp/mydocument/Triangle_problem1/build/Demo" "4" "5" "6"

Directory: /home/unsp/mydocument/Triangle_problem1/build

"test_abc_4_5_6" start time: May 06 19:58 CST

Output:

----------------------------------------------------------

this only constuct a acute triangle

<end of output>

Test time =   0.00 sec

----------------------------------------------------------

Test Pass Reason:

Required regular expression found.Regex=[acute triangle

]

"test_abc_4_5_6" end time: May 06 19:58 CST

"test_abc_4_5_6" time elapsed: 00:00:00

----------------------------------------------------------

4/7 Testing: test_abc_3_3_3

4/7 Test: test_abc_3_3_3

Command: "/home/unsp/mydocument/Triangle_problem1/build/Demo" "3" "3" "3"

Directory: /home/unsp/mydocument/Triangle_problem1/build

"test_abc_3_3_3" start time: May 06 19:58 CST

Output:

----------------------------------------------------------

this only constuct a equilateral triangle

<end of output>

Test time =   0.00 sec

----------------------------------------------------------

Test Pass Reason:

Required regular expression found.Regex=[equilateral triangle

]

"test_abc_3_3_3" end time: May 06 19:58 CST

"test_abc_3_3_3" time elapsed: 00:00:00

----------------------------------------------------------

5/7 Testing: test_abc_2_5_5

5/7 Test: test_abc_2_5_5

Command: "/home/unsp/mydocument/Triangle_problem1/build/Demo" "2" "5" "5"

Directory: /home/unsp/mydocument/Triangle_problem1/build

"test_abc_2_5_5" start time: May 06 19:58 CST

Output:

----------------------------------------------------------

this only constuct a isosceles triangle

<end of output>

Test time =   0.00 sec

----------------------------------------------------------

Test Pass Reason:

Required regular expression found.Regex=[isosceles triangle

]

"test_abc_2_5_5" end time: May 06 19:58 CST

"test_abc_2_5_5" time elapsed: 00:00:00

----------------------------------------------------------

6/7 Testing: test_abc_2_2_5

6/7 Test: test_abc_2_2_5

Command: "/home/unsp/mydocument/Triangle_problem1/build/Demo" "2" "2" "5"

Directory: /home/unsp/mydocument/Triangle_problem1/build

"test_abc_2_2_5" start time: May 06 19:58 CST

Output:

----------------------------------------------------------

this is illegal input

<end of output>

Test time =   0.00 sec

----------------------------------------------------------

Test Pass Reason:

Required regular expression found.Regex=[illegal input

]

"test_abc_2_2_5" end time: May 06 19:58 CST

"test_abc_2_2_5" time elapsed: 00:00:00

----------------------------------------------------------

7/7 Testing: test_abc_2_3_5

7/7 Test: test_abc_2_3_5

Command: "/home/unsp/mydocument/Triangle_problem1/build/Demo" "2" "3" "5"

Directory: /home/unsp/mydocument/Triangle_problem1/build

"test_abc_2_3_5" start time: May 06 19:58 CST

Output:

----------------------------------------------------------

this only construct a line

<end of output>

Test time =   0.00 sec

----------------------------------------------------------

Test Pass Reason:

Required regular expression found.Regex=[a line

]

"test_abc_2_3_5" end time: May 06 19:58 CST

"test_abc_2_3_5" time elapsed: 00:00:00

----------------------------------------------------------

End testing: May 06 19:58 CST

二、coverage覆盖截图检测

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

奋斗的蜗牛小猿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值