Java 我怎样才能让朱尼安静下来?
当我运行JUnit测试时,失败会导致非常大的堆栈跟踪,而这些跟踪并不能提供太多信息。我希望能够看到失败的assert语句,因此每个失败最多占用2或3行,而不是20行 我没有使用ANT调用JUnit,而是从命令行以非常大的批量运行它们。额外的输出只会使解析数据变得困难Java 我怎样才能让朱尼安静下来?,java,junit,Java,Junit,当我运行JUnit测试时,失败会导致非常大的堆栈跟踪,而这些跟踪并不能提供太多信息。我希望能够看到失败的assert语句,因此每个失败最多占用2或3行,而不是20行 我没有使用ANT调用JUnit,而是从命令行以非常大的批量运行它们。额外的输出只会使解析数据变得困难 编辑:此测试用于编程课程中的学生作业,因此会有错误,并且有相当多的程序需要测试。鉴于这是一种相对罕见的情况(从命令行大量运行JUnit,而不是使用IDE或Ant之类的工具)如果JUnit本身没有解决这个问题的方法,我也不会感到惊讶
编辑:此测试用于编程课程中的学生作业,因此会有错误,并且有相当多的程序需要测试。鉴于这是一种相对罕见的情况(从命令行大量运行JUnit,而不是使用IDE或Ant之类的工具)如果JUnit本身没有解决这个问题的方法,我也不会感到惊讶
为什么不将结果写入一个文件,然后通过一个小的解析器运行它,该解析器识别JUnit故障的开始,打印接下来的几行,然后跳到下一行的开始?我想说,一旦您修复了所有JUnit测试,您的JUnit测试将会很好而且安静。留下“嘈杂”的堆栈痕迹,作为消除它们并返回绿色栏的激励 “…我没有使用ANT调用JUnit…”-为什么不?是什么让他们成批给你买的?如果使用Ant,您不仅可以得到测试,还可以得到HTML报告任务。这将使输出“安静”,在不丢弃堆栈跟踪细节的情况下很好地呈现输出 听起来您在编写的JUnit测试工具中使用提交的作业作为第三方JAR。好办法。应该是向上或向下,红色或绿色。你现在是质量保证部门-堆栈跟踪是学生的责任 你可以给他们测试类,告诉他们在运行时让他们的东西通过。把责任推到他们身上 学生应该尽早了解单元测试的价值。我会给他们提供JUnit和Ant 并让他们提供运行JUnit测试作为通过分配的先决条件 下面是一个示例Ant build.xml,您可以根据需要自由修改它。请特别注意测试任务:
<?xml version="1.0" encoding="UTF-8"?>
<project name="spring-finance" basedir="." default="package">
<property name="version" value="1.6"/>
<property name="haltonfailure" value="no"/>
<property name="out" value="out"/>
<property name="production.src" value="src/main/java"/>
<property name="production.lib" value="src/main/webapp/WEB-INF/lib"/>
<property name="production.resources" value="src/main/resources"/>
<property name="production.classes" value="${out}/production/${ant.project.name}"/>
<property name="test.src" value="src/test/java"/>
<property name="test.lib" value="src/test/lib"/>
<property name="test.resources" value="src/test/resources"/>
<property name="test.classes" value="${out}/test/${ant.project.name}"/>
<property name="exploded" value="out/exploded/${ant.project.name}"/>
<property name="exploded.classes" value="${exploded}/WEB-INF/classes"/>
<property name="exploded.lib" value="${exploded}/WEB-INF/lib"/>
<property name="reports.out" value="${out}/reports"/>
<property name="junit.out" value="${reports.out}/junit"/>
<property name="web.src" value="src/main/webapp"/>
<property name="web.lib" value="${web.src}/WEB-INF/lib"/>
<property name="web.classes" value="${web.src}/WEB-INF/classes"/>
<path id="production.class.path">
<pathelement location="${production.classes}"/>
<pathelement location="${production.resources}"/>
<fileset dir="${production.lib}">
<include name="**/*.jar"/>
<exclude name="**/junit*.jar"/>
<exclude name="**/*test*.jar"/>
</fileset>
</path>
<path id="test.class.path">
<path refid="production.class.path"/>
<pathelement location="${test.classes}"/>
<pathelement location="${test.resources}"/>
<fileset dir="${test.lib}">
<include name="**/junit*.jar"/>
<include name="**/*test*.jar"/>
</fileset>
</path>
<available file="${out}" property="outputExists"/>
<target name="clean" description="remove all generated artifacts" if="outputExists">
<delete dir="${out}" includeEmptyDirs="true"/>
</target>
<target name="create" description="create the output directories" unless="outputExists">
<mkdir dir="${production.classes}"/>
<mkdir dir="${test.classes}"/>
<mkdir dir="${junit.out}"/>
<mkdir dir="${exploded.classes}"/>
<mkdir dir="${exploded.lib}"/>
</target>
<target name="compile" description="compile all .java source files" depends="create">
<!-- Debug output
<property name="production.class.path" refid="production.class.path"/>
<echo message="${production.class.path}"/>
-->
<javac srcdir="src" destdir="${out}/production/${ant.project.name}" debug="on" source="${version}">
<classpath refid="production.class.path"/>
<include name="**/*.java"/>
<exclude name="**/*Test.java"/>
</javac>
<javac srcdir="${test.src}" destdir="${out}/test/${ant.project.name}" debug="on" source="${version}">
<classpath refid="test.class.path"/>
<include name="**/*Test.java"/>
</javac>
</target>
<target name="test" description="run all unit tests" depends="compile">
<!-- Debug output
<property name="test.class.path" refid="test.class.path"/>
<echo message="${test.class.path}"/>
-->
<junit printsummary="yes" haltonfailure="${haltonfailure}">
<classpath refid="test.class.path"/>
<formatter type="xml"/>
<batchtest fork="yes" todir="${junit.out}">
<fileset dir="${test.src}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
<junitreport todir="${junit.out}">
<fileset dir="${junit.out}">
<include name="TEST-*.xml"/>
</fileset>
<report todir="${junit.out}" format="frames"/>
</junitreport>
</target>
<target name="exploded" description="create exploded deployment" depends="test">
<copy todir="${exploded}">
<fileset dir="${web.src}"/>
</copy>
<copy todir="${exploded}/WEB-INF">
<fileset dir="${web.src}/WEB-INF"/>
</copy>
<copy todir="${exploded.classes}">
<fileset dir="${production.classes}"/>
</copy>
<copy todir="${exploded.lib}">
<fileset dir="${production.lib}"/>
</copy>
</target>
<target name="jar" description="create jar file" depends="test">
<jar destfile="${out}/${ant.project.name}.jar" basedir="${production.classes}" includes="**/*.class"/>
</target>
<target name="war" description="create war file" depends="exploded">
<war basedir="${exploded}" webxml="${exploded}/WEB-INF/web.xml" destfile="${out}/${ant.project.name}.war"/>
</target>
<target name="package" description="create package for deployment" depends="test">
<antcall target="war"/>
</target>
</project>
这似乎描述了一种过滤JUnit在从命令行运行测试时报告的内容的方法。这种方法似乎是创建一个替代入口点类(即“main”方法),该类使用一个替代RunListener来记录错误
警告:这将涉及到一些Java编码,浏览JUnit javadocs,以及(可能)查看JUnit源代码以获取想法。失败的JUnit测试用例应该是嘈杂的。我喜欢他们对我尖叫;)
如果您的单元测试用例经常出现问题和中断,尽管一切都很好(假阳性),那么请考虑更改测试用例。也可以包装JUnit测试运行程序,而不是自己运行测试用例。虽然这样做很好,但测试的代码实际上是学生提交的作业,因此它会失败。我们不使用ANT的唯一原因是我们还没有投入时间来转换它。尽管这可能需要一些调查。