【问题标题】:Running Junit test in loop and continuing further if one in between fail循环运行 Junit 测试,如果中间有一个失败则继续
【发布时间】:2017-10-05 07:24:01
【问题描述】:

我有五个不同的测试数据并循环运行(相同的)JUnit 测试(五次)。也就是说,每次循环迭代,都会从 JSON 文件中读取新的 Test 输入数据并运行测试,这基本上就是 AsertEquals。

我非常清楚 JUnit 参数化最适合这种情况,但有一段时间我必须坚持使用循环中的不同测试数据运行测试。

我的测试基本上是这样的:

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");
            }
        }

现在我有以下问题:

1) 如何检查 Assert.asserEquals 和 Assert.assertNotEquals 是否成功?因为它们返回 void 我不能在 if 条件下写它们。有没有其他方法可以检查?

2) 目前,如果一项测试失败,例如测试-2,然后它不会进一步运行循环并退出。而我想进一步迭代并运行进一步的测试。

我知道我的 for 循环在这种情况下可能不太好,但我想知道我是否可以用它实现我想要的。

【问题讨论】:

  • 您在测试中尝试做的太多了。单元测试应该很简单,并且只涵盖一个专门的案例场景。如果您的测试中有条件语句.. 有问题
  • 您可能想要使用参数化测试:stackoverflow.com/questions/4247933/…

标签: java loops junit assert


【解决方案1】:

您只需要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);
}

【讨论】:

  • 谢谢塔马斯。我想不出这么简单的解决方案真是太糟糕了! :-(
  • 我不得不删除 Assert.fail(failuremsg) 因为如果我写它我不能在循环中进一步迭代并运行进一步的测试......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-13
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
相关资源
最近更新 更多