简体   繁体   English

如何从 Maven 运行 testng.xml?

[英]How to run testng.xml from Maven?

I configured my testng1.xml on the pom.xml file, but still it looks like it just runing all of the tests that has @Test annotation instead of only those who are configured in the testng1.xml file.我在 pom.xml 文件上配置了我的 testng1.xml,但它看起来仍然只是运行所有具有 @Test 注释的测试,而不是仅在 testng1.xml 文件中配置的那些。 I copied many examples from google, but nothing works.我从谷歌复制了很多例子,但没有任何效果。

please assist.请协助。

Here is my testng1.xml:这是我的 testng1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite guice-stage="DEVELOPMENT" name="GroupXdefault suite">
  <test verbose="2" name="Default test">
    <classes>
        <class name="GroupX.ArtifactX.Test2"/>
        <class name="GroupX.ArtifactX.Test3"/>
    </classes>
  </test> <!-- Default test -->
</suite> <!-- Default suite -->

Here is my POM.xml:这是我的 POM.xml:

<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>GroupX</groupId>
  <artifactId>ArtifactX</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>ArtifactX</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

<profiles>
    <profile>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20</version>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>testng1.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

  <dependencies>

    <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.11</version>
    </dependency>



  </dependencies>
</project>

You profile configuration is wrong (id is missing and the profile is not activated).您的配置文件配置错误(缺少 ID 且配置文件未激活)。 Just move the <build> section in <project> .只需移动<project><build>部分。

try to Include the testng.xml path as shown in below code尝试包含 testng.xml 路径,如下面的代码所示

</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <suiteXmlFiles>
                    <suiteXmlFile>testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <inherited>true</inherited>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM