问题
Can you explain in a few sentences:
- Why we need it / why they make our life easier ?
- How to unit-test [simple example in Java] ?
- When do not we need them / types of projects we can leave unit-testing out?
- useful links
回答1:
Why we need it / why they make our life easier ?
- 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...
Links
JUnit - Unit testing
EclEmma - test coverage tool
link text - Wikipedia link to unit testing
回答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.
回答3:
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:
- HTMLUnit for Web
- SIPUnit for SIP
- SwingUnit for GUI Code
回答4:
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)
回答5:
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)
回答6:
This is how how your programming should be :
- Decide on an interface (not necessarily a java interface, but how method looks like to everybody
- Write a test
- Code the implementation
回答7:
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.
来源:https://stackoverflow.com/questions/2355463/unit-testing-in-java-what-is-it