Search code examples
testingintellij-ideajmeterperformance-testingjunit-jupiter

How to launch Intellij Idea's Java tests from JMeter?


I need to find a way to launch tests from Intellij Idea in JMeter. I tried to use JUnit Request but it supports junit 3 and 4, and I use junit 5 that's why it's not possible to use this sampler. Also I tried to use Java Request and JSR223 request by trying to write code that initializes my tests and triggers their execution. I used ChatGPT help with that, but unsuccessful. Could you help me with that, please? It's been several days already and I still cannot resolve the problem.

ChatGPT examples of code: //JSR223 Sampler

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import com.example.MyTest; // Import your test class

public class MyTestRunner {
    public static void main(String[] args) {
        Result result = JUnitCore.runClasses(MyTest.class); // Replace with your test class
        System.out.println("Total tests executed: " + result.getRunCount());
        System.out.println("Total tests failed: " + result.getFailureCount());
    }
}

//Java Request Sampler

import org.example.MyTest;

public class TestRunner {
    public void runTests() {
        MyTest test = new MyTest(); // Replace with your test class
        test.setup(); // Replace with your test setup method
        test.runTests(); // Replace with your test execution method
        test.tearDown(); // Replace with your test cleanup method
    }
}

// In the Java Request, create an instance of TestRunner and call runTests(): new TestRunner().runTests();


Also I put a jar of the project in lib/ext


Solution

  • If this is really what you're looking for (I mean it's possible to run everything from JMeter but you're unlikely to get good results), assuming that you have:

    1. Your test classes copied to "lib/junit" folder of your JMeter execution
    2. JUnit5 and all its dependencies copied to JMeter Classpath
    3. Any other 3rd-party libraries which are needed by your tests in JMeter Classpath
    4. junit-platform-launcher is in JMeter Classpath (if not yet)
    5. JMeter is restarted to pick everything up

    You should be able to use the following code to kick off your tests

    import org.junit.platform.launcher.Launcher
    import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder
    import org.junit.platform.launcher.core.LauncherFactory
    import org.junit.platform.launcher.listeners.SummaryGeneratingListener
    
    import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
    
    def listener = new SummaryGeneratingListener()
    def request = LauncherDiscoveryRequestBuilder.request()
            .selectors(selectClass(com.example.MyTest.class))
            .build()
    Launcher launcher = LauncherFactory.create()
    launcher.discover(request)
    launcher.registerTestExecutionListeners(listener)
    launcher.execute(request);
    log.info('Passed: ' + listener.getSummary().getTestsSucceededCount())
    log.info('Failed: ' + listener.getSummary().getTestsFailedCount())
    

    Demo:

    enter image description here

    More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?