我需要找到一种方法在 JMeter 中从 Intellij Idea 启动参数化测试。在这里(如何从 JMeter 启动 Intellij Idea 的 Java 测试?)我在启动测试方面得到了很好的帮助,但我仍然无法运行测试。看来,测试对于 jmeter 来说是不可见的。我将 junit 5 及其所有依赖项 jar 以及我的项目中的所有依赖项添加到 JMETER_HOME/lib 中,还将一个包含测试类的 jar 添加到 JMETER_HOME/lib 中。
这是我在 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())
我得到的日志:
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*)
看着我的水晶球,我可以看到您要么没有将 junit-jupiter-params 库添加到您的 JMeter Classpath,要么在那之后没有重新启动 JMeter,或者两者都没有。
假设您的 com.example.MyTest.java 文件中有一个类似的函数,如下所示:
@ParameterizedTest
@ValueSource(strings = {"parameter1", "parameter2"})
public void testWithParameter(String parameter) {
//whatever test code you want here
}
您可以从 JMeter 调用它并将参数打印到 jmeter.log 文件,如下所示:
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)
演示: