例如,我们有 @RepeatedTest
:
import org.junit.jupiter.api.RepeatedTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SomeClassTest {
@RepeatedTest(3)
public void doSmth() {
String str = "hello";
SomeClass someClass = new SomeClass();
assertEquals(str.substring(0, 2), someClass.doSmth(str));
}
}
它工作正常,直到我们尝试从 Test Suite 运行它。像这样:
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
@SelectClasses({
SomeClassTest.class
})
public class TestSuite {
}
SomeClass
public class SomeClass {
public String doSmth(String str){
return str.length() > 3? str.substring(0,2) : str;
}
}
控制台中没有错误。测试根本就不会开始。 为什么?
<小时/>更新
我在 SomeClass
中启用了日志记录。
public class SomeClass {
private final static Logger log = LoggerFactory.getLogger(SomeClass.class.getName());
public String doSmth(String str){
log.info("in doSmth");
return str.length() > 3? str.substring(0,2) : str;
}
}
现在我在日志文件中看到 @RepeatedTest
从测试套件中调用了 3 次。但之后的测试会怎样呢?为什么会掉下来?
谁能告诉我是否有可能记录 JUnit 的工作?
最佳答案
您可以使用 SomeClassTest
类中的以下 stub 记录重复的测试执行详细信息:-
private Logger log = Logger.getLogger(SomeTest.class.getName());
@BeforeEach
void beforeEach(TestInfo testInfo, RepetitionInfo repetitionInfo) {
log.info(String.format("About to execute repetition %d of %d for %s",
repetitionInfo.getCurrentRepetition(),
repetitionInfo.getTotalRepetitions(),
testInfo.getTestMethod().get().getName()));
}
此外,按照当前的逻辑,我看不出测试会失败
的原因,否则您可能会面临断言错误,而在失败情况下,您必须最终遇到该错误。
关于java - @RepeatedTest 未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46958621/