Home > Software design >  Unit testing the parametrized test programmatically with out Suite
Unit testing the parametrized test programmatically with out Suite

Time:03-24

I do have some parametrized test and i want to run the tests programmatically and not with annotation processors e.g @Suite @RunWith(Suite.class) , is there a way that i can access and invoke the test classes from other test classes ?

  • Some test cases
@SpringBootTest
class Test {

    @ParameterizedTest
    @ValueSource(strings = {"first", "second"})
    public void example(String values) {
    ...
    }
    
   ...
}

CodePudding user response:

Yes you can run test classes from code like that.

JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
junit.run(Test.class);

You can read more about that here: https://www.baeldung.com/junit-tests-run-programmatically-from-java

CodePudding user response:

You can use EngineTestKit that provides support for executing a test plan for a given TestEngine and then accessing the results via a fluent API to verify the expected results.

EngineTestKit
            .engine("junit-jupiter")
            .selectors(selectClass(Test.class))
            .execute().testEvents().assertStatistics(o -> o.failed(1));
  • Related