log4j - 何时在测试中使用 PropertyConfigurator?
我知道您应该只加载一次 log4j 属性,那么当您进行单元测试时,标准做法是什么?我应该将它加载到每个单元测试文件中吗?我应该把它放在 jUnit 的 setUp() 方法中吗?
谢谢
I know you're supposed to only load log4j properties once, so what is standard practice when you're doing unit tests? Should I load it in every unit test file? Should I put it in jUnit's setUp() method?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么首先要在单元测试中加载 log4j 属性?在正常情况下,单元测试不应生成日志消息。 IMO 你应该简单地关闭日志记录(也适用于正在单元测试的代码)。单元测试唯一需要的输出是 JUnit(或您最喜欢的单元测试框架)自动为您生成的摘要,其他任何东西都只是混乱。我将其添加到单元测试类中的类设置方法中:
唯一的例外是当我为尚未测试的类/模块组合新的单元测试时(我正在使用遗留代码,试图用单元测试覆盖它)逐渐),或者在分析失败的测试时。然后,我让日志消息发送到标准输出,以获取有关我调用的代码中出现问题的线索。但是,一旦单元测试(或应用程序代码)被修复,我就会再次关闭注销。
Why do you want to load log4j properties in unit tests at the first place? In the normal course of events, unit tests are not supposed to produce log messages. IMO you should simply switch off logging (also for the code being unit tested). The only required output of unit tests is the summary what JUnit (or your favourite unit test framework) produces for you automatically, anything else is just clutter. I add this to the class setup method in my unit test classes:
The only exception is when I am putting together a new unit test for an as yet untested class / module (I am working with legacy code, trying to cover it with unit tests gradually), or when analysing a test that fails. Then I let the log messages go to the standard output to get clues about what went wrong somewhere inside the code I called. But once the unit test (or the application code) is fixed, I switch logging off again.