我有个maven项目。是否有方法从TestNg Xml文件中读取pom文件的属性?例如,我想从pom文件中读取应用程序的版本,然后使用@ TestNG注释将其传递给我的测试。
到目前为止,我已经尝试将pom属性直接作为TestNG xml文件中的值传递,但是它没有从pom中获取值。相反,它打印pom属性。
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name ="Implementing Parametrization">
<test name ="Testing Functionality">
<parameter name = "browser" value = "${project.version}" />
<parameter name = "username" value = "test@gmail.com" />
<parameter name = "password" value = "abc@xyz123" />
<classes>
<class
name="it.org.seleniumtests.Parametrization.GenericHomePage"/>
</classes>
</test>
</suite>打印测试中的值:预期结果: 1.2.0和实际结果:${project.version}
我知道我可以在JVM参数中这样做,就像我在这里解释的那样:https://rationaleemotions.wordpress.com/2017/09/29/dynamic-parameterization-in-testng/,但这不是我想要实现的。我已经在pom文件中得到了我需要的值。我希望在我的TestNG xml文件中获取它,这样我就可以将它作为参数传递给我的测试。
发布于 2019-08-02 04:05:42
注意:利用这种方法的将剥夺您直接在IDE中运行testng套件xml文件的能力(而不会从IDE中调用与maven相关的目标)。
是的,您可以这样做,如下所示:
您需要利用maven资源插件并对其启用筛选,以便Maven开始用实际值替换变量。
将以下内容添加到<build>标记中
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>如下所示(请注意,我们并不是指来自src/test/resources的testng套件xml文件,因为这将包含带有占位符的套件文件,但我们需要包含由maven替换的实际值的套件文件,该文件在文件夹中可用,以${project.build.testOutputDirectory}值表示)。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
<executions>
<execution>
<phase>test</phase>
</execution>
</executions>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${project.build.testOutputDirectory}/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>下面是testng套件xml文件
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Implementing Parametrization">
<test name="Testing Functionality">
<parameter name="version" value="${project.version}"/>
<classes>
<class
name="com.rationaleemotions.IgetInputFromMavenResources"/>
</classes>
</test>
</suite>这是考试课
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class IgetInputFromMavenResources {
@Test
@Parameters("version")
public void testMethod(String projectVersion) {
System.err.println("Project version " + projectVersion);
}
}这是执行输出
09:27 $ mvn clean resources:testResources test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.rationaleemotions:playground >------------------
[INFO] Building playground 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ playground ---
[INFO] Deleting /Users/krmahadevan/githome/PlayGround/playground/target
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-cli) @ playground ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ playground ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/krmahadevan/githome/PlayGround/playground/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ playground ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/krmahadevan/githome/PlayGround/playground/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ playground ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ playground ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 8 source files to /Users/krmahadevan/githome/PlayGround/playground/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M1:test (default-test) @ playground ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
Project version 1.0-SNAPSHOT
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.324 s - in TestSuite
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.441 s
[INFO] Finished at: 2019-08-02T09:28:15+05:30
[INFO] ------------------------------------------------------------------------PS:您所调用的博客是我的:)
https://stackoverflow.com/questions/57301927
复制相似问题