Single Post

Header

Sunday, November 1, 2015

JUnit TestSuite Example in Selenium Java

Examples with JUnit TestSuite in Selenium

To execute particular classes as a test suite

@RunWith(Suite.class)
@SuiteClasses ({
FirstClass.class,
SecondClass.class
})

@Rule  
ErrorCollector  -- It is built in class in Junit used to collect errors and make the testcase fail if we use assertions.

1.MyJunitTestSuite.java

package junit;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses ({
FirstClass.class,
SecondClass.class
})
public class MyJunitTestSuite {


2. FirstClass.java
package junit;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class FirstClass {
@BeforeClass
public static void testBeforeClass() {
System.out.println("Before Class");
}
@AfterClass
public static void testAfterClass() {
System.out.println("After Class");
}
@Before
public void testBeforeMethod() {
System.out.println("Before Method");
}
@After
public void testAfterMethod() {
System.out.println("After Method");
}
@Test
public void test1() {
System.out.println("test1");
}
@Test
public void test2() {
System.out.println("test2");
}
}

3. SecondClass.java
package junit;

import org.junit.Ignore;
import org.junit.Test;

public class SecondClass {
@Test
public void test1() {
System.out.println("SecondClass - test1");
}
@Ignore
@Test
public void test2() {
System.out.println("Ignore method");
}
@Test
public void test3() {
System.out.println("SecondClass - test3");
}
}

Execute MyJunitTestSuite.java file

Output:
Before Class
Before Method
test1
After Method
Before Method
test2
After Method
After Class
SecondClass - test1
SecondClass - test3


No comments:

Post a Comment