20200924-3 单元测试,结对

此作业要求请参加作业要求20200924-3 单元测试,结对

项目源码参见git@e.coding.net:qqq2/f4/f4.git

作业要求

2人结对,使用TDD测试框架 (如NUnit, JUnit, cppUnit等)完成本周作业四则运算试题生成的单元测试。

要求1

对每个功能,先给出测试用例,然后再编码功能。请注意把测试用例视为功能需求完成的检验指标。 (40分)

A:

  1. 问题1的测试代码在test/f4_1_test.py文件。

问题1的算法采用了最基础的拼接方法,随机生成4个数字和三个操作符,并按顺序拼接到一起(num1 op1 num2 op2 num3 op3 num4),得到四则表达式。

问题1采用了四个测试用例,分别是检测生成的是否是数组,是否操作符,计算结果的正确性,测试数组转字符串的正确性,代码如下。

    def test_digitals_number(self):
        """test number of digitals, must have 4 digitals"""
        self.assertEqual(len(self.digitals), 4)
    
    def test_ops_number(self):
        self.assertEqual(len(self.ops), 3)

    def test_answer(self):
        if len(self.digitals) == 3 and len(self.ops) == 3:
            ans = self.digitals[0]
            for ii, op in enumerate(self.ops):
                ans = getattr(operator, op.__name__)(ans, self.digitals[ii+1])
            self.assertEqual(self.ans, ans)
        else:
            self.assertIsNotNone(self.ans)

    def test_2_string(self):
        """"""
        ops_dic = {"add":"+", "sub":"-", "mul":"*", "truediv":"/"}
        string = f"{self.digitals[0]} {ops_dic[self.ops[0].__name__]} {self.digitals[1]} {ops_dic[self.ops[1].__name__]} {self.digitals[2]} {ops_dic[self.ops[2].__name__]} {self.digitals[3]} ="
        self.assertEqual(self.string, string)
  1. 问题2-4的测试代码在test/f4.py文件。

问题2-4采用全新算法实现,该算法使用二叉树来生成四则表达式,主要有如下指标:

  1. 生成一颗符合要求的二叉树并正确初始化;
    def test_tree(self):
        """
        a tree not None and
        a tree all partent node are op function objection and
        all leaf nodes are number
        test will run 200 times
        """
        for ii in range(0, 200):
            tree = copy.deepcopy(trees[random.randint(0, len(trees) - 1)])
            init_tree(tree)
            self.assertIsNotNone(tree)
            self.assertTrue(self.__lnr__(tree))
  1. 使用二叉树转换的表达式字符串计算结果与程序生成的计算结果一致(直接使用二叉树使用逆波兰算法);
    def test_calc(self):
        """"""
        for ii in range(0, 200):
                tree = copy.deepcopy(trees[random.randint(0, len(trees) - 1)])
                init_tree(tree)
                lrn = tree_lrn(tree)
                lnr, op = tree_lnr(tree)
                try:
                    # if div 0, calc_list will raise a ValueError(right action)
                    # have to catch that error and skip
                    ans = calc_list(lrn)
                except Exception:
                    continue
                string = list2string(lnr)
                self.assertEqual(ans, self.__calc_string__(string))
                self.assertIsNotNone(None)
  1. 读取程序生成的文件,并测试表达式和结果的正确性。
    def test_file_out(self):
        """"""
        os.system("python src/main.py -c 500")
        with open("question.txt", "r") as f:
            for line in f.readlines():
                line = line.replace("\n", "")
                line = line.replace(r"\\", "")
                arr = line.split("=")
                if len(arr) == 2:
                    exp = arr[0]
                    ans_arr = arr[1].split(" ")
                    temp = []
                    for item in ans_arr:
                        if item != "":
                            temp.append(item)
                    ans_arr = temp
                    if len(ans_arr) == 1:
                        ans = Fraction(ans_arr[0])
                    elif len(ans_arr) == 2:
                        ans = Fraction(ans_arr[0]) + Fraction(ans_arr[1])
                    else:
                        raise ValueError(f"value error: {arr[1]}")
                    self.assertEqual(ans, self.__calc_string__(exp))

要求2

在博客报告测试用例全部fail 到 全部pass 的过程,报告事实 (fail到修改代码或者测试用例,到pass) 以及收获。 除了最初的框架,测试用例中存在一次性pass没有经过fail的,也报告一次性通过,给出如此优秀地实现了这部分功能的代码。由2位同学中的一位发布博客提交到作业,指明自己的结对伙伴;另一位在作业中引用这一博客,指明自己的结对伙伴。(40分)

A.

fig 1: 构建测试用例,但还未正确实现,全部不通过

fig 2: 成功生成数字数组

fig 3: 成功生成操作符数组

fig4: 将数组转换为表达式字符串未通过

fig 5: 修改后测试通过

fig 6: 构建全部测试用例

fig 7: 生成树

fig 8: 表达式

fig 9: 文件

要求3

做好准备,在接下的一周你可能无法通过别人的测试用例。 (0分)

要求4

使用coding.net做版本控制。checkin 前要求清理 临时文件、可执行程序,通常执行 build-clean可以达到效果。(5分)

A: 项目位于git@e.coding.net:qqq2/f4/f4.git

备忘

这是教师备忘,与本次课程的同学无关。
需要在作业要求文本中明确标明,两人结对,但是博客不得相同。
周昊指出,结对中的图片属于二人共同所有。

posted @ 2020-10-07 00:50  WenqiangXie  阅读(88)  评论(0编辑  收藏  举报