It allows you to check the expected behavior of the piece(s) of code you are testing, serving as a contract that it must satisfy.
It also allows you to safely re-factor code without breaking the functionality (contract) of it.
It allows you to make sure that bug fixes stay fixed by implementing a Unit test after correcting a bug.
It may serve as as a way to write decoupled code (if you have testing in mind while writing your code).
How to unit-test [simple example in Java] ?
Check out the JUnit website and the JUnit cookbook for details. There isn't much to writing JUnit test cases. Actually coming up with good test cases is surely harder than the actual implementation.
When do not we need them / types of projects we can leave unit-testing out?
Don't try to test every method in a class, but rather focus on testing the functionality of a class. Beans for example, you won't write tests for the getters and setters...
Because that is your proof that the application actually works as intended. You'll also find regression bugs much easier. Testing becomes easier, as you don't have to manually go through every possible application state. And finally, you'll most likely find bugs you didn't even know existed even though you manually tested your code.
Google for junit
Unit tests should always be written, as said, it is your proof that the application works as intended. Some things cannot or can be hard to test, for example a graphical user interface. This doesn't mean that the GUI shouldn't be tested, it only means you should use other tools for it.
It's probably work reading the Wikipedia article on Unit Testing, as this will answer most of your questions regarding why. The JUnit web site has resources for writing a Java unit test, of which the Junit Cookbook should probably be your first stop.
Personally, I write unit tests to test the contract of a method, i.e. the documentation for a particular function. This way you will enter into a cycle of increasing your test coverage and improving documentation. However, you should try to avoid testing:
Other people's code, including the JDK
Non-deterministic code, such as java.util.Random
JUnit is not the only unit testing framework available for Java, so you should evaluate other frameworks like TestNG before diving into it.
In addition to "top-level" frameworks, you will also find quite a few projects covering specific areas, such as:
What I would have written is already covered in many of the responses here but I thought I'd add this...
The best article I ever read on when to use/not to use unit tests was on Steve Sanderson's blog. That is an excellent article covering the cost/benefit of unit testing on different parts of your code-base (i.e. a compelling argument against 100% coverage)
我有一个逐步过程作为示例,在 Eclipse 中编写 Java 单元测试。请查看“如何编写 Unite 测试”。
我希望它有帮助。
As it's name shows, it is test for our units in our program. For example, if you have a method that returns the summation of two integers, you will test it with two numbers to see if it works correctly. For example, for 2+2, it returns 4.
We need these tests, since in big projects, there are a lot of programmers. It is important that these programmers work in harmony. unit tests act like the proof of correctness for the procedures that our programmers writes. so, a programmer writes his procedure plus it's unit test in order to show that his procedure works correctly. Then, he commits his changes to the project. These tests help you to prevent a lot of bugs before occurrence.
I have a step by step procedure as an example, to write down a Java unit test in Eclipse. Please look "How To Write Unite Tests".
发布评论
评论(7)
查看 JUnit 网站和 JUnit 食谱 了解详细信息。编写 JUnit 测试用例没有太多内容。实际上提出好的测试用例肯定比实际实施更困难。
不要尝试测试类中的每个方法,而应专注于测试类的功能。例如,Bean,您不会为 getter 和 setter 编写测试......
JUnit - 单元测试
EclEmma - 测试覆盖率工具
链接文本 - 单元测试的维基百科链接
Check out the JUnit website and the JUnit cookbook for details. There isn't much to writing JUnit test cases. Actually coming up with good test cases is surely harder than the actual implementation.
Don't try to test every method in a class, but rather focus on testing the functionality of a class. Beans for example, you won't write tests for the getters and setters...
JUnit - Unit testing
EclEmma - test coverage tool
link text - Wikipedia link to unit testing
因为这是应用程序确实按预期运行的证明。您还会更容易地发现回归错误。测试变得更加容易,因为您不必手动检查每个可能的应用程序状态。最后,即使您手动测试了代码,您也很可能会发现您甚至不知道存在的错误。
Google for junit
应始终编写单元测试,如上所述,它是应用程序按预期工作的证明。有些东西不能或可能很难测试,例如图形用户界面。这并不意味着不应测试 GUI,而只是意味着您应该使用其他工具。
参见第 2 点。
Because that is your proof that the application actually works as intended. You'll also find regression bugs much easier. Testing becomes easier, as you don't have to manually go through every possible application state. And finally, you'll most likely find bugs you didn't even know existed even though you manually tested your code.
Google for junit
Unit tests should always be written, as said, it is your proof that the application works as intended. Some things cannot or can be hard to test, for example a graphical user interface. This doesn't mean that the GUI shouldn't be tested, it only means you should use other tools for it.
See point 2.
阅读有关 单元测试 的维基百科文章可能会有所帮助,因为这将回答您有关的大多数问题为什么。 JUnit 网站提供了用于编写 Java 单元测试的资源,其中 Junit Cookbook 可能应该是您的第一站。
就我个人而言,我编写单元测试来测试方法的契约,即特定函数的文档。这样,您将进入增加测试覆盖率和改进文档的循环。但是,您应该尽量避免测试:
JUnit 不是唯一可用于 Java 的单元测试框架,因此您应该评估其他框架,例如 TestNG 在深入研究它之前。
除了“顶级”框架之外,您还会发现不少涵盖特定领域的项目,例如:
It's probably work reading the Wikipedia article on Unit Testing, as this will answer most of your questions regarding why. The JUnit web site has resources for writing a Java unit test, of which the Junit Cookbook should probably be your first stop.
Personally, I write unit tests to test the contract of a method, i.e. the documentation for a particular function. This way you will enter into a cycle of increasing your test coverage and improving documentation. However, you should try to avoid testing:
JUnit is not the only unit testing framework available for Java, so you should evaluate other frameworks like TestNG before diving into it.
In addition to "top-level" frameworks, you will also find quite a few projects covering specific areas, such as:
我要写的内容已经在这里的许多回复中涵盖了,但我想我应该添加这个...
我读过的关于何时使用/不使用单元测试的最好的文章是 史蒂夫·桑德森的博客。这是一篇优秀的文章,涵盖了对代码库的不同部分进行单元测试的成本/收益(即反对 100% 覆盖率的令人信服的论点)
What I would have written is already covered in many of the responses here but I thought I'd add this...
The best article I ever read on when to use/not to use unit tests was on Steve Sanderson's blog. That is an excellent article covering the cost/benefit of unit testing on different parts of your code-base (i.e. a compelling argument against 100% coverage)
关于 Java 单元测试的方式和原因的最佳书籍之一是 使用 JUnit 在 Java 中进行实用单元测试(Andy Hunt 和 Dave Thomas)
One of the best books on the hows and whys of unit testing in Java is Pragmatic Unit Testing in Java with JUnit (Andy Hunt & Dave Thomas)
这就是你的编程应该是这样的:
This is how how your programming should be :
正如其名称所示,它是对我们程序中的单元的测试。例如,如果您有一个返回两个整数之和的方法,您将使用两个数字来测试它,看看它是否正常工作。例如,对于2+2,它返回4。
我们需要这些测试,因为在大项目中,有很多程序员。这些程序员协调工作非常重要。单元测试就像我们程序员编写的程序的正确性证明。因此,程序员编写他的程序并进行单元测试,以表明他的程序正常工作。然后,他将更改提交给项目。这些测试可以帮助您在发生之前防止很多错误。
我有一个逐步过程作为示例,在 Eclipse 中编写 Java 单元测试。请查看“如何编写 Unite 测试”。
我希望它有帮助。
As it's name shows, it is test for our units in our program. For example, if you have a method that returns the summation of two integers, you will test it with two numbers to see if it works correctly. For example, for 2+2, it returns 4.
We need these tests, since in big projects, there are a lot of programmers. It is important that these programmers work in harmony. unit tests act like the proof of correctness for the procedures that our programmers writes. so, a programmer writes his procedure plus it's unit test in order to show that his procedure works correctly. Then, he commits his changes to the project. These tests help you to prevent a lot of bugs before occurrence.
I have a step by step procedure as an example, to write down a Java unit test in Eclipse. Please look "How To Write Unite Tests".
I hope it helps.