Skip to content

Commit 8a17419

Browse files
committed
[Feature] add testSuite and let it run
1 parent 1bc48b1 commit 8a17419

File tree

14 files changed

+238
-52
lines changed

14 files changed

+238
-52
lines changed

students/812350401/src/main/java/com/coderising/myood/litejunit/liuxinv2/TestListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.coderising.myood.litejunit.liuxinv2;
22

33
/**
4-
* A Listener for test progress
4+
* A TestListener for test progress
55
*/
66
public interface TestListener {
77

students/812350401/src/main/java/com/coderising/myood/litejunit/v2/CalculatorTest.java

-38
This file was deleted.

students/812350401/src/main/java/com/coderising/myood/litejunit/v2/ClientToTest.java

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
package com.coderising.myood.litejunit.v2;
22

33

4+
import com.coderising.myood.litejunit.v2.example_test.AllTest;
5+
46
/**
57
* Created by thomas_young on 21/8/2017.
68
*/
79
public class ClientToTest {
810

911
public static void main(String[] args) {
1012
TestResult tr = new TestResult();
11-
12-
// Test cs1 = new CalculatorTest("testAdd");
13-
// tryTest(cs1, tr);
14-
// Test cs2 = new CalculatorTest("testSubtract");
15-
// tryTest(cs2, tr);
16-
17-
System.out.println("---------------------------------");
18-
tr.clearResult();
19-
Test ts = new TestSuite("AllTest");
20-
// ((TestSuite)ts).addTest(new CalculatorTest("haha"));
21-
((TestSuite)ts).addTest(new TestSuite(CalculatorTest.class));
22-
tryTest(ts, tr);
23-
13+
Test testAll = AllTest.suite();
14+
tryTest(testAll, tr);
2415
}
2516

2617
private static void tryTest(Test test, TestResult tr) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.coderising.myood.litejunit.v2;
2+
3+
/**
4+
* Created by thomas_young on 17/9/2017.
5+
*/
6+
public interface TestListener {
7+
void addError(Test test, Throwable t);
8+
void addFailure(Test test, Throwable t);
9+
void startTest(Test test);
10+
void endTest(Test test);
11+
}

students/812350401/src/main/java/com/coderising/myood/litejunit/v2/TestSuite.java

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public int countTestCases() {
2424
return totalCount;
2525
}
2626

27+
public TestSuite(){
28+
}
2729
public TestSuite(String name) {
2830
this.name = name;
2931
}
@@ -125,4 +127,7 @@ private String exceptionToString(Throwable t) {
125127

126128
}
127129

130+
public void addTestSuite(Class testClass) {
131+
addTest(new TestSuite(testClass));
132+
}
128133
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.coderising.myood.litejunit.v2.example.a;
2+
3+
/**
4+
* Created by thomas_young on 17/9/2017.
5+
*/
6+
public class Calculator {
7+
public int add(int a,int b){
8+
return a + b;
9+
}
10+
public int minus(int a,int b){
11+
return a - b;
12+
}
13+
public int multiply(int a, int b ){
14+
return a * b;
15+
}
16+
public int divide(int a , int b )throws Exception
17+
{
18+
if(b == 0){
19+
throw new Exception("除数不能为零");
20+
}
21+
return a / b;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.coderising.myood.litejunit.v2.example.b;
2+
3+
/**
4+
* Created by thomas_young on 17/9/2017.
5+
*/
6+
public class Byebye {
7+
public String Say(){
8+
return "Byebye!";
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.coderising.myood.litejunit.v2.example.b;
2+
3+
/**
4+
* Created by thomas_young on 17/9/2017.
5+
*/
6+
public class HelloWorld {
7+
public String Say(){
8+
return "Hello,World!";
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.coderising.myood.litejunit.v2.example_test;
2+
3+
import com.coderising.myood.litejunit.v2.Test;
4+
import com.coderising.myood.litejunit.v2.TestSuite;
5+
import com.coderising.myood.litejunit.v2.example_test.a.AAllTest;
6+
import com.coderising.myood.litejunit.v2.example_test.b.BAllTest;
7+
8+
/**
9+
* Created by thomas_young on 17/9/2017.
10+
*/
11+
public class AllTest {
12+
public static Test suite() {
13+
TestSuite suite = new TestSuite();
14+
suite.addTest(AAllTest.suite());
15+
suite.addTest(BAllTest.suite());
16+
return suite;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.coderising.myood.litejunit.v2.example_test.a;
2+
3+
4+
import com.coderising.myood.litejunit.v2.Test;
5+
import com.coderising.myood.litejunit.v2.TestSuite;
6+
7+
/**
8+
* Created by thomas_young on 17/9/2017.
9+
*/
10+
public class AAllTest {
11+
public static Test suite() {
12+
TestSuite suite = new TestSuite();
13+
suite.addTestSuite(CalculatorTest.class);
14+
return suite;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.coderising.myood.litejunit.v2.example_test.a;
2+
3+
import com.coderising.myood.litejunit.v2.Assert;
4+
import com.coderising.myood.litejunit.v2.TestCase;
5+
import com.coderising.myood.litejunit.v2.example.a.Calculator;
6+
7+
/**
8+
* Created by thomas_young on 17/9/2017.
9+
*/
10+
public class CalculatorTest extends TestCase {
11+
public CalculatorTest(String name) {
12+
super(name);
13+
}
14+
15+
/** *//**
16+
* 在Junit3.8中,测试方法满足如下原则
17+
* 1)public
18+
* 2)void
19+
* 3)无方法参数
20+
* 4)最重要的方法名称必须以test开头
21+
*/
22+
private Calculator cal;
23+
24+
//在执行每个test之前,都执行setUp;
25+
public void setUp(){
26+
cal = new Calculator();
27+
}
28+
29+
//在执行每个test之后,都要执行tearDown
30+
public void tearDown(){
31+
}
32+
33+
public void testAdd()
34+
{
35+
Calculator cal = new Calculator();
36+
int result = cal.add(1, 2);
37+
//断言assert
38+
Assert.assertEquals(3, result);
39+
}
40+
41+
public void testMinus()
42+
{
43+
Calculator cal = new Calculator();
44+
int result = cal.minus(5, 2);
45+
Assert.assertEquals(3, result);
46+
}
47+
48+
public void testMultiply()
49+
{
50+
Calculator cal = new Calculator();
51+
int result = cal.multiply(4, 2);
52+
Assert.assertEquals(8,result);
53+
}
54+
55+
public void testDivide()
56+
{
57+
Calculator cal = new Calculator();
58+
int result = 0;
59+
try {
60+
result = cal.divide(10,5);
61+
} catch (Exception e) {
62+
e.printStackTrace();
63+
//我们期望result = cal.divide(10,5);正常执行;如果进入到catch中说明失败;
64+
//所以我们加上fail。
65+
Assert.fail();//如果这行没有执行。说明这部分正确。
66+
}
67+
Assert.assertEquals(2,result);
68+
}
69+
70+
public void testDivide2()
71+
{
72+
Throwable tx = null;
73+
try
74+
{
75+
// Calculator cal = new Calculator();
76+
cal.divide(10,0);
77+
//正常来讲cal.divide(10,0);已经抛出异常,之后的代码不会被执行。
78+
//我们也期望是这样的。所以说如果下面的Assert.fail();执行了。
79+
//我的的测试就失败了。
80+
Assert.fail();//当执行到这里测试失败,后面的代码将不被执行。
81+
} catch (Exception e) {
82+
tx = e;
83+
}
84+
Assert.assertNotNull(tx); //断言tx不为空。也就是说肯定有异常。
85+
Assert.assertEquals(Exception.class,tx.getClass());//断言tx的类型为Exception类型
86+
Assert.assertEquals("除数不能为零", tx.getMessage());
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.coderising.myood.litejunit.v2.example_test.b;
2+
3+
import com.coderising.myood.litejunit.v2.Test;
4+
import com.coderising.myood.litejunit.v2.TestSuite;
5+
6+
/**
7+
* Created by thomas_young on 17/9/2017.
8+
*/
9+
public class BAllTest {
10+
public static Test suite() {
11+
TestSuite suite = new TestSuite();
12+
suite.addTestSuite(HelloWorldTest.class);
13+
suite.addTestSuite(ByebyeTest.class);
14+
return suite;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.coderising.myood.litejunit.v2.example_test.b;
2+
3+
import com.coderising.myood.litejunit.v2.TestCase;
4+
import com.coderising.myood.litejunit.v2.example.b.Byebye;
5+
6+
/**
7+
* Created by thomas_young on 17/9/2017.
8+
*/
9+
public class ByebyeTest extends TestCase {
10+
public ByebyeTest(String name) {
11+
super(name);
12+
}
13+
public void testSay(){
14+
Byebye bye = new Byebye();
15+
assertEquals("Byebye!", bye.Say());
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.coderising.myood.litejunit.v2.example_test.b;
2+
3+
import com.coderising.myood.litejunit.v2.TestCase;
4+
import com.coderising.myood.litejunit.v2.example.b.HelloWorld;
5+
6+
/**
7+
* Created by thomas_young on 17/9/2017.
8+
*/
9+
public class HelloWorldTest extends TestCase {
10+
11+
public HelloWorldTest(String name) {
12+
super(name);
13+
}
14+
15+
public void testSay(){
16+
HelloWorld hi = new HelloWorld();
17+
assertEquals("Hello,World!", hi.Say());
18+
}
19+
}

0 commit comments

Comments
 (0)