【问题标题】:How to run specific Java class from POM.XML with reference to TestNG如何参考 TestNG 从 POM.XML 运行特定的 Java 类
【发布时间】:2019-03-03 01:51:44
【问题描述】:

我确实有 Maven + TestNG + Selenium + Java 的配置

我想从 POM.XMl 文件中运行特定的 Java 类,

试过了:

     <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>demo.java</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

据我所知,提供了通用的解决方案,但没有奏效。它没有运行任何东西,构建成功,

请注意,在使用 TestNG 时,任何 Java 类中都没有 public static void main(String args[])

如果对&lt;mainClass&gt;demo.java&lt;/mainClass&gt; 部分有解释会很棒,参考为 TESTNG 类。

到目前为止我已经尝试过:

From Maven, how do I run a class that lives under src/test/java?

How to execute a java class file from maven command line

How do I execute a program using Maven?

Maven Run Project

【问题讨论】:

    标签: java maven selenium-webdriver testng pom.xml


    【解决方案1】:

    exec-maven-plugin 需要 public static void main(String[] args) 方法。

    为了从 exec-maven-plugin 运行 TestNG 类,您必须在 main 方法中运行 TestNG。试试这个解决方案:

    public static void main(String[] args) {
        TestNG testSuite = new TestNG();
        testSuite.setTestClasses(new Class[] { MyClass.class }); //it would be probably name of the class in which `main` method is located
        testSuite.addListener(new MyListener()); //optional
        testSuite.setDefaultSuiteName("Test Suite");
        testSuite.setDefaultTestName("Test Name");
        testSuite.setOutputDirectory("/my/output/dir");
        testSuite.run();
    }
    

    【讨论】:

      【解决方案2】:

      由于使用的是 Maven+ testng。在 pom.xml 中,您可以使用 maven-surefire-plugin 运行 pom.xml 中 testng.xml.Variable TestCaseNames 中提到的特定测试用例(多个测试用例,分开)。你可以在Testng中提到你的TestCase。

      <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-surefire-plugin</artifactId>
                      <version>2.22.0</version>
                      <configuration>                     
                          <suiteXmlFiles>
                              <suiteXmlFile>testng.xml</suiteXmlFile>
                          </suiteXmlFiles>
                          <properties>
                              <property>
                                  <name>testnames</name>
                                  <value>${TestCaseNames}</value>
                              </property>
                          </properties>
                      </configuration>
                  </plugin>
      

      在TestNg中:

      <test name="TestCase1">
              <classes>
                  <class name="com.company.test">
                      <methods>
                          <include name="testCase1" />
                      </methods>
                  </class>
              </classes>
          </test>
      

      【讨论】:

        【解决方案3】:

        我记得,一旦我经历了这个,它就奏效了。

            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.0</version>
                    <configuration>
                        <includes>
                            <include>demo.java</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        

        参考:http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

        【讨论】:

          猜你喜欢
          • 2014-12-10
          • 1970-01-01
          • 2015-02-18
          • 2017-11-04
          • 1970-01-01
          • 1970-01-01
          • 2012-11-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多