SiKing

June 18, 2015

TestNG @Before* and @After* methods

Filed under: automation — SiKing @ 2:58 pm
Tags:

I am a long time user of JUnit. Both v3 and v4. But I have finally hit the limits of what JUnit can do for me. Specifically, in case you’re interested, the showstopper was the signature of:

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

The static hurts.

Today I started looking at TestNG. It has a bunch of @Before.. and @After.. methods, like about twice as many as JUnit, and all of them just public void.

My first question was what order are they all run in? After a bit of Googling, several websites give you only a partial answer. All the examples use just one class that goes something like this:

public class FirstTest {
	@Test
	public void test1() {
		System.out.println("Running " + getClass().getSimpleName() + " / test1.");
	}
	
	@Test
	public void test2() {
		System.out.println("Running " + getClass().getSimpleName() + " / test2.");
	}

	@BeforeMethod
	public void beforeMethod() {
		System.out.println("Running " + getClass().getSimpleName() + " / beforeMethod.");
	}

	@AfterMethod
	public void afterMethod() {
		System.out.println("Running " + getClass().getSimpleName() + " / afterMethod.");
	}

	@BeforeClass
	public void beforeClass() {
		System.out.println("Running " + getClass().getSimpleName() + " / beforeClass.");
	}

	@AfterClass
	public void afterClass() {
		System.out.println("Running " + getClass().getSimpleName() + " / afterClass.");
	}

	@BeforeTest
	public void beforeTest() {
		System.out.println("Running " + getClass().getSimpleName() + " / beforeTest.");
	}

	@AfterTest
	public void afterTest() {
		System.out.println("Running " + getClass().getSimpleName() + " / afterTest.");
	}

	@BeforeSuite
	public void beforeSuite() {
		System.out.println("Running " + getClass().getSimpleName() + " / beforeSuite.");
	}

	@AfterSuite
	public void afterSuite() {
		System.out.println("Running " + getClass().getSimpleName() + " / afterSuite.");
	}
}

Things become much more apparent if you have two such classes, and you run them in one go. Running two such classes will give you something like the following:

Running FirstTest / beforeSuite.
Running SecondTest / beforeSuite.
Running FirstTest / beforeTest. ------\
Running SecondTest / beforeTest. -----+-\
Running FirstTest / beforeClass. ---\ | |
Running FirstTest / beforeMethod. -\| | |
Running FirstTest / test1.         || | |
Running FirstTest / afterMethod. --/| | |
Running FirstTest / beforeMethod. -\| | |
Running FirstTest / test2.         || | |
Running FirstTest / afterMethod. --/| | |
Running FirstTest / afterClass. ----/ | |
Running SecondTest / beforeClass. ---\| |
Running SecondTest / beforeMethod. -\|| |
Running SecondTest / test1.         ||| |
Running SecondTest / afterMethod. --/|| |
Running SecondTest / beforeMethod. -\|| |
Running SecondTest / test2.         ||| |
Running SecondTest / afterMethod. --/|| |
Running SecondTest / afterClass. ----/| |
Running FirstTest / afterTest. -------/ |
Running SecondTest / afterTest. --------/
PASSED: test1
PASSED: test2
PASSED: test1
PASSED: test2

===============================================
    net.sourceforge.sebase.demos
    Tests run: 4, Failures: 0, Skips: 0
===============================================

Running FirstTest / afterSuite.
Running SecondTest / afterSuite. 

In the above I have added the nested bowls myself to really stress the point.

The interesting (at least to me) things to note:

  • The pair of *Test and *Suite methods are run in no particular order. You can enforce an order if you need to by using one of the depends* parameters.
  • All the afterSuite methods are run after reporting is already generated.
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: