TestNG ERROR Cannot find class in classpath

2019-01-14 15:37发布

I'm trying to run testng via command line and I have following things in classpath:

  • testng jar, jar of compiled test case file, and other required jars.
  • testng.xml points to appropriate class in the hierarchy.

Testng doesn't run instead it throws:

[TestNG] [ERROR]  Cannot find class in classpath: (name of testcase file)

I encountered the same issue before and I just added.

In classpath and it solved the problem. But this time it did not seem to solve it. Any workarounds? Thanks in advance.

30条回答
我只想做你的唯一
2楼-- · 2019-01-14 16:18

make sure your suite.xml should not have .java extension

ex :

<test name="Test C1">
    <classes>
        <class name="com.taxi.suiteC.TestCase_C1" ></class>  
    </classes>
</test>
查看更多
家丑人穷心不美
3楼-- · 2019-01-14 16:19

When I converted my project with default pacage and a class named addition, to testng.xml. It got converted as:

<classes>
      <class name=".addition"/>
</classes>

and was throwing error that class cannot be loaded. If I remove . before addition, the code works fine. So be careful with default package.

查看更多
贪生不怕死
4楼-- · 2019-01-14 16:21

If your package name is FabFurnishPackage and your class name is registration then the code in xml should be

<classes>
<class name="FabFurnishPackage.registration">
</classes>

[TestNG] [ERROR] Cannot find class in classpath: registration issue is fixed...

查看更多
看我几分像从前
5楼-- · 2019-01-14 16:21

Just do Eclipse> Project > Clean and then run the test cases again. It should work.

What it does in background is, that it will call mvn eclipse:clean in your project directory which will delete your .project and .classpath files and you can also do a mvn eclipse:eclipse - this regenerates your .project and .classpath files. Thus adding the desired class in the classpath.

查看更多
干净又极端
6楼-- · 2019-01-14 16:21

I just had this problem and was because the skip test option in the run configuration was checked. made that unchecked and build, then run the testNg, worked.

查看更多
戒情不戒烟
7楼-- · 2019-01-14 16:21

I was facing the same issue,

Please check that .java & xml file was created in src folder. Created TestNG class by right clicking on src folder. Works fine!

查看更多
登录 后发表回答