简体   繁体   中英

How to run specific method before of only specific test cases using TestNG?

In my test class, suppose I've 15 test cases. Out of 15, I require common test data for only 5 test cases. Hence I want to write a method, which will create test data, but that method will execute before any of those 5 tests are run.

I know @BeforClass - which will run before any of tests from the class is run and @BeforeMethod - which will run before every test in the class.

I do not want to use @BeforeClass to create test data for 5 test cases out of 15 because if I want to debug a test which does not belong to those 5 test cases still it will create data, which is not required for my current test, also it will increase execution time.

Is there any way with TestNG, I can run specific method before some of the tests are executed(Without using testng.xml)

TestNG only provides dependency on other test methods. which makes a method to make as a test method.

To Archive what you needed You can do This:

@Test
void testMethod(){
     //this is your test method
     beforemethod();
}

//your before method for you test-case
void beforemethod(){
}

Hope this fixes you issue

From your description I understand you need a dataProvider (which is exactly that, a method providing same data for Multiple test cases or alternatively multiple data for the same test case).

@DataProvider(name = "dataProviderFor5TestCases")
public Object[][] createData() {
 return new Object[][] {
   { "Joe", new Integer(43) },
   { "Mary", new Integer(32)},
 };
}

Then you can declare the dataProvider on your test case as such:

@Test(dataProvider = "dataProviderFor5TestCases")
public void testCase1(String name, Integer age) {
 System.out.println(name + " " + age);
}

Result will be:

Joe 43
Mary 32

So testCase1 will be executed twice with the set of data created in the dataProvider. However, I think you need the same data for all 5 test Cases (achievable).

Now, regarding execution time. I am not 100% sure but I believe data is created on demand (ie if the testCase is skipped or failed no data is created; but I had a very small load so please try it and let us know!)

Update after OP's comment: So, you are probably better off, using testGroups then which will suit you for both setup before the test and cleanup afterwards (without being invoked for irrelevant test cases):

@Test(groups = { "init" })
public void serverInit() {
startServer();
}

@Test(groups = { "init" })
public void initEnvironment() {
createUsers()
}

@Test(groups = { "cleanup"}, dependsOnGroups = { "init.*" })
public void testCase1() {
//perform your tests
}


@Test(dependsOnGroups = { "cleanup"})
puplic void cleanup(){
deleteUsers();
killServer();
}

The above testCase1 won't be executed if any of the init test method fail (ie server fails to start). In addition, cleanup method will only be invoked if the testCase1 succeeded. If you want the cleanup method to be run regardless of testCase1 result you can use alwaysRun like so:

@Test(dependsOnGroups = { "cleanup"}, alwaysRun=true)

Hope that helps! Best of luck!

Example taken from here: TestNG DataProvider

You can use dependsOnMethods in @Test() annotation

eg

@Test
public void testDataSetup() 
 {
    // Setup your testDataHere
}


@Test(dependsOnMethods = { "testDataSetup" })
public void testExecute1() 
{
 // Use Your logic here which executes after datasetup
   }

For Complete Tutorials see this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM