-1

I have a test method as below that two other test methods are dependent to this method and this method should run before these two each time and not only once for both.

 @Test(dataProvider = "requestParameterProvider", groups = "jsonRequest")
 public void saveNewActivity_correctValues(Service service, 
 Map<String, Object> requestMap){}

 @Test(dependsOnMethods = "saveNewActivity_trackRequest_correctValues", dataProvider = "responseParameterProvider")
public void commitActivity_correctValues(Service service){}

@Test(dependsOnMethods = "saveNewActivity_trackRequest_correctValues", dataProvider = "exceptionParameterProvider")
public void failActivity_correctValues(Service service, FailureReason failureReason){}

what happens at above case is saveNewActivity_correctValues method run once first and then two other method run after that. but i want first method to be invoked two times before each dependent method and once as separate test. i can't put first method as @BeforeMethod because it is already a test and have a provider of it's own.

Mina Kh
  • 127
  • 1
  • 12
  • just write a method, and add a call to it at the start of those tests – Stultuske Feb 17 '20 at 13:10
  • i wanted to look for a standard way first for this problem because i want to have a data provider and with simple method i can't have that like that but thanks. – Mina Kh Feb 17 '20 at 13:24
  • Does this answer your question? https://stackoverflow.com/questions/20295578/difference-between-before-beforeclass-beforeeach-and-beforeall – Michele Dorigatti Feb 17 '20 at 16:10
  • no these annotations don't take a dataprovider and won't work as a separate test method so it doesn't help. thanks – Mina Kh Feb 18 '20 at 05:45

1 Answers1

2

Use the @Before annotation on the method you want to run before all tests.

If you don't want it to run before all methods, but only some, either refactor your tests out into 2 classes and use @Before in one, and not in the other and move your methods appropriately.

The other option is to just call the method(s) from each test you want them to run before.

I'm assuming you are using JUnit - so see here:

http://junit.sourceforge.net/javadoc/org/junit/Before.html

If not, update your post with what you are using.

mikeb
  • 10,578
  • 7
  • 62
  • 120
  • as i mentioned i want to have a data provider for the first test and run it as test also but i can't have data provider with @Before and can't run it as separate test . and i have junit and testng both. thanks – Mina Kh Feb 17 '20 at 13:22