单元测试练习(个人练习)
测试用例如下(红色为错误预言):
根据测试用例,测试代码部分如下:
@Test public void testIsTriangle1() { Triangle t = new Triangle(3,3,3); assertEquals(t.getType(t),"Regular"); } @Test public void testIsTriangle2() { Triangle t = new Triangle(2,1,3); assertEquals(t.getType(t),"Illegal"); } @Test public void testIsTriangle3() { Triangle t = new Triangle(-3,-3,-3); assertEquals(t.getType(t),"Regular"); } @Test public void testIsTriangle4() { Triangle t = new Triangle(4,5,6); assertEquals(t.getType(t),"Scalene"); } @Test public void testIsTriangle5() { Triangle t = new Triangle(5,6,6); assertEquals(t.getType(t),"Isoceles"); } @Test public void testIsTriangle6() { Triangle t = new Triangle(1,5,8); assertEquals(t.getType(t),"Illegal"); } @Test public void testIsTriangle7() { Triangle t = new Triangle(0,2,2); assertEquals(t.getType(t),"Illegal"); } public void testIsTriangle8() { Triangle t = new Triangle(-2,-2,2); assertEquals(t.getType(t),"Illegal"); }
测试结果如下:
结果显示为:除了3与预言相反,其他均通过
体会:
测试用例时,根据等价划分,可以分为四类:非三角形,不等边三角形即一般三角形,等腰三角形和等边三角形。在输入测试数据时,要注意特殊数据的测试,比如(-2,-2,2)等。本来想测试(1.3.4)这样的数据的,但是不会操作。这次最大的收获就是懂得使用Junit,搭建Junit,也充分认识到一个程序的正确性和可行性不能只凭简单的数据测试二一了了只,要考虑到数据的全面性和覆盖性。
完整代码请见:https://github.com/youwanqing/home3