run test classes from other path in testng.xml file

自古美人都是妖i 提交于 2021-02-18 19:31:34

问题


I am fairly beginner in TestNG, Maven & IntelliJ. I have created a new maven project and defined a 'runtest' class under src/main/java. I am able to run testcases through testng.xml if I specify test classes under src/main/java.

However, I want to specify my test classes inside src/test/java. If I specify any test class 'test1.java' under src/test/java and run, it gives error : Error: Cannot find class in classpath: java/test1

Workarounds I already tried:

  1. In testng.xml file, giving full path of the test case - starting from /Users/ (Note: I am using MAC)
  2. In testng.xml file, giving full path of the test case - starting from src/
  3. Specified additional classpath in pom.xml under maven-surefire-plugin (over here also I specified full path, path from project base directory etc.)
  4. I tried to clear cache, however no difference

Other observations:

Intellij shows me an error when I specify path in testng.xml file. It gives me options to create class XXXX. When I click on create class - it gives me 2 locations (src/main/java & src/test/java). I already have respective test class under src/test/java. Why is it not accepting?

I tried to check my classpath in Mac, but I'm not having much success.

Any help will be appreciated.

Thanks

Based on comments, editing my questions. Specifying my code details.

I want to call testng.xml file from TestNG class. Here is my TestNG Class: runtests.java in src/main/java

public static TestNG testng;

public static void main(String[] args) {

    try {

        testng = new TestNG();
        testng.setPreserveOrder(true);
        testng.setVerbose(0);
        testng.setTestSuites(Arrays.asList("config/testng.xml"));
        testng.run();
        }
     catch (Exception e) {
        //Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }

    }

The reason I want to do this way is, I want to create maven build and I want to specify testng.xml editable to user. So, if someone wants to run just 1 test case, they can edit testng.xml and run only 1 test case.

Here is my testng.xml in config/testng.xml

suite name="Testing" verbose="0"
test name="Test1"
classes
class name="java/test1"
classes
test

over here in the class name, I am not able to add classes from other location. I am only able to specify classes mentioned in src/main/java. I want to specify classes mentioned in src/test/java.

One more thing, I want to do is: I want to create one configuration file, that will go with build and will be editable. So, if user wants to edit configutation (i.e. change server name etc.) user can edit configuration file and run test cases.


回答1:


First you should locate your unit tests which should be named like *Test.java, Test*.java or TestCase*.java into the appropriate folder which is in Maven src/test/java. Furhtermore you should never define full path within a Maven project it does not matter if you are on Mac/Linux/Windows Box or whatever.

So as an Example the folder layout looks like this:

.
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── soebes
    │               └── training
    │                   └── maven
    │                       └── simple
    │                           └── BitMask.java
    └── test
        ├── java
        │   └── com
        │       └── soebes
        │           └── training
        │               └── maven
        │                   └── simple
        │                       └── BitMaskTest.java
        └── resources
            └── log4j.properties

Furthermore you should not need to define supplemental classpath in maven-surefire-plugin's configuration cause if you locate the unit tests as in the example above into the correct location it will works without any supplemtal configuration.

If you like to use TestNG you can simply put a dependency in your pom file with scope test like this:

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.8.8</version>
  <scope>test</scope>
</dependency>

Apart from that you don't need any testng.xml file or any other configuration file to run your unit tests by Maven nor by IntelliJ nor by Eclipse. You simply run the tests by using the following command line (which i recommend to test first before going intot your IDE):

mvn clean package

You also don't need to configure maven-surefire-plugin this will work out of the box.



来源:https://stackoverflow.com/questions/24298447/run-test-classes-from-other-path-in-testng-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!