有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么TestNG看到的是我的包而不是我的类?

运行时(Windows 7命令行):

C:\rest-app\src\main\java\com\mycompany\app\Test>;java组织。testng。TestNG TestNG。xml

我得到:

===============================================

套房1 运行的测试总数:0,失败:0,跳过:0

===============================================

这是我测试的时候。xml文件看起来像:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite1" verbose="1" >
<test name="SandBoxTests"   >
    <packages>
        <package name="com.mycompany.app.Test" />
    </packages>
</test>
</suite>

我有三个测试在我的类在该软件包

当我改变我的测试时。要创建的xml文件:

<suite name="Suite1" verbose="1" >
<test name="SandBoxTests"   >
    <classes>
        <class name="com.mycompany.app.Test.SandBoxTests">
            <methods>
                <include name="com.mycompany.app.Test.SandBoxTests.TestA"/>
            </methods>
        </class>
    </classes>
 </test>
</suite>

我得到的答复是:

[TestNG][错误] 在类路径:com中找不到类。我的公司。应用程序。测验沙箱试验

我的类路径如下所示:

C:\rest-app\lib\*;C:\rest-app\src\main\java\com\mycompany\app\Test\*

第一个目录指向包含的所有目录。jar文件,包括testng和最后一个目录,包括我试图从中运行测试的类文件的位置。我的测试。xml文件也在那里

那么,TestNG怎么会发现我的包很好,但却看不到我的类呢

我的代码是:

package com.mycompany.app.Test;

import com.mycompany.app.RestMethods;
import com.mycompany.app.Test.TestObjects.AutoCompleteList;
import com.mycompany.app.Test.TestObjects.AutocompleteItem;
import com.mycompany.app.Test.TestObjects.Title;
import com.thoughtworks.xstream.XStream;
import org.apache.http.HttpResponse;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static org.junit.Assert.assertTrue;


public class SandBoxTests

{ ...

@BeforeClass
public void SetupSandBoxTests() throws Exception
{

}

@Test
public void TestA() throws Exception
{
    ...
}

...
}

编辑:我更新了我的类路径,以包含包含我的TestNG测试的编译源文件的位置,并且还包括包含我的TestNG的路径的根。xml文件,但没有任何更改。我补充说:

C:\rest-app\out\production\Main\*;C:\rest-app\out\production\Main\com\homeaway\app\Test\*

编辑10/25:Maven现在可以正确地构建和执行测试,但我仍然无法从命令行执行testng。为了让Maven运行我的测试,我不得不使用

<includes>
    <include>**/*.java</include>
</includes>

而不是:

<suiteXmlFiles>
    <suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>

共 (4) 个答案

  1. # 1 楼答案

    我认为你的类路径是错误的。包含*的类路径项与类文件不匹配。你可以参考this来了解一些情况。 如果您的类位于C:\rest-app\out\production\Main\com\homeaway\app\Test\,那么 com\urco\app\Test是包名的一部分,因此我认为您应该在类路径中只放置C:\rest-app\out\production\Main\

    希望对你有帮助

  2. # 2 楼答案

    你试过了吗

    <suite name="Suite1" verbose="1" >
    <test name="SandBoxTests"   >
        <classes>
            <class name="com.mycompany.app.Test.SandBoxTests">
                <methods>
                    <include name="TestA"/>
                </methods>
            </class>
        </classes>
     </test>
    </suite>
    

    我认为在方法下,它不是在搜索整个类路径。我没有试过这个。但这应该行得通

  3. # 3 楼答案

    类路径应该指向包的起始位置,而不是java文件本身

    移动你的testng。将xml转换为src/main/java并使用以下类路径:

    C:\rest-app\lib*;C:\rest-app\src\main\java\

    编辑: 我意识到您实际上是在尝试直接运行测试套件。这需要在已编译的测试类上完成-您的类路径应该指向二进制文件

    但是,如果您使用默认设置,这一切都由maven为您处理:

    src/main/java/ <- your java code
    src/test/java/ <- your test code where test-classes are named *Test.java
    

    在你的pom里。xml:

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

    见:http://maven.apache.org/plugins/maven-surefire-plugin/examples/testng.html

  4. # 4 楼答案

    您的项目可能无法编译源文件。您是否使用maven执行测试