testng fails to load test classes from ant

孤人 提交于 2019-12-23 10:14:13

问题


I'm trying to run TestNG tests from Ant. After reading the manual, I came up with:

<taskdef name="testng" classname="org.testng.TestNGAntTask" classpath="../../lib/testng-6.8.1.jar"/>
<target name="testng" depends="compile-tests" description="Run testng test suite">      

<fileset id="mixed.tests" dir="${bin.main}">
    <include name="**/*Test.*"/>
</fileset>

<testng mode="mixed" classfilesetref="mixed.tests" 
    verbose="5"
    failureProperty="tests.failed" outputdir="/tmp/report">
    <classpath>
        <fileset dir="../../lib" includes="*.jar"/>
        <fileset dir="../../java/bin/" includes="**/*.class"/>
    <fileset dir="/home/y/lib/jars" excludes="junit*jar,messagebus-disc-parent.jar" includes="**/*.jar" description="all jars installed by dependent pacakges" />
    </classpath>
</testng>

When I run it it fails with:

   [testng] 'org.testng.TestNG'
   [testng] '@/tmp/testng8260935716887369607'
   [testng]
   [testng] The ' characters around the executable and arguments are
   [testng] not part of the command.
   [testng] Exception in thread "main" org.testng.TestNGException:
   [testng] Cannot load class from file: /home/adamatan/SOME_PATH_THAT_EXISTS/functional/EnforceBasicFilteringTest.class
   [testng]     at org.testng.internal.ClassHelper.fileToClass(ClassHelper.java:600)
   [testng]     at org.testng.TestNG.configure(TestNG.java:1393)
   [testng]     at org.testng.TestNG.privateMain(TestNG.java:1354)
   [testng]     at org.testng.TestNG.main(TestNG.java:1333)
   [testng] The tests failed.

What I've already tried:

Files and permissions:

/home/adamatan/SOME_PATH_THAT_EXISTS/functional/EnforceBasicFilteringTest.class:

  • Appears in the classpath printout (it's very long, so I don't put it here).
  • Is created by the compile target, so it is non-empty, and ant can read its contents.
  • ls -l-ing the file gives -rw-r--r-- 1 adamatan users 4548 Nov 21 12:19

mode type:

Tried both testng mode="testng" and testng mode="mixed". Both failed with the same error.

Why can't testng read this valid class file that was just created?


回答1:


The same problem was solved for me by adding to the classpath the directory where tests themselves are located (looks like it's ${bin.main} in your case).




回答2:


Just a shot in the dark here but from what I read TestNG will be launched as a forked process (from http://testng.org/doc/ant.html):

This task runs TestNG tests and is always run in a forked JVM.

As such you might want to add the path to your code for the testng task. Also make sure your compilation of the Java code happens before the call to TestNG.

See here for an example of someone who hit the same issue:

https://groups.google.com/forum/#!topic/testng-users/Lc2Pcj7B9C4




回答3:


Some suggestions:

  1. class file created, but it is invalid and impossible to read. Can you check you can use it? For example, does javap -p $class give you list of methods, not error?

  2. One java (with higher version) created file, and another (with lower version) is trying to load it.



来源:https://stackoverflow.com/questions/20121114/testng-fails-to-load-test-classes-from-ant

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