selvi
selvi

Reputation: 1457

How to use testng annotations in test cases?

  1. I have below defined scenario.
  2. I want to use testng annotation.
  3. I want to execute methodfortestcase1() as first.
  4. And then I want to execute testcase1() as second.
  5. And then I want to execute methodfortestcase2() as third.
  6. And then I want to execute testcase2() as fourth.
  7. I have tried with different combinations of testng annotations such as Before Suite,Beforeclass,Beforemethod and BeforeTest.
  8. But, I am not getting the correct order of the test executions.
  9. How can I use the annotation for below defined scenario?

My code will be like below:

1.methodfortestcase1()
2.testcase1()
3.methodefortestcase2()
4.testcase2()

Upvotes: 0

Views: 279

Answers (3)

Namrata Bagerwal
Namrata Bagerwal

Reputation: 1210

If you are using JUnit4 to run testcase you can use the following annotation to run your testcases:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@FixMethodOrder(MethodSorters.JVM)
@FixMethodOrder(MethodSorters.DEFAULT)

And If you are running it using JUnit3, mind that the testcases are called in alphabetical order of their names and 'test' prefix is must for naming the testcases. Many Experts say that it is always better to write independent testcases. Your testcase should not depend on each other. Your testcases should be robust enough to be orderless to test your application code in a better way. Then only you will come to know where your code is lacking!

Upvotes: 0

Darwin Allen
Darwin Allen

Reputation: 300

To get the desired result you'll likely have to use a combination of @Test(priority) and dependsOnMethods. Keep in mind that when using dependsOnMethods that if a dependency method fails, the dependents will not be run.

Upvotes: 2

kondu
kondu

Reputation: 410

Use @Test (priority ) annotation. The lower priorities will be scheduled first.

Upvotes: 3

Related Questions