ricb ricb - 4 months ago 23
Java Question

TestNG - sharing state between test classes

I have a TestNG suite of test classes that I am running via a testng.xml file. This works fine. All tests are run serially, so no parallel-execution hurdles. My goal now is to take state that was generated by the test methods in one test class (e.g., a customerId primary key value generated by a database insert) and pass it along to another test class, so that the test methods in that second class can take further action based on that shared state (e.g., look up the just-inserted customer using the customerId value from the first class). It's easy to share state between test methods in a single class, of course, via class member variables, but I don't see how to share it across test classes.

Answer

Use a factory to create a repository. The repository stores "interesting test state" properties.

  TestRepository tr = TestRepositoryFactory.getInstance();
  ...
  tr.store("test13.PKID", pkid.toString());

and then in your subsequent code, repeat the call to the factory and then get the value:

  String spkid = tr.get("test13.PKID");