【问题标题】:JMeter Maven Plugin not able to detect JUnit Tests from the fat Snapshot Jar createdJMeter Maven 插件无法从创建的胖快照 Jar 中检测到 JUnit 测试
【发布时间】:2021-07-12 16:49:34
【问题描述】:

我想使用 JMeter Maven 插件 运行 JUnit 测试以进行性能测试。我正在目标文件夹中创建我的项目快照 jar。此快照 jar 包含使用 maven-assembly-plugin 的测试类和外部 jar。相同的快照 jar 能够从 JMeter 应用程序中检测到 JUnit 测试方法并成功运行,但当我将其放入 <project>/src/test/jmeter/lib/junit 时,无法从插件中检测到相同的测试方法。

我将 JMX 文件放在 <project>/src/test/jmeter 中,并将快照 jar 放在此文件夹 <project>/src/test/jmeter/lib/junit 中。

请在下面找到我的 pom 文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>com.lazerycode.jmeter</groupId>
                <artifactId>jmeter-maven-plugin</artifactId>
                <version>3.4.0</version>
                <executions>
                    <!-- Generate JMeter configuration -->
                    <execution>
                        <id>configuration</id>
                        <goals>
                            <goal>configure</goal>
                        </goals>
                    </execution>
                    <!-- Run JMeter tests -->
                    <execution>
                        <id>jmeter-tests</id>
                        <goals>
                            <goal>jmeter</goal>
                        </goals>
                    </execution>
                    <!-- Fail build on errors in test -->
                    <execution>
                        <id>jmeter-check-results</id>
                        <goals>
                            <goal>results</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <generateReports>true</generateReports>
                    <jmeterExtensions>
                        <artifact>kg.apc:jmeter-plugins:pom:1.4.0</artifact>
                        <artifact>kg.apc:jmeter-plugins-json:2.7</artifact>
                    </jmeterExtensions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <appendAssemblyId>false</appendAssemblyId>
                    <archive>
                        <manifest>
                            <mainClass>MainClass.TestMethod</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>maven-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
</project>

我能够从 JMeter 应用程序运行相同的测试,但无法从 mvn clean verify 命令检测到相同的类?

运行mvn clean verify后生成的日志如下。它说JUnitSampler: ClassNotFoundException:: MyClass.TestMethod

2021-07-12 11:18:38,748 WARN o.a.j.p.j.s.JUnitSampler: ClassNotFoundException:: MyClass.TestMethod
2021-07-12 11:18:38,750 INFO o.a.j.s.SampleResult: Note: Sample TimeStamps are START times
2021-07-12 11:18:38,750 INFO o.a.j.s.SampleResult: sampleresult.default.encoding is set to ISO-8859-1
2021-07-12 11:18:38,750 INFO o.a.j.s.SampleResult: sampleresult.useNanoTime=true
2021-07-12 11:18:38,750 INFO o.a.j.s.SampleResult: sampleresult.nanoThreadSleep=5000
2021-07-12 11:18:38,751 INFO o.a.j.t.JMeterThread: Thread is done: ThreadGroup 1-1
2021-07-12 11:18:38,751 INFO o.a.j.t.JMeterThread: Thread finished: ThreadGroup 1-1
2021-07-12 11:18:40,745 INFO o.a.j.t.JMeterThread: Thread started: ThreadGroup 1-2
2021-07-12 11:18:40,745 WARN o.a.j.p.j.s.JUnitSampler: ClassNotFoundException:: MyClass.TestMethod
2021-07-12 11:18:40,746 INFO o.a.j.t.JMeterThread: Thread is done: ThreadGroup 1-2
2021-07-12 11:18:40,746 INFO o.a.j.t.JMeterThread: Thread finished: ThreadGroup 1-2
2021-07-12 11:18:42,746 INFO o.a.j.t.JMeterThread: Thread started: ThreadGroup 1-3
2021-07-12 11:18:42,746 WARN o.a.j.p.j.s.JUnitSampler: ClassNotFoundException:: MyClass.TestMethod
2021-07-12 11:18:42,746 INFO o.a.j.t.JMeterThread: Thread is done: ThreadGroup 1-3
2021-07-12 11:18:42,747 INFO o.a.j.t.JMeterThread: Thread finished: ThreadGroup 1-3
2021-07-12 11:18:44,748 INFO o.a.j.t.JMeterThread: Thread started: ThreadGroup 1-4
2021-07-12 11:18:44,749 WARN o.a.j.p.j.s.JUnitSampler: ClassNotFoundException:: MyClass.TestMethod

【问题讨论】:

    标签: jmeter performance-testing blazemeter jmeter-maven-plugin


    【解决方案1】:

    我认为您需要在 JUnit 测试中添加一个指向 .jar 文件的 custom local Maven repository

    <repositories>
        <repository>
            <id>your-junit-jar</id>
            <name>your junit repo</name>
            <url>file:/path/to/your-junit-jar-with-dependencies.jar</url>
        </repository>
    </repositories>
    

    然后您应该能够使用&lt;junitLibraries&gt; tag 让 JMeter 知道这个包含 JUnit 测试的额外库,例如:

    <junitLibraries>
        <artifact>com.yourcompany.yourgroup:your-artifact:1.0-SNAPSHOT</artifact>
    </junitLibraries>
    

    更多信息:How to Use the JMeter Maven Plugin

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多