0

我有一个主要在 Windows 中开发的 Java 项目。我有一些我在 TestNG 中编写的单元测试,我也运行它们......我正在使用 TestNG 版本 5.14.1

我的项目在 OS X 下编译良好。TestNG 测试也编译良好。但是当我尝试执行单元测试时,任务失败并出现以下错误:

Omair-Inams-MacBook-Pro:PNServices omairkhawaja$ ant clean testng
...

compile-and-jar:
    [mkdir] Created dir: /Users/omairkhawaja/source/my-project/common/server/PNServices/build/classes
    [javac] Compiling 131 source files to /Users/omairkhawaja/source/my-project/common/server/PNServices/build/classes
    [javac]                                                                                           
     [copy] Copying 7 files to /Users/omairkhawaja/source/my-project/common/server/PNServices/build/classes
      [jar] Building jar: /Users/omairkhawaja/source/my-project/common/server/PNServices/build/jars/PNServices.jar

compileTest:
    [mkdir] Created dir: /Users/omairkhawaja/source/my-project/common/server/PNServices/build/test/classes
    [javac] /Users/omairkhawaja/source/my-project/common/server/PNServices/buildtools/build-test.xml:28: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 14 source files to /Users/omairkhawaja/source/my-project/common/server/PNServices/build/test/classes
    [javac] /Users/omairkhawaja/source/my-project/common/server/PNServices/src/test/java/com/corp/monitoring/my-project/service/discovery/PnDiscoveryServiceTest.java:234: warning: non-varargs call of varargs method with inexact argument type for last parameter;
    [javac] cast to java.util.List<java.lang.Object> for a varargs call
    [javac] cast to java.util.List<java.lang.Object>[] for a non-varargs call and to suppress this warning
    [javac]         when(mdo.getList("ineligibleDevices")).thenReturn(ineligibleResultList, null);
    [javac]                                                                                 ^
    [javac] /Users/omairkhawaja/source/my-project/common/server/PNServices/src/test/java/com/corp/monitoring/my-project/service/discovery/PnDiscoveryServiceTest.java:252: warning: non-varargs call of varargs method with inexact argument type for last parameter;
    [javac] cast to java.util.List<java.lang.Object> for a varargs call
    [javac] cast to java.util.List<java.lang.Object>[] for a non-varargs call and to suppress this warning
    [javac]         when(mdo.getList("ineligibleDevices")).thenReturn(null, null);
    [javac]                                                                 ^
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 2 warnings
    [mkdir] Created dir: /Users/omairkhawaja/source/my-project/common/server/PNServices/build/test/jar
      [jar] Building jar: /Users/omairkhawaja/source/my-project/common/server/PNServices/build/test/jar/agent-test.jar

testng:
    [mkdir] Created dir: /Users/omairkhawaja/source/my-project/common/server/PNServices/build/testng
    [mkdir] Created dir: /Users/omairkhawaja/source/my-project/common/server/PNServices/build/coverage
   [testng] [TestNGAntTask] TESTNG PASSED @/var/folders/ft/p4_xg83109v465y_ktnrq29m0000gn/T/testng6488341970286082385 WHICH CONTAINS:
   [testng]   -usedefaultlisteners
   [testng]   false
   [testng]   -d
   [testng]   /Users/omairkhawaja/source/my-project/common/server/PNServices/build/testng
   [testng]   -groups
   [testng]   unit
   [testng]   -listener
   [testng]   org.uncommons.reportng.HTMLReporter;org.testng.reporters.JUnitXMLReporter;org.uncommons.reportng.JUnitXMLReporter
   [testng]   -suitename
   [testng]   Ant suite
   [testng]   -testname
   [testng]   Ant test
   [testng]   /Users/omairkhawaja/source/my-project/common/server/PNServices/buildtools/suite.xml
   [testng] Listening for transport dt_socket at address: 5005
   [testng] [ERROR]: No test suite found.  Nothing to run
   [testng] java.io.FileNotFoundException: test-output/testng.css (No such file or directory)
   [testng]     at java.io.FileOutputStream.open(Native Method)
   [testng]     at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
   [testng]     at java.io.FileOutputStream.<init>(FileOutputStream.java:145)
   [testng]     at org.testng.internal.Utils.writeResourceToFile(Utils.java:618)
   [testng]     at org.testng.reporters.HtmlHelper.generateStylesheet(HtmlHelper.java:25)
   [testng]     at org.testng.reporters.SuiteHTMLReporter.generateReport(SuiteHTMLReporter.java:49)
   [testng]     at org.testng.TestNG.run(TestNG.java:613)
   [testng]     at org.testng.TestNG.privateMain(TestNG.java:999)
   [testng]     at org.testng.TestNG.main(TestNG.java:936)
   [testng] ERROR WHILE WRITING TO test-output/index.html
   [testng] java.io.IOException: No such file or directory
   [testng]     at java.io.UnixFileSystem.createFileExclusively(Native Method)
   [testng]     at java.io.File.createNewFile(File.java:883)
   [testng]     at org.testng.internal.Utils.writeFile(Utils.java:168)
   [testng]     at org.testng.reporters.SuiteHTMLReporter.generateIndex(SuiteHTMLReporter.java:138)
   [testng]     at org.testng.reporters.SuiteHTMLReporter.generateReport(SuiteHTMLReporter.java:75)
   [testng]     at org.testng.TestNG.run(TestNG.java:613)
   [testng]     at org.testng.TestNG.privateMain(TestNG.java:999)
   [testng]     at org.testng.TestNG.main(TestNG.java:936)

BUILD SUCCESSFUL
Total time: 13 seconds
Omair-Inams-MacBook-Pro:PNServices omairkhawaja$ 

Omair-Inams-MacBook-Pro:PNServices omairkhawaja$ pwd
/Users/omairkhawaja/source/my-project/common/server/PNServices

Omair-Inams-MacBook-Pro:PNServices omairkhawaja$ ls -l buildtools/ | grep suite
-rw-r--r--   1 omairkhawaja  staff   858  5 Apr 15:43 suite.xml

这是我正在使用的 testng 目标

<testng outputDir="${testng.report.dir}"
                workingDir="${test.resources.dir}"
                haltOnfailure="false"
                useDefaultListeners="false"
                listeners="org.uncommons.reportng.HTMLReporter,org.testng.reporters.JUnitXMLReporter,org.uncommons.reportng.JUnitXMLReporter" dumpCommand="true"
                groups="unit" >
            <jvmarg value="-Dlog4j.configuration=file:log4j.properties" />
            <jvmarg value="-Dquest.debug=1" />
            <jvmarg value="-Xdebug"/>
            <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7007"/>
            <sysproperty key="org.uncommons.reportng.title" value="PNServices Unit Tests"/>
            <classpath>
                <fileset refid="test-dependencies.fileset"/>
                <fileset dir="${fglam.devkit.home}">
                    <include name="**/*.jar"/>
                </fileset>
                <fileset dir="${fglcore.home}">
                    <include name="**/*.jar"/>
                </fileset>
                <fileset dir="${build.jars.dir}">
                    <include name="*.jar"/>
                </fileset>
                <fileset dir="${test.resources.dir}">
                    <include name="log4j.properties"/>
                </fileset>
                <fileset dir="${build.test.dir}/jar" includes="agent-test.jar" />
            </classpath>

            <xmlfileset dir="${basedir}/buildtools" includes="suite.xml"/>
        </testng>

这是我的 suite.xml 文件

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<!--Test suite name must not be changed as it is referenced in sonar-project.properties -->
<suite name="TestSuite" verbose="1" >
  <!-- Test MUST be named TEST-xxxx as Sonar requires this file naming format to be able to parse test results-->
  <test name="TEST-Unit">
      <packages>
          <package name="com.corp.monitoring.*" />
      </packages>

      <groups>
          <run>
            <exclude name="brokenTests"  />
            <include name="unit"  />
          </run>
        </groups>
  </test>
</suite>

正如开头的命令行输出所示,我的 suite.xml 文件位于适当的目录中。我不确定为什么 TestNG 在 Windows 上成功运行但在 OS X 上运行失败。有什么想法吗?

4

1 回答 1

2

问题的发生是由于使用 maven ant 任务下载 ReportNG 依赖项,该任务还声明了对一个古老的 TestNG 版本的依赖项,该版本首先在类路径中被拾取并导致 testng ant 目标失败。

我最近在 Windows上遇到了类似的问题.. 在挣扎了一段时间后发现了问题所在.. 我试图使用 maven 依赖项 ant 任务导入 ReportNG 依赖项......它正在下载旧版本的 TestNG。 .. 正如Marcin Zajączkowski所指出的,要在 maven 中使用 reportNG,您需要排除传递的 TestNG 依赖项。. 我不知道在使用 ant maven 任务时是否有可能的同义修复。

于 2012-07-05T18:36:52.017 回答