简体   繁体   English

使用 Junit 5 在测试套件中仅运行特定方法

[英]Running only specific methods in test suite using Junit 5

I have 2 test classes (A,B), both have 3 test methods.我有 2 个测试类(A,B),都有 3 种测试方法。 I put them into a test suite and want to run only specific methods (example: only the first method in all classes).我将它们放入测试套件并只想运行特定方法(例如:仅所有类中的第一个方法)。 For this use case in junit4 I had custom runner and this was done by passing the Runner into @RunWith(CustomRunner.class).对于junit4中的这个用例,我有自定义跑步者,这是通过将跑步者传递给@RunWith(CustomRunner.class)来完成的。

I am trying to migrate my junit4 test suites into junit5 test suites.我正在尝试将我的 junit4 测试套件迁移到 junit5 测试套件中。 I have created a test suite:我创建了一个测试套件:

import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@SelectClasses({com.example.junit5testexample.A.class,
com.example.junit5testexample.B.class})
@Suite
@SuiteDisplayName("junit5 suite")
public class JUnit5TestSuiteExample {

}

How can I now filter the methods?我现在如何过滤方法? Should I use Extensions and @ExtendWith?我应该使用 Extensions 和 @ExtendWith 吗? If so, which extension would you recommend?如果是这样,您会推荐哪个扩展?

Tried with providing custom class implementing ExecutionCondition however this does not work and I am not sure why.尝试提供自定义 class 实施 ExecutionCondition 但这不起作用,我不知道为什么。 Am I missing something or is the whole approach wrong?我错过了什么还是整个方法都错了?

public class RunMethodCondition implements ExecutionCondition { 

    @Override 
    public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context){
        final Method testedMethod = context.getTestMethod().get();
        if(testedMethod.getName()=="Atest1"){
            return ConditionEvaluationResult.enabled("enabled");
        }
        return ConditionEvaluationResult.disabled("disabled");
    }

}

and my test suite:和我的测试套件:

@SelectClasses({com.example.junit5testexample.A.class,
com.example.junit5testexample.B.class})
@Suite
@ExtendWith(RunMethodCondition.class)
public class JUnit5TestSuiteExample {

}

From junit docs: "An ExecutionCondition is evaluated to determine if a given container or test should be executed based on the supplied ExtensionContext."来自 junit 文档:“评估 ExecutionCondition 以确定是否应根据提供的 ExtensionContext 执行给定的容器或测试。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM