I need to find a way to launch parameterized test from Intellij Idea in JMeter. Here (How to launch Intellij Idea's Java tests from JMeter?) I got good help with launching the tests, but I still cannot run the test. It seems, the test's not visible for jmeter. I added junit 5 and all its dependencies jars and all dependencies I have in my project to JMETER_HOME/lib, also I add a jar containing test classes to JMETER_HOME/lib.
Here's the code I use in JSR223 Sampler:
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 org.junit.platform.launcher.listeners.TestExecutionSummary
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.*;
def listener = new SummaryGeneratingListener()
def request = LauncherDiscoveryRequestBuilder.request()
.selectors(
selectClass(com.example.MyTest.class),
selectMethod(com.example.MyTest.class, "myTest", String.class)).build()
def launcher = LauncherFactory.create()
launcher.discover(request)
launcher.registerTestExecutionListeners(listener)
launcher.execute(request)
def summary = listener.getSummary()
log.info('Total tests executed: ' + summary.getTestsFoundCount())
log.info('Total tests succeeded: ' + summary.getTestsSucceededCount())
log.info('Total tests failed: ' + summary.getTestsFailedCount())
Logs I get:
2023-08-14 12:05:13,051 INFO o.a.j.s.FileServer: Default base='...\apache-jmeter-5.6.2\bin'
2023-08-14 12:05:13,056 INFO o.a.j.g.a.Load: Loading file: ...\apache-jmeter-5.6.2\bin\MyTest\Test.jmx
2023-08-14 12:05:13,056 INFO o.a.j.s.FileServer: Set new base='...\apache-jmeter-5.6.2\bin\MyTest'
2023-08-14 12:05:13,266 INFO o.a.j.s.SaveService: Testplan (JMX) version: 2.2. Testlog (JTL) version: 2.2
2023-08-14 12:05:13,281 INFO o.a.j.s.SaveService: Using SaveService properties version 5.0
2023-08-14 12:05:13,281 INFO o.a.j.s.SaveService: Using SaveService properties file encoding UTF-8
2023-08-14 12:05:13,286 INFO o.a.j.s.SaveService: Loading file: ...\apache-jmeter-5.6.2\bin\MyTest\Test.jmx
2023-08-14 12:05:14,051 INFO o.a.j.s.SampleResult: Note: Sample TimeStamps are START times
2023-08-14 12:05:14,051 INFO o.a.j.s.SampleResult: sampleresult.default.encoding is set to UTF-8
2023-08-14 12:05:14,051 INFO o.a.j.s.SampleResult: sampleresult.useNanoTime=true
2023-08-14 12:05:14,051 INFO o.a.j.s.SampleResult: sampleresult.nanoThreadSleep=5000
2023-08-14 12:05:14,537 INFO o.a.j.s.FileServer: Set new base='...\apache-jmeter-5.6.2\bin\MyTest'
2023-08-14 12:05:28,217 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2023-08-14 12:05:28,222 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2023-08-14 12:05:28,222 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2023-08-14 12:05:28,222 INFO o.a.j.e.u.CompoundVariable: Note: Function class names must contain the string: '.functions.'
2023-08-14 12:05:28,222 INFO o.a.j.e.u.CompoundVariable: Note: Function class names must not contain the string: '.gui.'
2023-08-14 12:05:28,527 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2023-08-14 12:05:28,527 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : Thread Group
2023-08-14 12:05:28,527 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group Thread Group.
2023-08-14 12:05:28,527 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2023-08-14 12:05:28,532 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false
2023-08-14 12:05:28,542 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2023-08-14 12:05:28,542 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2023-08-14 12:05:28,547 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2023-08-14 12:05:30,038 INFO c.w.a.u.ConfigurationInfo: The target execution environment is: ...
2023-08-14 12:05:30,043 INFO c.w.a.u.ConfigurationInfo: The target execution site page is: ...
2023-08-14 12:05:30,048 INFO c.w.a.u.TestListener: Test result summary for MyTest {}
2023-08-14 12:05:30,083 INFO o.a.j.p.j.s.J.J.2 sampler: Total tests executed: 0
2023-08-14 12:05:30,083 INFO o.a.j.p.j.s.J.J.2 sampler: Total tests succeeded: 0
2023-08-14 12:05:30,083 INFO o.a.j.p.j.s.J.J.2 sampler: Total tests failed: 0
2023-08-14 12:05:30,093 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1
2023-08-14 12:05:30,093 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1
2023-08-14 12:05:30,093 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
2023-08-14 12:05:30,093 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)
Looking at my crystal ball I can see that you either have not added junit-jupiter-params library to your JMeter Classpath or haven't restarted JMeter after that or both.
Given you have a function like this in your com.example.MyTest.java file which looks like:
@ParameterizedTest
@ValueSource(strings = {"parameter1", "parameter2"})
public void testWithParameter(String parameter) {
//whatever test code you want here
}
You can call it from JMeter and print the parameters to the jmeter.log file as follows:
import org.junit.platform.engine.TestExecutionResult
import org.junit.platform.launcher.TestIdentifier
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.selectMethod
def listener = new SummaryGeneratingListener() {
@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
def testName = testIdentifier.getDisplayName()
def result = testExecutionResult.getStatus().toString()
log.info('Test Case: ' + testName)
log.info('Result: ' + result)
log.info('------------------------------------------------------------')
}
}
def request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectMethod(com.example.MyTest.class, 'testWithParameter', String.class))
.build()
def launcher = LauncherFactory.create()
launcher.discover(request)
launcher.registerTestExecutionListeners(listener)
launcher.execute(request)
Demo: