从testng.xml文件中的其他路径运行测试类 [英] run test classes from other path in testng.xml file

查看:71
本文介绍了从testng.xml文件中的其他路径运行测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是TestNG,Maven&IntelliJ.我创建了一个新的maven项目,并在src/main/java下定义了一个"runtest"类.如果我在src/main/java下指定测试类,则可以通过testng.xml运行测试用例.

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.

但是,我想在src/test/java中指定我的测试类.如果我在src/test/java下指定任何测试类'test1.java'并运行,它将给出错误:错误:在类路径中找不到类:java/test1

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. 在testng.xml文件中,提供测试用例的完整路径-从/Users/开始(注意:我正在使用MAC)
  2. 在testng.xml文件中,提供测试用例的完整路径-从src/
  3. 开始
  4. 在maven-surefire-plugin下的pom.xml中指定了其他类路径(在这里我还指定了完整路径,项目基本目录的路径等)
  5. 我尝试清除缓存,但是没有区别

其他观察结果:

当我在testng.xml文件中指定路径时,Intellij显示给我一个错误.它为我提供了创建类XXXX的选项.当我单击创建类时-它给了我2个位置(src/main/java和src/test/java).我已经在src/test/java下有各自的测试类.为什么不接受?

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?

我尝试在Mac中检查我的类路径,但没有取得太大的成功.

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

任何帮助将不胜感激.

谢谢

根据评论,编辑我的问题.指定我的代码详细信息.

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

我想从TestNG类调用testng.xml文件.这是我的TestNG课程:src/main/java中的runtests.java

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

公共静态TestNG测试;

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());
    }

    }

我要这样做的原因是,我要创建Maven构建,并且要指定对用户可编辑的testng.xml.因此,如果某人只想运行1个测试用例,则可以编辑testng.xml并仅运行1个测试用例.

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.

这是我在config/testng.xml中的testng.xml

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

套房名称="Testing" verbose ="0"
测试名称="Test1"

类名="java/test1"

测试

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

在这里的班级名称中,我无法从其他位置添加班级.我只能指定src/main/java中提到的类.我想指定src/test/java中提到的类.

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.

我想做的另一件事是:我想创建一个配置文件,该文件将与build一起使用并且是可编辑的.因此,如果用户要编辑配置(即更改服务器名称等),则可以编辑配置文件并运行测试用例.

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.

推荐答案

首先,您应该找到单元测试,其名称应为 * Test.java Test * .java TestCase * .java 放入Maven src/test/java 中的相应文件夹中.此外,永远不要在Maven项目中定义完整路径,无论您使用的是Mac/Linux/Windows Box还是其他类型都没关系.

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

此外,您无需在maven-surefire-plugin的配置中定义补充类路径,因为如果将上述示例中的单元测试放置在正确的位置,则无需任何配置即可工作.

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.

如果您想使用TestNG,则可以使用如下范围测试将您的依赖项简单地放入pom文件中:

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>

除此之外,您不需要任何 testng.xml 文件或任何其他配置文件即可通过Maven,IntelliJ或Eclipse运行单元测试.您只需使用以下命令行(我建议您在进入IDE之前先进行测试)来运行测试:

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

您也不需要配置maven-surefire-plugin,这将立即可用.

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

这篇关于从testng.xml文件中的其他路径运行测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆