Java 如何避免'@重复测试注释在JUnit';中每次前后运行;?
我对JUnit5有以下问题。 我想运行一个测试15次,所以我使用了注释Java 如何避免'@重复测试注释在JUnit';中每次前后运行;?,java,testing,junit,annotations,automated-tests,Java,Testing,Junit,Annotations,Automated Tests,我对JUnit5有以下问题。 我想运行一个测试15次,所以我使用了注释@RepeatedTest(15),它成功了。但问题是,在每次运行中,它都会在每个方法之前调用,在每个方法之后调用。 它对所有15个循环都这样做,但是它应该只在第一次运行之前调用@beforeach,在最后一次运行之后调用@AfterEach。我想我不能使用@之前和@之后,因为我有多个测试,所以这只能在测试1和测试50之前调用 目前的运行情况: 它应该如何运行: 好的,我找到了解决办法! 我不知道为什么,但是对于每个测试类,
@RepeatedTest(15)
,它成功了。但问题是,在每次运行中,它都会在每个方法之前调用,在每个
方法之后调用。
它对所有15个循环都这样做,但是它应该只在第一次运行之前调用@beforeach
,在最后一次运行之后调用@AfterEach
。我想我不能使用@之前
和@之后
,因为我有多个测试,所以这只能在测试1和测试50之前调用
目前的运行情况:
它应该如何运行:
好的,我找到了解决办法!
我不知道为什么,但是对于每个测试类,调用的是之前的@和之后的@
。
因此,我删除了@beforeach
和@AfterEach
方法,并将它们的代码移动到@BeforeAll
和@aftereall
现在他们是这样运行的:
我得到了一个.jtl文件的预期输出,每个测试用例上有15次运行的数据。好的,我找到了解决方案!
我不知道为什么,但是对于每个测试类,调用的是之前的@和之后的@
。
因此,我删除了@beforeach
和@AfterEach
方法,并将它们的代码移动到@BeforeAll
和@aftereall
现在他们是这样运行的:
我得到了一个.jtl文件的预期输出,每个测试用例上有15次运行的数据。我在这里看到四个选项:
为每个场景创建一个新的测试类,每个测试类使用一个@RepeatedTest
方法,并使用@BeforeAll
和@AfterAll
进行测量
不要使用@RepeatedTest
,而是在正常的@测试中进行重复和测量
让JUnit在需要决定执行的地方注入RepetitionInfo
:
import org.junit.jupiter.api.AfterEach;
导入org.junit.jupiter.api.beforeach;
导入org.junit.jupiter.api.DisplayName;
导入org.junit.jupiter.api.RepeatedTest;
导入org.junit.jupiter.api.RepetitionInfo;
导入org.junit.jupiter.api.TestInfo;
课堂重复测试{
@之前
无效设置(RepetitionInfo RepetitionInfo,TestInfo TestInfo){
如果(repetitionInfo.getCurrentRepetition()==1){
System.out.println(“在重复“+testInfo.getDisplayName()”之前);
}
}
@RepeatedTest(value=3,name=“{displayName}{TotalRepeations}的{CurrentRepeation}”)@displayName(“test1”)
void test1(TestInfo TestInfo){
System.out.println(testInfo.getDisplayName());
}
@RepeatedTest(value=5,name=“{displayName}{TotalRepeations}的{CurrentRepeation}”)@displayName(“test2”)
void test2(TestInfo TestInfo){
System.out.println(testInfo.getDisplayName());
}
@之后
无效拆卸(RepetitionInfo RepetitionInfo,TestInfo TestInfo){
if(repetitionInfo.getCurrentRepetition()==repetitionInfo.getTotalRepetitions()){
System.out.println(“在重复“+testInfo.getDisplayName()”之后);
}
}
}
输出:
Before repetition of test1_1of3
test1_1of3
test1_2of3
test1_3of3
After repetition of test1_3of3
Before repetition of test2_1of5
test2_1of5
test2_2of5
test2_3of5
test2_4of5
test2_5of5
After repetition of test2_5of5
如果有很多这样的测试,可以考虑实现一个小型JUnit扩展。可能会引入一些新的方法注释
我在这里看到四种选择:
为每个场景创建一个新的测试类,每个测试类使用一个@RepeatedTest
方法,并使用@BeforeAll
和@AfterAll
进行测量
不要使用@RepeatedTest
,而是在正常的@测试中进行重复和测量
让JUnit在需要决定执行的地方注入RepetitionInfo
:
import org.junit.jupiter.api.AfterEach;
导入org.junit.jupiter.api.beforeach;
导入org.junit.jupiter.api.DisplayName;
导入org.junit.jupiter.api.RepeatedTest;
导入org.junit.jupiter.api.RepetitionInfo;
导入org.junit.jupiter.api.TestInfo;
课堂重复测试{
@之前
无效设置(RepetitionInfo RepetitionInfo,TestInfo TestInfo){
如果(repetitionInfo.getCurrentRepetition()==1){
System.out.println(“在重复“+testInfo.getDisplayName()”之前);
}
}
@RepeatedTest(value=3,name=“{displayName}{TotalRepeations}的{CurrentRepeation}”)@displayName(“test1”)
void test1(TestInfo TestInfo){
System.out.println(testInfo.getDisplayName());
}
@RepeatedTest(value=5,name=“{displayName}{TotalRepeations}的{CurrentRepeation}”)@displayName(“test2”)
void test2(TestInfo TestInfo){
System.out.println(testInfo.getDisplayName());
}
@之后
无效拆卸(RepetitionInfo RepetitionInfo,TestInfo TestInfo){
if(repetitionInfo.getCurrentRepetition()==repetitionInfo.getTotalRepetitions()){
System.out.println(“在重复“+testInfo.getDisplayName()”之后);
}
}
}
输出:
Before repetition of test1_1of3
test1_1of3
test1_2of3
test1_3of3
After repetition of test1_3of3
Before repetition of test2_1of5
test2_1of5
test2_2of5
test2_3of5
test2_4of5
test2_5of5
After repetition of test2_5of5
如果有很多这样的测试,可以考虑实现一个小型JUnit扩展。可能会引入一些新的方法注释
显然,您对@RepeatedTest的想法与测试框架的实现不符。也许你可以对具体的用例做更多的阐述。这样做可能更容易提出替代建议。这些测试用于性能测试。beforeach
方法创建一个.jtl文件。在测试运行期间,进行了一些测量。测量数据被写入一个数组,保存有关开始、停止、经过时间等的信息。测试完成后,每次调用@AfterEach
。在这种方法中,我循环遍历数组并将数据写入.jtl文件,最后关闭它。问题是,使用@RepeatedTests(15)
它创建了15个文件,因为它调用了15次@
@BeforeAll Method
Test 1:
- Run1
- Run2
@AfterAll Method
@BeforeAll Method
Test 2:
- Run1
- Run2
@AfterAll Method
Before repetition of test1_1of3
test1_1of3
test1_2of3
test1_3of3
After repetition of test1_3of3
Before repetition of test2_1of5
test2_1of5
test2_2of5
test2_3of5
test2_4of5
test2_5of5
After repetition of test2_5of5