標籤:
軟體測試的第一次上機課上,第一次使用JUint對項目進行測試。
安裝是最開始要進行的工作,JUint的安裝是比較容易的,只需將需要的jar包引入到項目中即可
最開始的Triangle代碼如下:
package com.tju.scs;public class Triangle { int a, b, c; public Triangle(){ a = 0; b = 0; c = 0; } public Triangle(int a, int b, int c){ if(a > b) { int temp = a; a = b; b = temp; } if(a > c) { int temp = a; a = c; c = temp; } if(b > c) { int temp = c; c = b; b = temp; } this.a = a; this.b = b; this.c = c; } int test(){ int x = 0; if(a + b < c || a <=0 || b <= 0 || c <= 0) x = 0; else if(a == b && b == c) { x = 1; } else if(a == b || b == c) { x = 2; } else x = 3; return x; } int legal(){ if(this.test() != 0) return 1; else return 0; } int isoscelesTriangle() { if(this.test() == 1 || this.test() == 2) return 1; else return 0; } int equilateralTriangle() { if(this.test() == 1) return 1; else return 0; } int normalTriangle() { if(this.test() == 3) return 1; else return 0; }}
Junit產生的單元測試代碼如下
package com.tju.scs;import static org.junit.Assert.*;import org.junit.Before;import org.junit.Test;public class TriangleTest { Triangle tr = null; @Before public void setUp() throws Exception { int a = 0; int b = 0; int c = 0; tr = new Triangle(a, b, c); } @Test public void testLegal() { assertEquals(0, tr.legal()); } @Test public void testIsoscelesTriangle() { assertEquals(0, tr.isoscelesTriangle()); } @Test public void testEquilateralTriangle() { assertEquals(0, tr.equilateralTriangle()); } @Test public void testNormalTriangle() { assertEquals(0, tr.normalTriangle()); }}
用 a = 0, b = 0, c = 0 作為三邊後,上述測試代碼結果如下:
即此三角形不合法,非等腰,非等邊,非普通三角形。
軟體測試學習筆記:Junit入門