Reputation: 1529
I have five diffrent test data and run the (same) JUnit test in loop (five times). That is, with each loop iteration, new Test input data will be read from JSON files and the test will be run, which basically does AsertEquals.
I very well know that JUnit parameterization suits this scenario best but for a moment I have to stick to running test with different test data in loop.
My test basically looks like this:
for (int i = 0; i < tests.length; i++) {
int test = tests[i];
logger.info("---------------------------------------------------------------------------------------------------------------------------------------------------" +
"--------------------------------------------------------------------------------------------------------------------------------------------------------");
logger.info("Executing Test: " + tests[i]);
logger.info("Json file for Test " + tests[i] + " is " + file);
logger.info("---------------------------------------------------------------------------------------------------------------------------------------------------" +
"--------------------------------------------------------------------------------------------------------------------------------------------------------");
FileReader fr = new FileReader(file);
kparams = readKernelParameters.readJsonFile(fr);
setUpMatrices();
setUpKernel();
setUpPSM();
if (!setUpFailure) {
logTestConfiguration();
if (logPsmOutput) {
File testFile = getPSMFileOutput();
write(testFile, Matrix.transpose(velocity));
}
if (testsToBeRun[i] == 5) {
Assert.assertNotEquals("Running PSM Check " + tests[i] + ": ", 0f, (double) Matrix.diff(vOut, velocity, quality)[6], 1.0f);
//here I want to check if above Assert.assertNotEquals was successful, if yes //then I would like to write in log file
} else {
Assert.assertEquals("Running PSM Check " + tests[i] + ": ", 0f, (double) Matrix.diff(vOut, velocity, quality)[6], 1.0f);
//same here
}
} else {
Log.error("Failure in test setup");
Assert.fail("Failure in test setup");
}
}
Now I have following questions:
1) How do I check if Assert.asserEquals and Assert.assertNotEquals was successful ? since they return void I can not write them in if condition. is there any other way I can check it ?
2) Currently, what happens is, if one test fails for e.g. Test -2, then it does not run the loop further and exits. Whereas I would like to iterate further and run further tests.
I know my for loop for this scenario may not be good, but I am wondering if I can achieve what I want to with it.
Upvotes: 4
Views: 4018
Reputation: 7164
All you need is a try-catch
:
String failureMsg = "";
for (int i = 0; i < tests.length; i++) {
try {
// your test logic comes here
} catch (AssertionError assertFaild) {
// log error or signal it somehow, e.g.:
failureMsg = failureMsg + assertFaild.getMessage();
}
}
if (!failureMsg.isEmpty()) {
// you might want to collect more data
Assert.fail(failureMsg);
}
Upvotes: 3