为什么用 JUnit 进行单元测试的类不能有 main ?
我的讲师之前提到过这一点,但我不太明白为什么会这样。有人能解释一下吗?
我们正在编写一个程序来计算素数数组列表,并且必须使用 JUnit 来确保该数组列表的所有成员都是素数。为什么我不能使用 main 来测试这个类?
非常感谢 :)
My lecturer mentioned this before, but I don't really understand why this is the case. Would anyone be able to explain ?
We are writing a program to compute an array list of prime numbers, and we have to use JUnit to ensure all members of this arraylist are prime. Why can I not use a main in testing this class ?
Thank you very much :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧,这些答案大部分都太复杂了。我认为你的问题更为根本。这是一个非常好的
答案是,当您成为一名 java 开发人员并开始编写大量随着时间的推移而更新/修复的代码时,那么拥有一个单独的测试插件将有助于从外部自动对您的代码运行测试代码来检查它是否仍然按照您期望的方式工作。这意味着您可以出于任何原因修复/调试代码的不同方面,然后您的老板走过来询问自您修复以来代码是否仍然执行客户希望它执行的操作?没有复杂性你可以回答他,不需要复杂的主内错误语句,这些错误语句与正常的程序输出混合在一起(并在非测试条件下减慢代码),但有一个漂亮的绿色 junit 条表明一切仍然有效。除非您开发大型项目并且需要进行数百个测试,否则您不会看到它的价值。此外,junit 还有许多其他技巧......
Ok these answers are for the most part too complex. I think your question is more fundamental. ANd its a very good one
The answer is when you become a java developer and start writing large amount of code that get updated/fixed over time then it helps to have a separate test plug-in that will automatically run tests on your code from outside the code to check if it’s still working in the way you would expect. This means you can fix/debug different aspect of the code for whatever reason and afterwards your boss walks over and asks does the code still do what the client wanted it to do since your fix? Without complication You can answer him without complex in-main error statements, which are mixed up with the normal program output (and slow down the code in non test conditions), but with a pretty green junit bar that says it all still works. You won’t see the value of this until you develop large projects and you have hundreds of tests to do. In addition junit has a number of other tricks up its sleeves...
因为 JUnit 提供了一个
main
来调用您在类中提供的函数。您仍然可以拥有自己的main
函数;当您运行 JUnit 时,它们只是不会被使用。您可以使用 main 函数单独测试您自己的类,但使用 JUnit 有一些优点,如 org.life.java 的答案中所述。Because JUnit is providing a
main
that calls the functions that you provide in your classes. You can still have your ownmain
functions; they just won't get used when you run JUnit. You can usemain
functions to test your own classes individually, but using JUnit has some advantages as described in org.life.java's answer.可以,只是不推荐。如果您编写一个单元测试来测试它,那么您可以使用 junit 测试运行程序来运行测试并生成一个报告,指示它是通过还是失败。如果您不这样做,那么您将需要编写自己的报告机制。
单元测试通常具有以下结构:
您的情况有类似的情况,因此是使用 junit 的良好候选者。
可用的单元测试 API 为您提供了有用的实用程序,您通常需要自己编写这些实用程序。
您为什么不尝试这两种方法并亲自看看。
You can, it just wouldn't be recommended. If you write a unit test for testing it, then you can use the junit test runner to run the test and to produce a report indicating whether it passed or failed. If you don't do this then you'll need to code your own report mechanism.
Unit tests have the following structure normally:
Your situation has something similar and is thus a good candidate for using junit.
The unit testing API's available provide you with useful utilities that you would ordinarily have to code yourself.
Why don't you try both approaches and see for yourself.
在单元测试中,您并不是将任何东西作为一个整体进行测试。单元测试通常必须测试一个单元的方法。因此,您应该编写计算数组的方法,并使用 Junit 来测试该方法。
main 方法只是一个入口点,它“定义”过程的流程。在单元测试中我们不担心流程。我们只关注单位。程序流程是使用系统/组件测试而不是单元测试来验证的。
In unit testing you are not testing anything as a whole. A unit test must test a UNIT normally a method. So you should write the method that computes your array, and use Junit to just test the method.
The main method is just an entrypoint and it "defines" the flow of the procedure. In unit testing we don't worry on flow. We just forcus on the unit. The program flow is verified using the System/Component test, not by the unit tests.
因为 JUnit 测试是由框架运行的,而不是作为标准控制台应用程序运行的。
JUnit 测试运行程序通过反射查找测试。
请参阅此处的文档。
Because JUnit tests are run by a framework not as a standard console application.
The JUnit test runner finds the tests by reflection.
See the documentation here.
请参阅:
org.junit.runner.JUnitCore.main(String...)
,类似的东西是底层的。See:
org.junit.runner.JUnitCore.main(String...)
, something like that is underlying.