简体   繁体   English

运行多个JUnit测试-如果发生一次失败,请不要重复

[英]Running multiple JUnit tests - Don't repeat if a failure happens once

I am in a project now that is using JUnit as a framework to test engineering data (ref: last question Creating a Java reporting project -- would like to use JUnit and Ant but not sure how ) 我现在在一个项目中,该项目使用JUnit作为测试工程数据的框架(参考:最后一个问题, 创建Java报告项目-想使用JUnit和Ant,但不确定如何使用

Since a picture (err a code block) tells a 1,000 words, so let me paste my loop: 由于图片(错误代码块)显示了1,000个单词,因此让我粘贴循环:

JUnitCore junit = new JUnitCore();
RunListener listener = new RunListener();
junit.addListener(listener);

[...]

for (AbstractFault fault : faultLog) {
    theFault = fault;
    Result result = junit.run(GearAndBrakeFaultLogReports.class);

    for (Failure f : result.getFailures()) {
        output.println(log.getName());
        output.println(fault.getName());
        output.println(HelperFunctions.splitCamelCase(f.getDescription()
                .getMethodName()));
        output.println(f.getMessage());
        output.println();
    }
}

As you can see, I am running the "junit.run" many times (for each fault in the log). 如您所见,我多次运行“ junit.run”(针对日志中的每个错误)。

However, if any one of my tests fires a fail() I don't want to repeat that test. 但是,如果我的任何测试触发了fail(),我都不想重复该测试。 In other words, if there are 50 faults in a log, and in fault #1 a test fails, I don't want to attempt that test in the 49 future faults I am looping through. 换句话说,如果日志中有50个故障,而在#1故障中,测试失败,那么我就不想在我要遍历的49个未来故障中尝试该测试。

Here is an example test: 这是一个示例测试:

private static boolean LeftMLGDownTooLongFound = false;
@Test
public final void testLeftMLGDownTooLong() {
if (!LeftMLGDownTooLongFound 
        && handleLDGReportFaults(false)
        && theFault.getName().equals(FaultNames.LDG_LG_DWN_TIME.toString())) {

        assertNotGreater(getPCandRecNum(), 8f, ldgFault.getLeftStrutUpTime());
        LeftMLGDownTooLongFound = true;
    }
}

Currently, do to this, I am making a static bool that is set to false at first, but switches to true after the first assertion. 目前,为此,我正在制作一个静态布尔值,该布尔值最初设置为false,但在第一个断言之后切换为true。 Not sure if this works, but its the idea. 不知道这是否可行,但它的想法。 I don't want to do this for every single test (100's of them). 我不想为每个测试(其中100个测试)都这样做。

Is there any public function, method, or way in the JUnitCore or Runner class that I can flag it so a test never runs more than once after a fail() is called? 我可以在JUnitCore或Runner类中标记任何公共函数,方法或方式,以便在调用fail()之后测试不会运行一次以上吗?

Ah, figured it out. 啊,想通了。 To do this, I need to implement a way to find the failed tests, then in the @Before area, ax out of the test. 为此,我需要实现一种方法来查找失败的测试,然后在@Before区域中将其删除。 Here is what I added. 这是我添加的内容。

@Rule public TestName name = new TestName();
@Before
public void testNonFailedOnly() {
    Assume.assumeTrue(!failedTests.contains(name.getMethodName()));
}
private static List<String> failedTests = new ArrayList<String>(256);

@Rule
public TestWatcher watchman = new TestWatcher() {
    /* (non-Javadoc)
     * @see org.junit.rules.TestWatcher#failed(java.lang.Throwable, org.junit.runner.Description)
     */
    @Override
    protected void failed(Throwable e, Description description) {
        super.failed(e, description);
        failedTests.add(description.getMethodName());
    }
};

It does add about 1.5 seconds of overhead, which sucks... but better than the alternative!! 它确实增加了约1.5秒的开销,这很糟糕...但是比其他方法要好!

Anyone have ideas on how to optimize this? 有人对如何优化它有想法吗? I believe the overhead is from the TestWatcher, don't think it from the arraylist. 我相信这些开销是来自TestWatcher,而不是来自arraylist。

I used a Java Class that every test extends . 我使用了每个测试都可以extends的Java类。 In the @Before of this class I set a boolean hasPassed = false; @Before ,我设置了一个布尔hasPassed = false; At the end of every @Test method I set this variable hasPassed = true; 在每个@Test方法的结尾,我都设置了此变量hasPassed = true; In the @AfterMethod you can then check the variable. 然后,您可以在@AfterMethod检查变量。 If your test causes an exception, it wont reach the end and the variable is still false. 如果您的测试导致异常,它将不会到达结尾,并且变量仍为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM