0

I want to create a data-driven framework, where the test will execute based on data in excel.

Currently when I execute the tests, all results under one testName only.

What I am looking for is for every iteration should be treated as separate test.

James Z
  • 12,209
  • 10
  • 24
  • 44
Cod
  • 179
  • 1
  • 12

1 Answers1

0

It is not possible to add an annotation to an existing class at runtime; see Adding Java Annotations at Runtime. That Q&A suggests using Proxy to or an adapter to deal with this, but I don't think that approach will work for TestNG testing.

You probably need to take a different approach. Section 5.14 of the TestNG documentation has an example that shows how to run the tests in a test class programmatically:

TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { Run2.class });
testng.addListener(tla);
testng.run();

This example creates a TestNG object and runs the test class Run2. It also adds a TestListener. You can either use the adapter class org.testng.TestListenerAdapter or implement org.testng.ITestListener yourself. This interface contains various callback methods that let you keep track of when a test starts, succeeds, fails, etc...

Then Section 5.17 explains how to use a method interceptor to massage the list of methods to be called. Finally Section 5.18 explains various ways to tell TestNG to use listeners such as a method interceptor.

If you combine these with code for extracting data from your Excel spreadsheet and run the relevant test classes or test methods programmatically.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216