junit多个类组合测试(简单示例)

一个简单的junit测试多个类方式:

 1 package com.lenovo.unit3;
 2 
 3 /**
 4  * 模拟对象
 5  */
 6 public class TSP {
 7 
 8     public int shortestPath(int num) {
 9         try {
10             Thread.sleep(num);
11         } catch (InterruptedException e) {
12             e.printStackTrace();
13         }
14         return num;
15     }
16 }
 1 package com.lenovo.unit3;
 2 
 3 import junit.framework.TestCase;
 4 
 5 public class TestSimple extends TestCase {
 6 
 7     public TestSimple(String name){
 8         super(name);
 9     }
10 
11     public void testAdd(){
12         assertEquals(2,1+1);
13         assertEquals(4,2+2);
14         assertEquals(-8,-12+4);
15     }
16 }
 1 package com.lenovo.unit3;
 2 
 3 import junit.framework.TestCase;
 4 
 5 public class TestClassOne extends TestCase {
 6 
 7     public TestClassOne(String method){
 8         super(method);
 9     }
10 
11     public void testAddition(){
12         assertEquals(4,2+2);
13     }
14 
15     public void testSubtraction(){
16         assertEquals(0,2-2);
17     }
18 }
 1 package com.lenovo.unit3;
 2 
 3 import junit.framework.Test;
 4 import junit.framework.TestCase;
 5 import junit.framework.TestSuite;
 6 
 7 public class TestClassTwo extends TestCase {
 8 
 9     public TestClassTwo(String method){
10         super(method);
11     }
12 
13     public void testLongRunner(){
14         TSP tsp = new TSP();
15         assertEquals(2000,tsp.shortestPath(2000));
16     }
17 
18     public void testShortTest(){
19         TSP tsp = new TSP();
20         assertEquals(140,tsp.shortestPath(140));
21     }
22 
23     public void testAnotherShortTest(){
24         TSP tsp = new TSP();
25         assertEquals(586,tsp.shortestPath(586));
26     }
27 
28     public static Test suite(){
29         TestSuite suite = new TestSuite();
30         suite.addTest(new TestClassTwo("testShortTest"));
31         suite.addTest(new TestClassTwo("testAnotherShortTest"));
32         return suite;
33     }
34 }
 1 package com.lenovo.unit3;
 2 
 3 import junit.framework.Test;
 4 import junit.framework.TestCase;
 5 import junit.framework.TestSuite;
 6 
 7 public class TestClassComposite extends TestCase {
 8 
 9     public TestClassComposite(String method){
10         super(method);
11     }
12 
13     static public Test suite() {
14         TestSuite suite = new TestSuite();
15         suite.addTestSuite(TestClassOne.class);
16         suite.addTest(TestClassTwo.suite());
17         return suite;
18     }
19 }

 

posted @ 2022-03-10 10:22  永远的希望  阅读(226)  评论(0)    收藏  举报