简体   繁体   English

Junit 5 对属性测试执行测试 class

[英]Junit 5 execute test on attribute test class

Hi i got an abstract BaseTest and a second with 2 attribute implementation of this BaseTest嗨,我得到了一个抽象的 BaseTest 和第二个这个 BaseTest 的 2 属性实现

I want to call all @Test on attribute class.我想在属性 class 上调用所有 @Test。

some Code:一些代码:

public abstract class BaseTest<T> {

 public abstract T getInstance();
@BeforeEach
 public void setup(){
   this.instance = getInstance();
 }
@Test
all test methods ....

I want to create a second class with 2 implementation of this BaseTest like我想创建第二个 class 与此 BaseTest 的 2 个实现,如

class TwoImplOfBaseTest {
private BaseTest<Integer> testA;
private BaseTest<String> testB;

 @Test
 public void testAll(){
 //write something like testA.doAllTest();
     //write something like testB.doAllTest();

} }

thanks for reading谢谢阅读

I think you are over complicating it.我认为你把它复杂化了。 If i understand correctly you have BaseTest class because of common test cases between A and B. @Test, and almost all other junit annotations, is inherited from super classes, unless the method is overridden.如果我理解正确,您有 BaseTest class 因为 A 和 B 之间的常见测试用例。@Test 和几乎所有其他 junit 注释都是从超类继承的,除非该方法被覆盖。 In this case all you need is creating concrete implementations of BaseTest.在这种情况下,您只需要创建 BaseTest 的具体实现。 JUnit will initialize test class instances, run all @Test methods, etc. JUnit 将初始化测试 class 实例,运行所有 @Test 方法等。

public class TestA extends BaseTest<Integer> {
  //init stuff if needed
}

And other class等class

public class TestB extends BaseTest<String> {
  //init stuff if needed
}

And that's it, the junit runner will run all @Test methods from BaseTest for TestA and then for TestB.就是这样,junit 运行器将从 BaseTest 为 TestA 然后为 TestB 运行所有 @Test 方法。

Is your goal to run all annotated @Test methods inside multiple classes?您的目标是在多个类中运行所有带注释的 @Test 方法吗?

public void runAll() {
    LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
      .selectors(selectPackage("com.package.where.your.test.classes.are"))
      .filters(includeClassNamePatterns(".*Test"))
      .build();
    Launcher launcher = LauncherFactory.create();
    TestPlan testPlan = launcher.discover(request);
    launcher.registerTestExecutionListeners(listener);
    launcher.execute(request);
}

RunJUnit5TestsFromJava runner = new RunJUnit5TestsFromJava();
    runner.runAll();

    TestExecutionSummary summary = runner.listener.getSummary();
    summary.printTo(new PrintWriter(System.out));
}

To run a single Test class:要运行单个测试 class:

public class RunJUnit5TestsFromJava {
    SummaryGeneratingListener listener = new SummaryGeneratingListener();

    public void runOne() {
        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
          .selectors(selectClass(FirstUnitTest.class))
          .build();
        Launcher launcher = LauncherFactory.create();
        TestPlan testPlan = launcher.discover(request);
        launcher.registerTestExecutionListeners(listener);
        launcher.execute(request);
    }
    // main method...
}

Reference: https://www.baeldung.com/junit-tests-run-programmatically-from-java参考: https://www.baeldung.com/junit-tests-run-programmatically-from-java

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

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