gpt4 book ai didi

java - 在 JUnit 中运行所有测试后调用方法

转载 作者:搜寻专家 更新时间:2023-11-01 02:26:28 25 4
gpt4 key购买 nike

我想为 JUnit 中的测试运行创建自定义 html 报告。我遇到的问题是在完成所有测试后释放资源和关闭标签。

我打开一个 FileChannel 用于写入报告。由于它应该是每个测试的行表,并且有数百个,所以我不想为每个测试打开和关闭 channel 。这里出现的问题是测试组织 - 我有嵌套套件,所以 testRunFinished 不是一个选项(指的是单个套件,而不是所有测试,我看到了 this question )。 TestWatcher 也不会帮助我,因为它仅指单个测试。

使用的工具:maven 3.0.5、ff webdriver、junit 4.11。

我在考虑两种选择:1) 在每次测试运行时打开和关闭 channel 2) 覆盖 finalize() 使其关闭 channel

它们看起来都不漂亮......我搜索了很多页面,但似乎没有人遇到与我相同的问题。

任何更漂亮的解决方案?

最佳答案

是的,请看这里 ( Before and After Suite execution hook in jUnit 4.x ):

@RunWith(Suite.class)
@SuiteClasses({Test1.class, Test2.class})
public class TestSuite {
@BeforeClass
public static void setUp() {
System.out.println("setting up");
}

@AfterClass
public static void tearDown() {
System.out.println("tearing down");
}
}

关于java - 在 JUnit 中运行所有测试后调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21773344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com