简体   繁体   中英

Can't run my test from only one class using testNG, maven, NetBeans

I created maven java project with pom.xml Then, created two TestNG cases called 'Test1.java' and 'Test2.java' in 'test' package. Also added 'NewTestNGSuite.xml' 'NewTestNGSuite.xml' Test1.java contains:

package tests;

import static org.testng.Assert.*;
import org.testng.annotations.Test;

public class Test1 {

    public Test1() {
    }
    @Test
     public void test_01() {
         System.out.println("\nFIRST!");
     }
     @Test
     public void test_02() {
         System.out.println("\nSECOND!");
     }
}

Test2.java contains:

    package tests;

    import static org.testng.Assert.*;
    import org.testng.annotations.Test;

    public class Test2 {

        public Test2() {
        }
        @Test
         public void test_03() {
             System.out.println("\nTHIRD!");
         }
         @Test
         public void test_04() {
             System.out.println("\nFOURTH!");
         }
    }

NewTestNGSuite.xml contains:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="tests" verbose="1">
   <test name="Test1" parallel="false">
        <classes>
                <class name="tests.Test1"/>
        </classes>
    </test>
</suite>

When I right click on NewTestNGSuite.xml and then 'Test File' it runs all my tests from Test1.java and from Test2.java

Results

What should I do to run tests only from Test1.java using NewTestNGSuite.xml?

pom.xml:

<?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.mycompany</groupId>
    <artifactId>testNG</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
      <dependencies>
            <dependency>
                   <groupId>org.testng</groupId>
                   <artifactId>testng</artifactId>
                   <version>6.8</version>
                   <type>jar</type>
            </dependency>
      </dependencies>
</project>

Try to add the wanted xml file to the pom.xml under surefire plugin:

    <build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <pluginManagement>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.0</version>
    <configuration>
      <suiteXmlFiles>
     <suiteXmlFile>NewTestNGSuite.xml</suiteXmlFile>
      </suiteXmlFiles>
    </configuration>
  </plugin>
        </plugins>
    </pluginManagement>
</build>

Or just add surefire plugin and choose your xml file with maven command:

mvn -Dsurefire.suiteXmlFiles=NewTestNGSuite.xml test

I guess you can customize each maven command from NetBeans too.

Ideally, it has to execute only Test1 class, as according to defined class.

Can you please update Testng dependency version to 6.14.3 I have seen you are using older version.

Make sure to maven clean and Test it.

try to retain only this in your XML and try again.

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
    <test name="Test1">
        <classes>
            <class name="tests.Test1"/>
        </classes>
    </test>
</suite>

verbose and parallel deceleration are not mandatory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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