我们应该测试控制台输出吗? [英] Should we unit test console outputs?

查看:184
本文介绍了我们应该测试控制台输出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些遗留代码,这些代码本身有一些 System.out.print 命令。
我的eCobertura插件显示此行为红色,因此我想对它们进行单元测试。

I am working with some legacy code that has some System.out.print commands in itself. My eCobertura plugin shows this lines red, so I want to unit test them.

这里我发现了一种单元测试控制台输出的方法,我认为非常有趣。

Here in stackoverflow I found a way to unit test console outputs which i thing is very interesting.

我是这样做的:

        private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();

        @Before
        public void setUpStreams() {
            System.setOut(new PrintStream(outContent));
        }

        @After
        public void cleanUpStreams() {
            System.setOut(null);
        }

        @Test
        public void out() {
            System.out.print("Some message from the system");
            assertEquals("Some message from the system", outContent.toString());
        }

到目前为止,测试变为绿色,但是当我运行代码覆盖时插件再次,我收到此消息:

So far so good, the test goes green but when I run the code coverage plugin again, I get this message:


线程Thread-0中的异常java.lang.NullPointerException
net。 sourceforge.cobertura.coveragedata.TouchCollector.applyTouchesOnProjectData(TouchCollector.java:186)
at
net.sourceforge.cobertura.coveragedata.ProjectData.saveGlobalProjectData(ProjectData.java:267)
at
net.sourceforge.cobertura.coveragedata.SaveTimer.run(SaveTimer.java:31)
at java.lang.Thread.run(Thread.java:662)

Exception in thread "Thread-0" java.lang.NullPointerException at net.sourceforge.cobertura.coveragedata.TouchCollector.applyTouchesOnProjectData(TouchCollector.java:186) at net.sourceforge.cobertura.coveragedata.ProjectData.saveGlobalProjectData(ProjectData.java:267) at net.sourceforge.cobertura.coveragedata.SaveTimer.run(SaveTimer.java:31) at java.lang.Thread.run(Thread.java:662)

我有些疑惑:


  • 尝试单元测试是否正确 System.out.print()的

  • eCobertura是否与此类测试兼容?

  • 如果eCobertura与这种类型的测试不兼容,为什么它会显示红线?

  • 我的测试中有什么问题吗?

  • 我使用的是jUnit 4.11你认为这与它有关吗?

  • 为什么eCobertura会给我这个错误?

  • Is it correct to try to unit test System.out.print()'s?
  • Is eCoberturaincompatible with this type of test?
  • If eCobertura is not compatible with this type of test, why does it show the line red?
  • Is there something wrong in my test?
  • I am using jUnit 4.11 do you think this has something to do with it?
  • why is eCobertura giving me this error?

推荐答案

尝试单元测试System.out.print()是否正确?

这在很大程度上取决于您要测试的内容。我认为没有必要测试System.out.print()本身,我希望Sun / Oracle已经做到了这一点。但是,如果您的应用程序向控制台输出重要信息,这是验证输出的唯一方法,那么您需要对其进行测试。
如果您可以通过测试相应的类来测试代码,那么您可能不需要测试输出流本身。

This highly depends on what you are trying to test. I don't think it is necessary to test System.out.print() itself, I hope Sun/Oracle has done this enough. But if your application outputs important information to the console and this is your only way to validate the output, than yes, you need to test it. If you can test your code by testing the corresponding classes, then you probably don't need to test the output stream itself.

查看数据库,例如:我不自行测试JDBC驱动程序,但我测试了加载/保存数据的所有代码/功能和数据库。

Look at databases, for example: I don't test the JDBC driver it self, but I do test all code/functionality that is part of loading/saveing data from and to the database.

红线仅表示从未执行过代码行。这可能没问题,或者可能意味着您的测试不会触及他们应该使用的代码的一部分。获得高测试覆盖率很重要,但可能并非总是需要100%的目标(想想帕累托原则)

The red line only means the code line was never executed. This can be okay or it could mean that your tests don't touch a part of your code that they should. Getting test coverage high is important, but aiming for 100% might not always be necessary (think Pareto principle)

至于你的空指针异常

您对 System.setOut(null)的调用; 将System.out设置为null,eCobertura可能会尝试写一些标准输出,现在为空。您可能需要在@Before方法中保存orignial Out Stream并在@After方法中恢复它以允许后面的代码使用StdOut

Your call to System.setOut(null); set the System.out to null and eCobertura probably tries to write something to Standard Out, which is null now. You might need to save the orignial Out Stream in your @Before Method and restore it in our @After method to allow the code that follows to use StdOut

是eCobertura与此类测试不兼容?

如果eCobertura与此类测试不兼容,为什么它会显示红线?

为什么eCobertura会给我这个错误?

它可以因为eclipse和eCobertura之间的通信是通过Standard Out发生的,但我不确定。如果是这种情况,那么如果你重定向标准输出,不仅你的输出,而且Cobertura的输出被重定向,GUI不再看到什么被执行,什么没有,因此着色为红色

It could be that the communication between eclipse and eCobertura happens via Standard Out, but I am not sure. If this is the case, than if you redirect Standard Out not only your output but also the output from Cobertura gets redirected and the GUI does not see anymore what gets executed and what not, thus coloring it red

我的测试中有什么问题吗?

可能需要确保正确恢复StdOut。

It might be necessary to ensure StdOut is correctly restored.

我使用的是jUnit 4.11您认为这与它有关吗?

不,我不这么认为

这篇关于我们应该测试控制台输出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆