mercredi 2 décembre 2015

Access fields of a TestNG test class from a TestListenerAdapter

Background

I have the following situation:

  • My test-classes implement org.testng.ITest
  • They all have a Helper containing info on the current test environment (e.g. device-under-test)

For example:

com.company.appundertest.Helper h;

public class TestClass implements org.testng.ITest {
    private String testName;

    //Helper is initialized externally in Factory + DataProvider 
    //and passed to Constructor.
    public TestClass(com.company.appundertest.Helper hh) {

    this.h = hh;

    //constructor sets the test-name dynamically
    //to distinguish multiple parallel test runs.
    this.testName = "some dynamic test name";
    }

    @Override
    public String getTestName() {
        return this.testName;
    }

    @Test
    public void failingTest() {
        //test that fails...
    }
}

  • These test-classes are executed in parallel using Factory and parallel data-provider.
  • Upon Test Failure, I need to access variables within the Helper instance of the failing test-class. These will be used to identify the environment at the point of failure (e.g. take screenshot on failing device).

This problem essentially boils down to:

How would I access fields within the TestNG test-class?

References





Aucun commentaire:

Enregistrer un commentaire