DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

基于JUnit的测试包和测试用例的模板


测试用例 模板代码见附录 1 ,实际的测试代码写在方法 dataPortTest 中,注意这个方法不能有参数和返回值,前面用 @Test 标示,就可以用 JUnit 测了,如果这个测试用例仅仅做单个测试,不加到测试包中,可以去掉 suite 和 SyncActorTest 两个方法。 测试包 模板代码见附录 2 , addTest 的参数格式是 new TestClassName(“testMethodName”) ,一个测试类中可以有多个测试方法,一个包中可以测很多类,测试包可以很方便地选择需要测试的类和方法。 附录 1 package com.boco.godu.GAPI; import junit.framework.Assert; import junit.framework.TestCase; import junit.framework.TestSuite; import org.junit.Test; public class SyncActorTest extends TestCase { public static TestSuite suite() { TestSuite suite = new TestSuite(); suite.addTest(new SyncActorTest("dataPortTest")); return suite; } public SyncActorTest(final String name) { super(name); } @Test public void dataPortTest() { ConnFactory connFactory = new ConnFactory("10.0.2.47", 1235); connFactory.setUserName("<7001>"); connFactory.setPassword("goduapp"); Assert.assertEquals("goduapp", connFactory.getPassword()); try { IConnection conn = connFactory.getConnection(); Assert.assertEquals(IConnection.CONNECTION_INITIAL, conn.getStatus()); ISyncActor syncActor = conn.getSyncActor(); Document recXml = syncActor.sendCmd("ls id=1938695146").getXml(); // some other Assert... syncActor.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } } 附录 2 package com.boco.godu.GAPI; import junit.framework.TestCase; import junit.framework.TestSuite; public class GapiTestSuite extends TestCase { public static TestSuite suite() { TestSuite suite = new TestSuite(); suite.addTest(new SyncActorTest("dataPortTest")); suite.addTest(new SyncActorTest("dataPortTest2")); suite.addTest(new SyncActorTest2("...")); suite.addTest(...); return suite; } }



Published

Jul 22, 2010

Last Updated

Jul 22, 2010

Category

Tech

Tags

  • 测试 3
  • JUnit 10

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor