结对编程2—单元测试

林晓芳201421123092、陈惠201421123096

coding 地址:点我查看哦


题目描述:

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

  1. 把计算模块提取出来,单独创建一个类。
  2. 针对提取出来的计算类的接口函数做单元测试。

需求分析:

  1. 测试加法是否能正确工作;
  2. 测试加减乘除功能。
  3. 测试计算类对于各种参数的支持。

设计测试框架, 模拟测试数据:

计算模块的测试用例及运行结果:

复制代码
@Test
    public void testAdd() {
            assertEquals(String.valueOf("3"),Calculator.add("5/3","4/3"));
            assertEquals(String.valueOf("9"), Calculator.add("4","5"));
    }

    @Test
    public void testSub() {
            assertEquals(String.valueOf("1/3"),Calculator.sub("5/3","4/3"));
            assertEquals(String.valueOf("-1"),Calculator.sub("4","5"));
    }

    @Test
    public void testMul() {
            assertEquals(String.valueOf("20/9"),Calculator.mul("5/3","4/3"));
            assertEquals(String.valueOf("20"),Calculator.mul("4","5"));
    }

    @Test
    public void testDiv() throws Exception {
            assertEquals(String.valueOf("5/4"),Calculator.div("5/3","4/3"));
            assertEquals(String.valueOf("4/5"),Calculator.div("4","5"));
    }
复制代码

计算最大公约数测试:

@Test
    public void testGCD() {
        assertEquals(Calculator.GCD(15,10),5);
    }

参数错误提示:

复制代码
public static String div(String a, String b) throws Exception// 除法
    {
        a = a + "/1";
        b = b + "/1";
        String[] m = a.split("/");
        String[] n = b.split("/");
        int numerator1 = new Integer(m[0]);
        int denominator1 = new Integer(m[1]);
        int numerator2 = new Integer(n[0]);
        int denominator2 = new Integer(n[1]);
        if (denominator1 == 0 || numerator2 == 0 || denominator2 == 0) {
            throw new Exception("分母不能为0!");
        }
        int p = numerator1 * denominator2;
        int q = denominator1 * numerator2;
        int t = GCD(p, q);
        return Reduction(p / t, q / t);
    }
复制代码
复制代码
@Test
    public void testDiv() throws Exception {
            assertEquals(String.valueOf("5/4"),Calculator.div("5/3","4/3"));
            assertEquals(String.valueOf("4/5"),Calculator.div("4","5"));
            try{
                Calculator.div("4","0");
                }    
                catch(Exception ex){
                assertEquals("分母不能为0!",ex.getMessage());
                fail("分母不能为0!");
                }
    }
复制代码

 覆盖率:

非摆拍照片:

小结与感受:

在本次实验之前,自己从未尝试过单元测试,对junit的使用也不了解。第一次尝试着对自己写的代码进行测试,主要遇到的问题在于数据的错误,程序本事并没有什么问题。然而,这次实验只是对单元测试的基础学习。希望在今后的编程中,自己也能养成了对自己写的代码进行单元测试的习惯,掌握这项软件工程的核心之一,从而提高自己的代码质量。

评价伙伴:

先来一片面包:队友是面对问题能够积极想办法解决的人,而且很好沟通,遇到问题都能通过讨论轻松解决。

在把肉放上:对编程能力还有待提高。

再来一片面包: 通过两次合作,两个人也有了一定的默契,希望能够互相激励,努力做到更好。

PSP:

PSP2.1

Personal Software Process Stages

Time (%) Senior Student

Time (%)

Planning

计划

1.5h

0.5h

· Estimate

估计这个任务需要多少时间

7h

8h

· Analysis

需求分析 (包括学习新技术)

0.5h

0.5h

· Coding Standard

代码规范

1h

0.5h

· Design

具体设计

0.5h

1.5h

· Coding

具体编码

3h

2.5h

· Test

测试(自我测试,修改代码,提交修改)

2h

1.5h

Reporting

报告

1.5h

1h

原文地址:https://www.cnblogs.com/lxf-092/p/6639258.html