I'm trying to correlate test executions to test results. Method name, even qualified with parameters is not good enough because I may reuse the same inputs multiple times. I have looked for a thread ID in the ITestResults, but I see none. Is there a way to get the test's thread ID from the TestListenerAdapter?
In my example below, assuming I generated hundreds of inputs,
including cow and crow, my TestAnalysis object might notice both started
with "c" and alert me to that. However, in order to do that I need to
know if a given test passed or failed. I don't want to have in the end
of every test a "this test passed" when there is an obvious and easy
single place to do that, TestListenerAdapter. However, the only data I can get is the method name (from the stack). I need something better to positively ID the test than the name. For example, if I randomly generated 10 animals, I'd see the same method 10 times. Even with parameters, I might generate the same animal twice. Are there any suggestions on how to get this information? Thanks,
- JCD
public class MyTestCases {
public static final HashMap<Thread, TestAnalysis> test = new HashMap<Thread, TestAnalysis>();
public static synchronized TestAnalysis getActiveThread() {
if(!test.containsKey(Thread.currentThread())) test.put(Thread.currentThread(), new TestAnalysis());
return test.get(Thread.currentThread());
}
public static synchronized TestAnalysis getThreadById(long id) {
//TODO: Get Thread by Id
}
@Test(enabled=true, dataProvider = "dataGenerator", dataProviderClass = MyTestCases.class)
public void testCase(String animal) { getActiveThread().addData(animal
);
if("cow".equals(animal
)) throw new Error("Mooooo");
if("crow".equals(animal
)) throw new Error("Never more!");
}
//..Create generator, etc.
//TODO Have a final test run the analysis
}
public class TestAnalysisListener extends TestListenerAdapter {
@Override
public void onTestFailure(ITestResult tr) {
MyTestCases.getActiveThread().markTestFailed();//Wrong thread for test. This will create a new instance and mark it as a failure.
MyTestCases.getThreadById(tr.getThreadIdMagic()???).markTestFailed();//No magic I see can relate the thread executed for the test to the result.
// ITestResult does not know the thread ID used for the test, so how can I find out what test did
}
//Insert all other required methods ...
}