将测试方法映射至 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());
	}
}

 

 

有 3 个方法的名称以“test”开头:testAddtestDivideByZerotestEquals。这些方法将列示在“概述”选项卡的测试方法窗格中。

 

 

注意,如果用户使用测试编辑器来添加测试方法,则会将新方法添加到该代码中。

并且,为了能够在 TPTP 中运行 JUnit 类,此类必须包含静态“Test suite()”方法(这是 JUnit 的标准惯例)。当行为复选框处于未选中状态时,将自动生成这个 suite() 方法及其内容(并在用户使用测试编辑器保存测试时自动更新)。当该复选框处于选中状态时,用户必须实现这个 suite() 方法。

要了解有关对测试编辑方法的信息,请参阅 JUnit 测试编辑器主题。

相关概念
JUnit 测试编辑器