coding:https://coding.net/u/zachary0320/p/git_test3/git
结对小组:(201421153022、201421138411)
题目描述:
把计算模块提取出来,单独创建一个类。 针对提取出来的计算类的接口函数做单元测试。
单元测试代码:
CalcTest.java
int[] x = {3,4}; int[] y = {5,9}; int[] z = {3,0}; @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void testCalc() { Calc c = new Calc(); } @Test public void testPlusIntInt() { Calc.plus(2, 3); Calc.plus(5, 0); Calc.plus(2, 7); } @Test public void testSubtractIntInt() { Calc.subtract(2, 3); Calc.subtract(5, 0); Calc.subtract(2, 8); } @Test public void testMultiplyIntInt() { Calc.multiply(2, 3); Calc.multiply(5, 0); Calc.multiply(4, 8); } @Test public void testDivide() { Calc.divide(x, y); Calc.divide(z, y); Calc.divide(x, z); } @Test public void testYuefen() { Calc.yuefen(x); Calc.yuefen(y); Calc.yuefen(z); } @Test public void testCreate() { Calc.create(); } @Test public void testPlusIntArrayIntArray() { Calc.plus(x, y); Calc.plus(x, z); Calc.plus(y, z); } @Test public void testSubtractIntArrayIntArray() { Calc.subtract(x, y); Calc.subtract(y, z); Calc.subtract(x, z); } @Test public void testMultiplyIntArrayIntArray() { Calc.multiply(x, y); Calc.multiply(x, z); Calc.multiply(y, z); } @Test public void testDivide2() { Calc.divide2(x, y); Calc.divide2(x, z); Calc.divide2(y, z); } @Test public void testJustify() { Calc.justify(x); Calc.justify(y); Calc.justify(z); } // // @Test // public void testGenerate() { // Calc.generate(); // } @Test public void testNumber() { Calc.Number(50); } @Test public void testOperatorGenerate() { Calc.operatorGenerate(50); }
CalcFunction.java
public class CalcFunctionTest { static int N = 6; static String[] titles ; //存储题目 static String[] answers ; //存储答案 static String[] yourAnswers ; //存储回答的答案 CalcFunction a = new CalcFunction(); @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void testGenerate() { a.generate(5); } }
代码测试截图:
eclemma代码覆盖结果:
实验小结:
1、在代码的部分条件语句分支还没有覆盖到,
2、之前都没有做过单元测试,做完之后才发现其实在很多函数有很多问题,比如generate()函数里有变量参数还需要定义,在调用函数时没有仔细去考虑是要定义为static还是用setter,getter调用,
3、单元测试让修改代码也更清楚,条理更清晰,能准确发现一些函数的毛病
psp: