歡迎您光臨本站 註冊首頁

junit 單元測試使用方法

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

junit 是用來做單元測試,最近項目中可能要需要,所以了解了以下!說一下junit的使用!

一、安裝junit

下載junit.jar放在你工程的編譯路徑下, ok!這不用解釋

二、使用junit

使用的幾種情況:具體使用間三中的代碼註釋部分

1、測試某個類的所有方法

2、測試某個類的個別方法

3、測試幾個類中的全部方法

4、測試幾個類中的指定方法

三、 幾個已經生成的類、eclipse3.2 myeclipse4.1 j2sdk1.4.2

以下是被測試的類

package common;
  import java.io.*;
  public class RunTime
  {
  public boolean executeRunTime(int str)
  {
  /*
  * try { Process prop = Runtime.getRuntime().exec(str); // Process prop =
  * Runtime.getRuntime().exec("cmd/E:ON/C start 1.txt"); } catch
  * (IOException e) { e.printStackTrace(); }
  */
  if (str == 1)
  {
  return true;
  } else
  {
  return false;
  }
  }
  public int reValue(int a ,int b)
  {
  return a b;
  }
  }

以下是junit的測試類

package common;
  import junit.framework.AssertionFailedError;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.Assert;
  import junit.framework.TestSuite;
  public class RunTimeTest extends TestCase
  {
  public static RunTime op = null;
  public RunTimeTest(String name)
  {
  super(name);
  }
  protected void setUp() throws Exception
  {
  super.setUp();
  op = new RunTime();
  }
  protected void tearDown() throws Exception
  {
  super.tearDown();
  }
  /*
  * Test method for 'common.RunTimeTest.executeRunTime()'
  */
  public void testExecuteRunTime()
  {
  // junit.framework.TestResult r = new junit.framework.TestResult();
  try
  {
  Assert.assertEquals(true, op.executeRunTime(1));// 若失敗則拋出AssertionFailedError異常
  // throw new Exception( "This is a test.");
  // Assert.fail();
  } catch (Exception e)
  {
  System.out.println("sfsd");
  Assert.fail("fsdf");
  }
  }
  public void testreValue()
  {
  Assert.assertEquals(" i am here!",2,op.reValue(1,2));
  }
  // 可在一個單獨類中實現
  public static Test suite()
  {
  TestSuite suite = new TestSuite("ALL TEST");
  /*以下是測試某個具體方法*/
  //suite.addTest(new RunTimeTest("testreValue"));
  suite.addTest(new RunTimeTest("testExecuteRunTime"));
  /*下句是執行指定類中的所有方法*/
  //suite.addTestSuite(RunTimeTest.class);
  return suite;
  }
  public static void main(String[] args)
  {
  // 以下三種方式均可以,具體情況可運行以下,看一下結果
  junit.textui.TestRunner.run(suite());
  // junit.swingui.TestRunner.run(Test.class);
  // junit.awtui.TestRunner.run(Test.class);
  //junit.swingui.TestRunner.run(RunTimeTest.class);
  }
  }
以下是使用suite的類,可以測試多個
package common;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.framework.Test;
  import common.RunTimeTest;
  public class TestUnit
  {
  public static Test suite()
  {
  //以下是用來增加單個測試用例
  TestSuite suite = new TestSuite("ALL TEST");
  //以下這句將運行RunTimeTest中被指定的方法,如testreValue
  suite.addTest(new RunTimeTest("testreValue"));
  //以下這句將運行RunTimeTest中的所有測試方法
  //suite.addTestSuite(RunTimeTest.class);
  //以下這句講運行RunTimeTest.suite()中規定的一組方法
  //suite.addTest(RunTimeTest.suite());
  return suite;
  }
  public static void main(String[] args)
  {
  //以下三種方式均可以,具體情況可運行以下,看一下結果
  // junit.textui.TestRunner.run(TestUnit.class);
  // junit.swingui.TestRunner.run(Test.class);
  // junit.awtui.TestRunner.run(Test.class);
  // junit.swingui.TestRunner.run(TestUnit.class);
  junit.textui.TestRunner.run(suite());
  }
  }


[火星人 ] junit 單元測試使用方法已經有612次圍觀

http://coctec.com/docs/java/show-post-61162.html