【问题标题】:Is there a way to pass POM parameters/attributes to TestNG XML file?有没有办法将 POM 参数/属性传递给 TestNG XML 文件?
【发布时间】:2019-12-09 15:10:36
【问题描述】:

我有一个 Maven 项目。有没有办法从 TestNg Xml 文件中读取 pom 文件的属性,例如我想从 pom 文件中读取应用程序的版本,然后使用 @Parameter 注释从 TestNG xml 文件将其传递给我的测试。

到目前为止,我已经尝试将 pom 属性直接作为值传递到 TestNG xml 文件中,但它没有从 pom.xml 中获取值。相反,它会打印 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 文件中获取它,以便我可以将它作为参数传递给我的测试。

【问题讨论】:

    标签: maven testng pom.xml testng.xml


    【解决方案1】:

    注意:利用这种方法将剥夺您直接从 IDE 中运行 testng 套件 xml 文件的能力(无需从 IDE 中调用与 maven 相关的目标)

    是的,你可以这样做:

    阅读更多 herethis stackoverflow 答案。

    您需要利用 maven 资源插件并对其启用过滤,以便 Maven 开始用实际值替换变量。

    将以下内容添加到您的 &lt;build&gt; 标记中

    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
        <filtering>true</filtering>
      </testResource>
    </testResources>
    

    这是你的surefire插件条目的样子(请注意,我们不是指来自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:你点出来的博客是我的:)

    【讨论】:

    • 是的,我很清楚这是您的博客 :) 我很高兴有像您这样的人善意地帮助社区。我们需要更多像你这样的人。并感谢您的解决方案。它确实有效,但我不想剥夺我直接从 IDE 中运行我的 testng 套件 xml 文件的能力。我实际上找到了解决方法。
    • 抱歉,当字符限制达到时分成两部分:DI 创建了我自己的 ITestListener,它从正确的 pom 文件中读取值(因为我有与共享测试相关联的项目),它创建一个动态参数并分配测试的值,都在运行时。我无法实现的一点是在从侦听器内部的 pom 中获取值之后,我无法设置/覆盖参数的值(如果你可以分享这个,那就太好了)。
    猜你喜欢
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 2017-06-20
    • 2022-08-20
    • 1970-01-01
    • 2021-03-05
    相关资源
    最近更新 更多