Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Migrating to PDT test framework based on Junit4
Revision as of 04:06, 10 June 2007 by Unnamed Poltroon (Talk) (→PDT Test Framework Cookbook (delta from Junit3.8))
PDT Test Framework Cookbook (delta from Junit3.8)
- No need to extend from
TestCase
- still you can extend fromPhpTestCase
to have Golden capabilities - use
Assert
class forassert<Type>
testing - Mark unit test as
@GUITest
or@HeadlessTest
or@BareTest
- this annotation indicates which runner to use (GUI, Headless or bare). - Mark test method as
@Test
- Mark as
@Before
methods that should be run before each test method - Mark as
@After
methods that should be run after each test method - Mark as
@BeforeClass
method that should be run before all test methods - Mark as
@AfterClass
method that should be run after all test methods - Add parameter
(expected=<ExceptionClass.class>)
to the@Test
annotation - Add parameter
(timeout=<ms>)
to the@Test
annotation - Add
@Ignore
to ignore the test - Add
@Parameter
to run unit test several times with different parameters.
Example
import org.junit.*; import junit.framework.JUnit4TestAdapter; import java.util.Vector; public class SimpleUnitTest { String value; @Before public void setUp() { value="start"; } @Test public void testValue() { Assert.assertEquals("start", value); value += " 1"; Assert.assertEquals("start 1", value); } @Ignore public void testDoNothing() { Assert.assertTrue(true); } @Test(expected=ArrayIndexOutOfBoundsException.class) public void testException() { Vector test = new Vector(); Assert.assertEquals(0, test.elementAt(42)); } @After public void tearDown() { value=""; } }