java - @RepeatedTest 未启动

标签 java unit-testing junit5

例如,我们有 @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;
    }
}

控制台中没有错误。测试根本就不会开始。 为什么?

problem

<小时/>

更新

我在 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/

相关文章:

java - java反序列化时如何动态创建序列化类的对象而不调用构造函数?

java - 使用 Mockito 测试抽象类

java - 在第一次测试失败时中断测试类

junit5 - 通过 Maven 运行测试套件在 junit5 中不起作用

Java:Android 应用程序可以在虚拟设备上运行,但在真实设备上崩溃

java.sql.SQLException : Bad connection URL while accessing the database

unit-testing - 使用单元测试项目中定义的启动类时,.NET Core TestServer返回404

unit-testing - 在 Shell 脚本中测试任何协议(protocol)

java - 如何选择使用 Maven 执行哪些 JUnit5 标签

java - 想法 : "Assign statement to new local variable"?