代码改变世界

结对编程2——单元测试

2017-03-29 20:51  c_xx  阅读(171)  评论(1编辑  收藏  举报

码市地址:https://git.coding.net/cjz113/jiedui2.git

结对成员:201421123111  201421123113

题目描述:

  上一周大家为四则运算程序设计了2-3个新功能,本次在隔了一周之后,我们循序渐进地进阶。本次目标:

  1. 把计算模块提取出来,单独创建一个类。
  2. 针对提取出来的计算类的接口函数做单元测试。
  3. 通过单元测试代码,测试加法是否能正确工作;
  4. 通过单元测试代码,测试加减乘除功能。
  5. 通过单元测试代码,测试计算类对于各种参数的支持:

题目要求:

  1. 结对编程实现上述功能,同样的,在程序正式开发之前,请先预估下PSP每个环节的消耗时间(分钟),并在过程中统计实际耗时(分钟),最后提交PSP表格。依然注意,这个主要是给你们自己看的,不必造假数据。
  2. 继续两人结对协作,把编码规范、领航员和驾驶员角色互换做到位。
  3. 单元测试: 有单元测试保证,有代码覆盖率。

提取计算类:

类定义

class cla
{
public:
    string integer(int op,int x,int y);
    string fraction(int op,int a,int b,int c,int d);
    static int maxNumber(int k, int l);
};    

整数:

string integer(int op, int x, int y)
{
    char Operator[] = { '+', '-', '*', '/' };

    string result;

    int element, denominator;        //定义答案分子、分母
    int maxNum;        //定义公约数
    char answer[10];        //答案

    if (Operator[op] == Operator[0])
    {
        int n;
        n = x + y;
        result = sprintf_s(answer, "%d", n);
    }
    else
        if (Operator[op] == Operator[1])
        {
            int n;
            n = x - y;
            result = sprintf_s(answer, "%d", n);
        }
        else
            if (Operator[op] == Operator[2])
            {
                int n;
                n = x*y;
                result = sprintf_s(answer, "%d", n);
            }
            else
                if (Operator[op] == Operator[3])
                {
                    element = x;
                    denominator = y;
                    maxNum = cla::maxNumber(element, denominator);
                    element = element / maxNum;
                    denominator = denominator / maxNum;
                    if (denominator != 1)
                    {
                        result = sprintf_s(answer, "%d/%d", element, denominator);
                    }
                    else
                    {
                        result = sprintf_s(answer, "%d", element);
                    }
                }
                else
                    if (Operator[op] != Operator[0] && Operator[op] != Operator[1] && Operator[op] != Operator[2] && Operator[op] != Operator[3])
                    {
                        result = "error";
                    }
    return result;
}

分数:

string fraction(int op, int a, int b, int c, int d)
{


    char Operator[] = { '+', '-', '*', '/' };

    string result;

    int element, denominator;        //定义答案分子、分母
    int maxNum;        //定义公约数
    char answer[10];        //答案

    if (Operator[op] == Operator[0])
    {
        element = (b*c) + (a*d);
        denominator = a*c;
        maxNum = cla::maxNumber(element, denominator);
        element = element / maxNum;
        denominator = denominator / maxNum;
        if (denominator != 1)
        {
            result = sprintf_s(answer, "%d/%d", element, denominator);
        }
        else
        {
            result = sprintf_s(answer, "%d", element);
        }
    }
    else
        if (Operator[op] == Operator[1])
        {
            element = (b*c) - (a*d);
            denominator = a*c;
            maxNum = cla::maxNumber(element, denominator);
            element = element / maxNum;
            denominator = denominator / maxNum;
            if (denominator != 1)
            {
                result = sprintf_s(answer, "%d/%d", element, denominator);
            }
            else
            {
                result = sprintf_s(answer, "%d", element);
            }
        }
        else
            if (Operator[op] == Operator[2])
            {
                element = b*d;
                denominator = a*c;
                maxNum = cla::maxNumber(element, denominator);
                element = element / maxNum;
                denominator = denominator / maxNum;
                if (denominator != 1)
                {
                    result = sprintf_s(answer, "%d/%d", element, denominator);
                }
                else
                {
                    result = sprintf_s(answer, "%d", element);
                }
            }
            else
                if (Operator[op] == Operator[3])
                {
                    element = b*c;
                    denominator = a*d;
                    maxNum = cla::maxNumber(element, denominator);
                    element = element / maxNum;
                    denominator = denominator / maxNum;
                    if (denominator != 1)
                    {
                        result = sprintf_s(answer, "%d/%d", element, denominator);
                    }
                    else
                    {
                        result = sprintf_s(answer, "%d", element);
                    }
                }
                else
                    if (Operator[op] != Operator[0] && Operator[op] != Operator[1] && Operator[op] != Operator[2] && Operator[op] != Operator[3])
                    {
                        result = "error";
                    }
    return result;
}

取最大公约数:

static int maxNumber(int k, int l)            
{
    while (k%l != 0)
    {
        int t = k%l;
        k = l;
        l = t;
    }
    return l;
}

测试用例:

整数计算测试用例:

TEST_METHOD(integerTest)
        {
            //TrivialCalculator test;
            string actual = test->integer(0, 1, 2);
            string expected = "3";
            Assert::AreEqual(expected, actual);

            actual = test->integer(0, 2, 1);
            expected = "2";
            Assert::AreNotEqual(expected, actual);

            actual = test->integer(1, 1, 2);
            expected = "-1";
            Assert::AreEqual(expected, actual);

            actual = test->integer(1, 2, 1);
            expected = "2";
            Assert::AreNotEqual(expected, actual);

            actual = test->integer(2, 1, 2);
            expected = "2";
            Assert::AreEqual(expected, actual);

            actual = test->integer(2, 2, 1);
            expected = "1";
            Assert::AreNotEqual(expected, actual);

            actual = test->integer(3, 1, 2);
            expected = "1 / 2";
            Assert::AreEqual(expected, actual);

            actual = test->integer(3, 2, 1);
            expected = "2";
            Assert::AreNotEqual(expected, actual);
        }

分数计算测试用例:

TEST_METHOD(fractionTest)
        {
            string actual = test->fraction(0, 1, 2, 1, 2);
            string expected = "1";
            Assert::AreEqual(expected, actual);

            actual = test->fraction(0, 1, 2, 1, 2);
            expected = "2";
            Assert::AreNotEqual(expected, actual);

            actual = test->fraction(1, 1, 2, 1, 2);
            expected = "0";
            Assert::AreEqual(expected, actual);

            actual = test->fraction(1, 1, 2, 1, 2);
            expected = "2";
            Assert::AreNotEqual(expected, actual);

            actual = test->fraction(2, 1, 2, 1, 2);
            expected = "1 / 4";
            Assert::AreEqual(expected, actual);

            actual = test->fraction(2, 1, 2, 1, 2);
            expected = "1";
            Assert::AreNotEqual(expected, actual);

            actual = test->fraction(3, 1, 2, 1, 2);
            expected = "1";
            Assert::AreEqual(expected, actual);

            actual = test->fraction(3, 1, 2, 1, 2);
            expected = "2";
            Assert::AreNotEqual(expected, actual);
        }

最大公约数测试用例:

        TEST_METHOD(maxNumberTest)
        {
            int actual = test->maxNumber(6, 3);
            int expected = 3;
            Assert::AreEqual(expected, actual);

            actual = test->maxNumber(4, 2);
            expected = 3;
            Assert::AreNotEqual(expected, actual);
        }

结果:

我们试了很多次,也问过同学,查了百度,以为是插件问题,也在vs2010、vs2012、vs2013、vs2015都试过,上最终还是没能成功测试

小结与感受:

自学能力真的太重要了,而我明显这方面还很弱,c++的测试以前老师没讲,我们需要自己查资料自己学习,看着很多同学慢慢的都掌握了,测试也在有条不紊的进行,而我们还不知所措,无奈只能向同学求助,按照同学给的教程链接http://www.cnblogs.com/xiehongfeng100/p/4438076.html一步步进行操作,一开始没有注意到要下载Unit Test Generator工具,一直没办法测试成功,耗了很长时间,后来想起来取下载时,出现了这种情况:

那就先去下载.NET Framework吧,却又出现了这种情况:

。。。

自己下的软件不行,那就用一下同学的软件吧,按照同学耐心指导的步骤做下来,在测试的时候,还是出现了:

这样几个错误,找了好久一直不知道该如何修改。。。

单元测试就这么失败了,经过这次作业,我更加深刻的体会到自己自主解决问题的能力还是非常差的,急需提高啊

结对照片:

PSP: