通过 class 和方法名称发现 JUnit5 参数化测试抛出 org.junit.platform.commons.PreconditionViolationException

Discovering JUnit5 parameterized test via class and method name throws org.junit.platform.commons.PreconditionViolationException

我有一套测试 运行 测试我公司网站上各种工具的 UI。我构建了一个系统,可以 运行 在不同的环境中进行这些测试,出于我的考虑,我编写的代码允许我 运行 在这些多个环境中进行单个测试。问题是,当我尝试通过 MethodSelector 运行 参数化测试(将 class 和方法名称作为字符串传递)时,出现以下错误:

Caused by: org.junit.platform.commons.PreconditionViolationException: Could not find method with name [TestTheThing] in class [tests.tool.fragments.TestClass].

然后我尝试传递包含参数的方法名称,但随后出现此错误:

Failed to load parameter type [String param] for method [TestTheThing] in class [tests.tool.fragments.TestClass].

我已经对此进行了一段时间的研究,但一无所获,非常感谢您的帮助。这是测试的模型:

public class TestClass {

    @ParameterizedTest
    @MethodSource("getParams")
    public void TestTheThing(String param) {
        // test code here
    }
}

这是我用来查找和 运行 测试的实际代码(来自 getAllTestSelectors 的所有选择器都是 ClassSelector 类型):

    // Runs a single tests in a fragment of a tool (used for batch-testing only)
    public void runSingleTestInFragment(String fragment, String test) {
        
        if (fragment.equals(""))
            throw new MasterTestException("Fragment name is empty");
        
        if (test.equals(""))
            throw new MasterTestException("Test name is empty");
        
        // Make sure test fragment exists
        String className = null;
        
        for (DiscoverySelector s : getAllTestSelectors()) {
            ClassSelector classSelector = (ClassSelector) s;
            String cname = classSelector.getClassName();
            String name = cname.substring(cname.lastIndexOf(".") + 1);
            
            if (name.contains(fragment)) {
                className = classSelector.getClassName();
                break;
            }
        }
        
        if (className == null)
            throw new MasterTestException("Could not find a test fragment named '" + fragment + "' for the tool " + data.getAsEnum("tool"));
        
        // Create the selector
        MethodSelector selector = DiscoverySelectors.selectMethod(className + "#" + test);
        
        // Test that the selector is valid (will throw a PreconditionViolationException if otherwise)
        try {
            selector.getJavaMethod();
            
        } catch (PreconditionViolationException e) {
            throw new MasterTestException("The test method '" + test + "' does not exist in the fragment '" + fragment + "'", e);
        }
        
        runTests(selector);
    }

类型名称不是 String param

相反,它必须是完全限定的类型名称。对于 java.lang.String.

的字符串

当通过 完全限定的方法名称 选择参数化方法时,如果 example 包中的 class,那将是 example.TestClass#TestTheThing(java.lang.String).

这在 Javadoc 中有完整的记录。