將測試方法對映至 TPTP JUnit 測試

從 Java 類別中,TPTP JUnit 測試會自動識別以 'test' 為名稱開頭的方法,並將這些方法對映至「測試方法」。 例如,類別 SimpleTest 的程式碼如下:

 

package junit.samples;
import junit.framework.*;
/**
* Some simple tests.
*
*/
public class SimpleTest extends TestCase 
{
	protected int fValue1;
	protected int fValue2;

	protected void setUp() 
	{
	fValue1= 2;
	fValue2= 3;
	}

	public static Test suite() 
	{
	/*
	* the type safe way
	*
	TestSuite suite= new TestSuite();
	suite.addTest(new SimpleTest("add") 
	{
	protected void runTest() { testAdd(); }
	});

	suite.addTest(new SimpleTest("testDivideByZero") 
	{
	protected void runTest() { testDivideByZero(); }
	});

	return suite;
	*/
	
	/*
	* the dynamic way
	*/

	return new TestSuite(SimpleTest.class);
}


	public void testAdd() 
	{
		double result= fValue1 + fValue2;
		// forced failure result == 5
		assertTrue(result == 6);
	}

	public void testDivideByZero() 
	{
		int zero= 0;
		int result= 8/zero;
	}

	public void testEquals() 
	{
		assertEquals(12, 12);
		assertEquals(12L, 12L);
		assertEquals(new Long(12), new Long(12));
		assertEquals("Size", 12, 13);
		assertEquals("Capacity", 12.0, 11.99, 0.0);
	}


	public static void main (String[] args) 
	{
		junit.textui.TestRunner.run(suite());
	}
}

 

 

有三個方法的名稱開頭是 'test':testAddtestDivideByZerotestEquals。「概觀」標籤的測試方法窗格會列出這些方法。

 

 

請注意,如果使用者以測試編輯器來新增測試方法,程式碼中將加入新的方法。

此外,這個類別必須有 static "Test suite()" 方法(標準的 JUnit 慣例),才能在 TPTP 中執行 JUnit 類別。 未勾選行為勾選框時,將自動產生這個 suite() 方法及內容(當使用者以文字編輯器儲存測試時也會更新)。 勾選此勾選框時,使用者必須實作這個 suite() 方法。

如需瞭解在測試上編輯方法,請參閱 JUnit 測試編輯器主題。

相關概念
JUnit 測試編輯器