What is the difference between @BeforeTestCase and @SetupTestCase

Thanks guys for the tool!

What is the difference between @BeforeTestCase in a Test Listener and @SetupTestCase in a Test Suite?

In which case to use one rather than the other?



I found here in which order they are called but why do we have 2 annotations for one purpose ?


Why use a tennis racket rather than a golf club?

They are executed under different circumstances and do not share a single purpose.

Understand this:

  1. A Test Suite contains one or more Test Cases.

  2. A Test Case performs your test steps against an “Application Under Test” (AUT).

You can see there is a clear difference between 1 and 2. A Test Suite contains potentially many Test Cases.

You can also see that there is a key difference between something that occurs ONCE before a Test Suite (BeforeTestSuite) and something that occurs before each Test Case (of which there may be many).

Why would you need them? You will need to wait until the solution they provide solves a problem you have encountered. Until then, they’re just solutions looking for a problem – they’re just “noise”. :slight_smile:

I use them every day. But my use of them would not pertain to you and your testing of your AUT. For example, my AfterTestCase writes a custom report used by members of my organization. My BeforeTestSuite graps a copy of the Suite ID for use elsewhere. If the Suite ID is empty, my code knows it is NOT running in a suite (which is useful to me for reasons too complex to describe here).

Good luck!

If I run a TestSuite with many testcases, then this will happen:

@BeforeTestCase will execute before each testcases
@SetupTestCase will execute before each testcases

So my question remains.

And I understand that if a user just run a test case without test suite then only @BeforeTestCase will be executed.

But in the case of a test suite, it’s redundant. It’s even more obvious when you see the screen copy in my second post.

I am not talking about @BeforeTestSuite.

Sorry. My bad. I misunderstood and didn’t read the question properly. :blush:

Yes, I recall having similar thoughts myself when I discovered Setup/TearDown. My guess is that SetupUp and TearDown are “older” and that BeforeTestCase (and Suite) came later. If that’s true, then they need to stay around for backwards compatibility reasons.

But I don’t know this is the case. Just a guess.

I’ll leave my original answer above – I enjoy laughing at myself :wink: :crazy_face:

1 Like

Thank you for the question.

@ methods in Test Suite and Test Case are meant for simple test fixtures setup.

Test Listener is meant for integration and advanced execution manipulations (example).

There is no definitive boundary, but in happy scenarios, things should be designed so that the tests still execute well with or without Test Listener. Of course, whatever you use does not really matter - public APIs are and will always be maintained.

@SetupTestCase is (potentialy) used when you want to extract test steps shared by all test cases. A common example is using WebUI.openBrowser() and WebUI.navigateToURL() inside your setup method.
If you don’t need it, just keep it skipped (default option).

On the other hand, more than one @BeforeTestCase listeners can be used for different purposes. For example, I use an “InfoTestListener” that logs some information about the test and a “ReportTestListener” that sends reports after the tests are finished. I could just merge them into one listener, but I like different functions logically divided.

1 Like

I just know about the Before Test case . Here it is ;
To execute a set-up method before any of the test methods included in the < test > tag in the testng.xml file.

Another great resource to understand:


1 Like