1. Annotation @Test is used in both JUnit4 and TestNG but from different class:
- import org.junit.Test; // JUnit 4
- ... or
- import org.testng.annotations.Test; // TestNG
- public class MyTestClass {
- @Test
- public void aTestMethod() throws ... { ... }
- }
Feature | JUnit 4 | TestNG |
Test annotation | @Test | @Test |
Run before all tests in this suite have run | – | @BeforeSuite |
Run after all tests in this suite have run | – | @AfterSuite |
Run before the test | – | @BeforeTest |
Run after the test | – | @AfterTest |
Run before the first test method that belongs to any of these groups is invoked | – | @BeforeGroups |
Run after the last test method that belongs to any of these groups is invoked | – | @AfterGroups |
Run before the first test method in the current class is invoked | @BeforeClass | @BeforeClass |
Run after all the test methods in the current class have been run | @AfterClass | @AfterClass |
Run before each test method | @Before | @BeforeMethod |
Run after each test method | @After | @AfterMethod |
Ignore test | @ignore | @Test(enbale=false) |
Expected exception | @Test(expected = ArithmeticException.class) | @Test(expectedExceptions = ArithmeticException.class) |
Timeout | @Test(timeout = 1000) | @Test(timeout = 1000) |
It says what exception will throw from the unit test.
//JUnit4
- public void divisionByZeroException() {
- int i = 1/0;
- }
- public void divisionByZeroException() {
- int i = 1/0;
- }
It says how to ignore the unit test.
//JUnit
- @Ignore("Not Ready to Run")
- @Test
- public void IgnoreTest() {
- }
- @Test(enabled=false)
- public void IgnoreTest() {
- }
It says how to bundle a few unit test and run together.
//JUnit4
The "@RunWith" and "@Suite" are use to run the suite test. The below class means both unit test "JunitTest1" and "JunitTest2" run together after "JunitTest3" executed. All the declaration is define inside the class.
- @RunWith(Suite.class)
- @Suite.SuiteClasses({
- JunitTest1.class,
- JunitTest2.class
- })
- public class JunitTest3 {
- }
XML file is use to run the suite test. The below XML file means both unit test “TestNGTest1” and “TestNGTest2” will run it together.
- <suite name="My test suite">
- <test name="My test">
- <classes>
- <class name="example.test.TestNGTest1" />
- <class name="example.test.TestNGTest2" />
- </classes>
- </test>
- </suite>
It says which test will execute after what test. If the dependent method fails, then all subsequent tests will be skipped, not marked as failed.
//JUnit4
JUnit framework is focus on test isolation; it did not support this feature at the moment.
//TestNG
It use "dependOnMethods" to implement the dependency testing as following
- @Test
- public void testMethod1() {
- }
- @Test(dependsOnMethods={"testMethod1"})
- public void testMethod2() {
- }
It says how to pass parameters to an unit test dynamically.
//JUnit4
The "@RunWith" and "@Parameter" is use to provide parameter value for unit test, @Parameters have to return List[], and the parameter will pass into class constructor as argument.
- @RunWith(value = Parameterized.class)
- public class JunitTest {
- private int number;
- public JunitTest(int number) {
- this.number = number;
- }
- @Parameters
- }
- @Test
- public void pushTest() {
- }
- }
//TestNG
XML file or “@DataProvider” is used to provide vary parameter for testing.
(see the post: Parameterized Test TestNG and observe the difference)