JMeter 5可以和JUnit 5一起使用吗?

92vpleto  于 2023-06-06  发布在  其他
关注(0)|答案(2)|浏览(115)

我在尝试使用jmeter-maven-plugin版本3运行JUnit 5测试套件时遇到了问题。测试套件的设置和拆卸阶段根本不会被调用。但是,使用@Test注解的方法将被调用。
我的设置是这样的:
1.第一个项目(包含JUnit测试用例)
虚拟测试套件(BasiTest.java):

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

@TestInstance(Lifecycle.PER_CLASS)
public class BasicTest {

    public BasicTest() {
    }

    @Test
    public void passingTest() {
        System.out.println("I'm a simple test that passes!");
    }

    @BeforeAll
    public void setUp() {
        System.out.println("setUp");
    }

    @AfterAll
    public void tearDown() {
        System.out.println("tearDown");
    }
}

pom.xml文件包含:

<dependencies>
      <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter</artifactId>
          <version>5.6.2</version>
          <scope>test</scope>
      </dependency>
  </dependencies>

1.第二个项目(应该通过jmeter-maven-plugin运行第一个项目生成的jar)
pom.xml文件:

<build>
        <plugins>
            <plugin>
                <groupId>com.lazerycode.jmeter</groupId>
                <artifactId>jmeter-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>configuration</id>
                        <goals>
                            <goal>configure</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jmeter-tests</id>
                        <goals>
                            <goal>jmeter</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <overrideRootLogLevel>debug</overrideRootLogLevel>
                    <jmeterExtensions>
                        <artifact>org.junit.jupiter:junit-jupiter:5.6.2</artifact>
                    </jmeterExtensions>
                    <junitLibraries>
                        <artifact>com.dummy.group.id:junit4sample:0.0.1-SNAPSHOT-tests</artifact>
                    </junitLibraries>
                </configuration>
            </plugin>
        </plugins>
    </build>

JMeter测试场景(JMX文件)为:

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.2.1">
  <hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
      <stringProp name="TestPlan.comments"></stringProp>
      <boolProp name="TestPlan.functional_mode">false</boolProp>
      <boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
      <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
      <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
        <collectionProp name="Arguments.arguments"/>
      </elementProp>
      <stringProp name="TestPlan.user_define_classpath"></stringProp>
    </TestPlan>
    <hashTree>
      <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
        <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
        <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
          <boolProp name="LoopController.continue_forever">false</boolProp>
          <stringProp name="LoopController.loops">1</stringProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">10</stringProp>
        <stringProp name="ThreadGroup.ramp_time">1</stringProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
        <boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
      </ThreadGroup>
      <hashTree>
        <JUnitSampler guiclass="JUnitTestSamplerGui" testclass="JUnitSampler" testname="JUnit Request" enabled="true">
          <stringProp name="junitSampler.classname">com.dummy.group.id.BasicTest</stringProp>
          <stringProp name="junitsampler.constructorstring"></stringProp>
          <stringProp name="junitsampler.method">passingTest</stringProp>
          <stringProp name="junitsampler.pkg.filter"></stringProp>
          <stringProp name="junitsampler.success">Test successful</stringProp>
          <stringProp name="junitsampler.success.code">1000</stringProp>
          <stringProp name="junitsampler.failure">Test failed</stringProp>
          <stringProp name="junitsampler.failure.code">0001</stringProp>
          <stringProp name="junitsampler.error">An unexpected error occurred</stringProp>
          <stringProp name="junitsampler.error.code">9999</stringProp>
          <stringProp name="junitsampler.exec.setup">false</stringProp>
          <stringProp name="junitsampler.append.error">false</stringProp>
          <stringProp name="junitsampler.append.exception">false</stringProp>
          <boolProp name="junitsampler.junit4">true</boolProp>
        </JUnitSampler>

        <hashTree>
          <ResultCollector guiclass="SummaryReport" testclass="ResultCollector" testname="Summary Report" enabled="true">
            <boolProp name="ResultCollector.error_logging">false</boolProp>
            <objProp>
              <name>saveConfig</name>
              <value class="SampleSaveConfiguration">
                <time>true</time>
                <latency>true</latency>
                <timestamp>true</timestamp>
                <success>true</success>
                <label>true</label>
                <code>true</code>
                <message>true</message>
                <threadName>true</threadName>
                <dataType>true</dataType>
                <encoding>false</encoding>
                <assertions>true</assertions>
                <subresults>true</subresults>
                <responseData>false</responseData>
                <samplerData>false</samplerData>
                <xml>false</xml>
                <fieldNames>true</fieldNames>
                <responseHeaders>false</responseHeaders>
                <requestHeaders>false</requestHeaders>
                <responseDataOnError>false</responseDataOnError>
                <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
                <assertionsResultsToSave>0</assertionsResultsToSave>
                <bytes>true</bytes>
                <sentBytes>true</sentBytes>
                <url>true</url>
                <threadCounts>true</threadCounts>
                <idleTime>true</idleTime>
                <connectTime>true</connectTime>
              </value>
            </objProp>
            <stringProp name="filename"></stringProp>
          </ResultCollector>
          <hashTree/>
        </hashTree>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>

我必须提到的是,包含JUnit测试用例(第一个项目)的jar文件是由junit插件加载的,用@Test注解的方法正在运行(我可以在控制台中看到输出),但安装和拆卸阶段完全被忽略。
你知道为什么吗
非常感谢!

zpgglvta

zpgglvta1#

  1. JMeter自动调用setUptearDown函数
    1.您需要从单独的JUnit请求采样器中显式调用用@BeforeClass@AfterClass注解的函数,这同样适用于@BeforeAll@AfterAll
    1.我不认为JUnit 5是官方支持的,考虑降级到JUnit 4,否则你将独自面对你的问题
    更多信息:How to Use JUnit With JMeter
omtl5h9j

omtl5h9j2#

我认为这行不通。到今天为止……当我给JUnit5 suite类时,它给出了这个错误

java.lang.ClassCastException: class com.tejasoft.tests.ju.ju5.ut.perf.TestAllPerfSuite cannot be cast to class junit.framework.TestCase (com.tejasoft.tests.ju.ju5.ut.perf.TestAllPerfSuite and junit.framework.TestCase are in unnamed module of loader org.apache.jmeter.DynamicClassLoader @72ea2f77)
        at org.apache.jmeter.protocol.java.sampler.JUnitSampler.initializeTestObject(JUnitSampler.java:675) ~[ApacheJMeter_junit-5.4.1.jar:5.4.1]
        at org.apache.jmeter.protocol.java.sampler.JUnitSampler.threadStarted(JUnitSampler.java:647) ~[ApacheJMeter_junit-5.4.1.jar:5.4.1]
        at org.apache.jmeter.threads.JMeterThread$ThreadListenerTraverser.addNode(JMeterThread.java:784) [ApacheJMeter_core-5.4.1.jar:5.4.1]
        at org.apache.jorphan.collections.HashTree.traverseInto(HashTree.java:993) [jorphan-5.4.1.jar:5.4.1]
        at org.apache.jorphan.collections.HashTree.traverse(HashTree.java:976) [jorphan-5.4.1.jar:5.4.1]
        at org.apache.jmeter.threads.JMeterThread.threadStarted(JMeterThread.java:752) [ApacheJMeter_core-5.4.1.jar:5.4.1]
        at org.apache.jmeter.threads.JMeterThread.initRun(JMeterThread.java:740) [ApacheJMeter_core-5.4.1.jar:5.4.1]
        at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:252) [ApacheJMeter_core-5.4.1.jar:5.4.1]
        at java.lang.Thread.run(Thread.java:1589) [?:?]

相关问题