简体   繁体   English

我应该对一种主要方法进行单元测试吗?

[英]Should I unit test a main method?

Is it worth trying to unit test a main method of a console application? 值得对控制台应用程序的主要方法进行单元测试吗?

The main method is the only code that doesn't have test coverage right now. 主要方法是目前唯一没有测试覆盖率的代码。

public static void main(String[] args) {
    if (args.length != 2) {
        System.out.println("Pass the root directory and the directory to scan.");
        System.exit(1);
    }
    Path root = Paths.get(args[0]);
    Path scan = Paths.get(args[1]);
    Demo demo = new Demo();
    String output = demo.scan(root, scan);
    System.out.println(output);
}

What are you testing, really? 真的在测试什么? This is the question you have to ask yourself when deciding to test certain pieces of code. 这是在决定测试某些代码段时必须问自己的问题。

In an ideal world, having 100% code and branch coverage is nice, and having the ability to ensure that code is functional when rewritten is a huge benefit. 在理想的情况下,拥有100%的代码和分支覆盖率是很好的,并且具有在重写时确保代码可以正常工作的能力是一个巨大的好处。 However, one should never use code coverage alone as a suitable "happy" metric for test coverage. 但是, 永远不要 将代码覆盖率用作测试覆盖率的合适“满意”指标。

In this case, you have to look long and hard at what you really want to inspect here. 在这种情况下,您必须仔细检查一下您真正想要在此处检查的内容。 Given that you're newing up a lot of things, and that you'll have to mock out almost everything here, the realistic thing you can do is test the mechanical process of, "Does this object call this method with these parameters?" 鉴于您要进行很多更新,并且必须在此处模拟几乎所有内容,因此您可以执行的实际操作是测试以下过程的机械过程:“此对象是否使用这些参数调用此方法?” Frankly, that doesn't feel like a good test. 坦白说,这并不是一个好的测试。

I would recommend that you exclusively test Demo#scan instead of the entirety of main , because: 我建议您专门测试Demo#scan而不是main的全部,因为:

  • scan is a method that requires a new instance of Demo , which is not easily injected into main . scan是一种需要新的Demo实例的方法,该实例不易注入main Yes, I'm aware you can use PowerMockito to accomplish this, but mocking out something that's newed up is a test smell to me. 是的,我知道您可以使用PowerMockito来完成此操作,但是模拟出新出现的内容对我来说是一种测试气味。
  • Your application will fail to run if any of these are true: 如果满足以下任何条件,则您的应用程序将无法运行
    • args doesn't receive the right number of params args没有收到正确数量的参数
    • The path passed in through args is invalid or malformed 通过args传递的路径无效或格式错误
  • If you did go down the path of testing this, you'd start to realize that you're really testing a lot of library code and ensuring that the library code worked. 如果您确实进行了测试,那么您将开始意识到自己确实在测试很多库代码,并确保库代码可以正常工作。 That has some advantages, but in this case, the juice isn't worth the squeeze. 这有一些优势,但是在这种情况下,榨汁是不值得的。

A main method is still code which can contain bugs and causing undesired application behavior. main方法仍然是代码,其中可能包含错误并导致不良的应用程序行为。 So, testing a main method can make sense in the same way testing every other method does. 因此,测试一种main方法可以像测试其他所有方法一样有意义。

Unfortunately, a main method is a static method which makes unit testing without additional frameworks not possible. 不幸的是, main方法是static方法,该方法无法在没有其他框架的情况下进行单元测试。 At best your main method should contain one method call then you don't have to worry if you should test it or not. 充其量,您的主方法应该包含一个方法调用,那么您不必担心是否应该对其进行测试。

Anyway, in my opinion the following tests can be derived from your main method. 无论如何,我认为以下测试可以从您的main方法中得出。

  • Does the program terminates if the path and/or scan directory is not provided? 如果未提供路径和/或扫描目录,程序会终止吗?
  • Is the result printed to the console? 结果打印到控制台了吗?
  • etc. 等等

Sure, it's comprehensible to say that the test results are very obvious and I it needs just one look at the code to say it works or the program will fail. 当然,可以说测试结果非常明显,我只需要看一下代码即可知道它可以工作,否则程序将失败。 On the other hand what if of other developers which are not that well skilled change your code or some requirement changes and you will have additional arguments to process. 另一方面,如果其他不那么熟练的开发人员会更改您的代码或某些需求更改,那么您将需要处理其他参数。 Would it be still that easy? 会那么容易吗?

And finally, in a pure TDD process you already had defined similar test cases which have to be covered before you have written your main method at all. 最后,在纯TDD过程中,您已经定义了类似的测试用例,在您编写main方法之前必须先进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM