java 我可以让 JUnit 更详细吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/180398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:18:55  来源:igfitidea点击:

Can I make JUnit more verbose?

javajunit

提问by Allain Lalonde

I'd like to have it yell hooray whenever an assert statement succeeds, or at the very least have it display the number of successful assert statements that were encountered.

我想让它在断言语句成功时大喊万岁,或者至少让它显示遇到的成功断言语句的数量。

I'm using JUnit4.

我正在使用 JUnit4。

Any suggestions?

有什么建议?

采纳答案by Dov Wasserman

If you want to see some output for each successful assertion, another simple approach which requires no external dependencies or source code, would be to define your own Assert class which delegates all methods to the standard JUnit Assert class, as well as logging successful assertions (failed assertions will be reported as usual by the JUnit class).

如果您想查看每个成功断言的输出,另一种不需要外部依赖项或源代码的简单方法是定义您自己的 Assert 类,该类将所有方法委托给标准 JUnit Assert 类,并记录成功的断言( JUnit 类将照常报告失败的断言)。

You then run a global search-and-replace on your test classes from "org.junit.Assert" => "com.myco.test.Assert", which should fix-up all regular and static import statements.

然后,您对来自“org.junit.Assert”=>“com.myco.test.Assert”的测试类运行全局搜索和替换,这应该修复所有常规和静态导入语句。

You could also then easily migrate your approach to the quieter-is-better-camp and change the wrapper class to just report the total # of passed assertions per test or per class, etc.

然后,您还可以轻松地将您的方法迁移到更安静的营地,并将包装类更改为仅报告每个测试或每个类等通过的断言总数。

回答by rzrelyea

Adding some info that would have been helpful to me when I wanted JUnit to be more verbose and stumbled on this question. Maybe it will help other testers in the future.

添加一些信息,当我希望 JUnit 更详细并偶然发现这个问题时,这些信息会对我有所帮助。也许它会在未来帮助其他测试人员。

If you are running JUnit from Ant, and want to see what tests are being run, you can add the following to your task:

如果您正在从 Ant 运行 JUnit,并想查看正在运行的测试,您可以将以下内容添加到您的任务中:

<junit showoutput="true" printsummary="on" enabletestlistenerevents="true" fork="@{fork}" forkmode="once" haltonfailure="no" timeout="1800000">

Note that showoutput, printsummary, and enabletestlistenerevents are what helped, not the other task attributes. If you set these, you'll get output like:

请注意,showoutput、printsummary 和 enabletestlistenerevents 是有帮助的,而不是其他任务属性。如果你设置这些,你会得到如下输出:

Running com.foo.bar.MyTest
junit.framework.TestListener: tests to run: 2
junit.framework.TestListener: startTest(myTestOne)
junit.framework.TestListener: endTest(myTestOne)
junit.framework.TestListener: startTest(myTestTwo)
junit.framework.TestListener: endTest(myTestTwo)
Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.495 sec

This was useful to me when my tests were timing out and I wasn't sure which tests were actually taking too long, and which tests got cancelled because they were unlucky enough to be running when the time was up.

当我的测试超时并且我不确定哪些测试实际上花费了太长时间时,这对我很有用,哪些测试被取消,因为它们很不幸在时间到了时运行。

回答by talg

You can use AOP (with Spring or AspectJ) define pointcuts on all assert methods in junit.framework.Assert class. Using spring you can implement your own class as after returning advice (http://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-advice-after-returning)which will only be called if the assert method passed (otherwise it throws an exception: junit.framework.AssertionFailedError ). In you own class you can implement a simple counter and print it at the end.

您可以使用 AOP(使用 Spring 或 AspectJ)在 junit.framework.Assert 类中的所有断言方法上定义切入点。使用 spring,您可以在返回建议后实现自己的类(http://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-advice-after-returning),它只会被调用如果 assert 方法通过(否则它会抛出异常: junit.framework.AssertionFailedError )。在您自己的课程中,您可以实现一个简单的计数器并在最后打印它。

回答by Greg Cottman

Are you really interested in an assertion that succeeds? Normally, the only interesting assertions are ones that fail.

你真的对成功的断言感兴趣吗?通常,唯一有趣的断言是失败的断言。

Being a fervent JUnit devotee myself, I try and make the output of the tests as quiet as possible because it improves the signal-to-noise ratio when something doesn't pass. The best test run is one where everything passes and there's not a peep from stdout.

作为一个狂热的 JUnit 拥护者,我尝试使测试的输出尽可能安静,因为它在某些事情没有通过时提高了信噪比。最好的测试运行是一切都通过并且没有来自标准输出的窥视。

You could always work on your unit test until it succeeds and run "grep Assert test.java | wc -l". :-)

您可以一直进行单元测试,直到它成功并运行“grep Assert test.java | wc -l”。:-)

回答by akuhn

Hard to be done. All assert methods are static members of the class Assert, which implies that the RunNotifier (which counts the successful and failed tests) is not within reach.

很难做到。所有断言方法都是类 Assert 的静态成员,这意味着 RunNotifier(计算成功和失败的测试)不在范围内。

If you dont refrain from an ungly hack: take the sources from JUnit, patch them to store the current notifier in a static field of Assert when running tests such that the static methods can report successful asserts to this notifier.

如果您不避免进行一次不明智的 hack:从 JUnit 获取源代码,在运行测试时修补它们以将当前通知程序存储在 Assert 的静态字段中,以便静态方法可以向此通知程序报告成功的断言。

回答by guerda

I don't think, it's the goal of JUnit to count matched assertions or print out more verbose information. If tests are atomic, you'll get most information in there. So I would review my tests.

我不认为,JUnit 的目标是计算匹配的断言或打印出更详细的信息。如果测试是原子的,您将在那里获得大部分信息。所以我会回顾我的测试。

You're also able to establish a LogFile in JUnit. It's possible, but it will decrease test execution performance...

您还可以在 JUnit 中建立一个 LogFile。这是可能的,但它会降低测试执行性能......

回答by Mark Bessey

I'm pretty sure you can create a custom TestRunner that does that. We ended up with something similar in our homemade Unit-testing framework (a clone of NUnit).

我很确定你可以创建一个自定义的 TestRunner 来做到这一点。我们最终在我们自制的单元测试框架(NUnit 的克隆)中得到了类似的结果。

Oh, wait - now that I'm reading your question again, if you really want output for each successful assertion, you'll have to dig into the plumbing more. The TestRunner only gets called once for each testcase start/end, so it'll count passed and failed tests, not assertions.

哦,等等 - 现在我正在再次阅读你的问题,如果你真的想要每个成功断言的输出,你将不得不深入研究管道。TestRunner 只在每个测试用例开始/结束时被调用一次,所以它会计算通过和失败的测试,而不是断言。

This isn't much of a problem for me, since I tend towards one assertion per test, generally.

这对我来说不是什么大问题,因为通常我倾向于每个测试一个断言。

回答by anjanb

Can you consider ?

可以考虑吗?

1) download junit source
2) to modify the class org.junit.Assert to do whatever modifications you're looking for

1) 下载 junit 源
2) 修改类 org.junit.Assert 以进行您正在寻找的任何修改

回答by fly.floh

junit's javadoc unfortunately says that only failed assertions are recorded (http://junit.sourceforge.net/javadoc_40/index.html)

不幸的是,junit 的 javadoc 说只记录失败的断言(http://junit.sourceforge.net/javadoc_40/index.html

so it seems it would not be possible

所以这似乎是不可能的