0
  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()
selvi
  • 1,271
  • 2
  • 21
  • 41

3 Answers3

3

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

kondu
  • 410
  • 3
  • 11
  • I don't want to use priority for methods.I want to differentiate between test and test which belongs to the method. – selvi Mar 25 '15 at 06:02
2

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.

Darwin Allen
  • 190
  • 4
  • 15
0

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!

Namrata Bagerwal
  • 1,202
  • 13
  • 27