gpt4 book ai didi

java - 每个参数调用的 Junit 参数化测试

转载 作者:行者123 更新时间:2023-12-01 13:15:31 25 4
gpt4 key购买 nike

在 junit 中,每个测试都应该独立运行。对于更复杂的过程,您通常运行设置方法。但我需要类似设置方法的东西,它在每个参数之前运行。

假设我有一个这样的测试用例:

setup()
contructor()
test1()
test2()
test3()
teardown()

从执行 i junit 参数化测试来看,这将最终像这样有 2 个参数:

setUpBeforeClass
contrcutor called
test1 running
contrcutor called
test2 running
contrcutor called
test3 running
contrcutor called
test1 running
contrcutor called
test2 running
contrcutor called
test3 running
tearDownAfterClass

我需要的是在每个参数之前调用的东西。所以结果会是这样的(用“()”改变方法):

setUpBeforeClass
contrcutor called
setupParam()
test1 running
contrcutor called
test2 running
contrcutor called
test3 running
contrcutor called
tearDownParam()
setupParam()
test1 running
contrcutor called
test2 running
contrcutor called
test3 running
tearDownParam()
tearDownAfterClass

我知道 junit 测试应该是原子的,但是每个参数的设置过程非常昂贵。有没有办法实现这种执行顺序?

提前致谢!

更新:

第一个答案中的 @Before 只会在每次测试之前调用。示例:

setUpBeforeClass
contrcutor called
before
test1 running
contrcutor called
before
test2 running
contrcutor called
before
test3 running
contrcutor called
before
test1 running
contrcutor called
before
test2 running
contrcutor called
before
test3 running
tearDownAfterClass

最佳答案

使用@Before注释

@Before
public void before() {
System.out.println("Before every test ");
}

关于java - 每个参数调用的 Junit 参数化测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22503395/

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