1. Reasons for not writing unit tests:
1. It takes too much time to write unit tests--write the test code while writing the implementation code
2. Run the test too long--separate time-consuming tests from other tests
3. Testing the code is not my job--don't expect someone else to clean up your code
4. I'm not sure what the code is doing, so it's impossible to test---------"build a prototype to help identify the needs
5. But the code can be compiled--"The compiler's acquiescence is only a very rough recognition of the code, only to verify that the syntax is correct, do not guarantee that the code behaves correctly
6. The company asked me to write the code, not the test--"unit test is basically a tool, and a compiler, development environment, editor, etc. in a unified position of the tool
7. If I don't have a tester or QA staff to work, then I feel guilty--"unit testing is just a low-level, code-oriented test for programmers.
8. My company does not allow me to run unit tests in real systems--"You can run unit tests on your own computer, use your own database, or use mock objects
2. JUnit
1. Custom assertion--encapsulation of common assertions
2. Exceptions: Predictable exceptions, unpredictable exceptions
3. What content to test--"Right-bicep
1. Whether the result is correct (right): If the code can run correctly, how do you know the result is correct?
2. Use of data files
3. Boundary conditions (boundary)-correct
6. Forced error condition (error-condition)-full disk, disconnected network connection, memory leak, dependency failure, clock problem, system overload, limited color palette, display resolution too high or too low
7. Performance characteristics--"as the input size slowly becomes larger, the problem slowly become complex
4. Boundary conditions correct--The fundamental question to be answered is: What else is wrong?
1. Consistency (conformance)--prepare data to match your desired structure, lack of head and tail
2. Order (ordering)-------------------the order of data, or the position of a data in a dataset
3. Interval (range)--"For a variable, the range of values that it belongs to may be broader than what you need or want, such as reshaping, negative values, greater than maximum, less than minimum, etc.
4. Reference/coupling (reference)-the program refers to what is outside the program or external dependencies, what other conditions the program needs to run
(1) Before condition: The system must be in what state this method can run
(2) After the condition: Your method will ensure that the state of what happened
5. Existence (Existence)--"For the values you pass in or maintain, ask yourself if the values do not exist, they may not exist when you expect them to exist, and how the behavior of the method will be
6. Cardinality (cardinality)-"0-1-n" principle
7. Temporal (time)--relative time, absolute time, concurrent access and synchronous access
8. Exercise:
1. A simple Stack class
(1) for the new stack, IsEmpty should be true,pop and top should throw Stackemptyexception exception
(2) If the item is null, the stack should not change, if the item is "", the stack inserts a control Fu data
(3) If the item is very long, the maximum length of the string is reached, the stack is inserted correctly, or the stack insertion exception is exceeded.
(4) For the empty stack, execute the push and then execute the pop, return to normal, then execute top, return stackemptyexception exception. For an empty stack, the push is performed before the top is executed, and then the pop is returned to normal. For a stack that has a value, the push is performed and the pop is executed, the top is executed, or the pop is executed after the top is executed, returning to normal. For a stack with a value, performing the Top,pop after performing the pop is normal, top throw stackemptyexception exception. For stacks with multiple values, the order of individual executions returns to normal. X Each method is independent
(5) Press the push continuously, each call top return string, and IsEmpty return false. Press in continuously push, call pop, call top again, call IsEmpty, if empty stack executes, top throws empty exception, IsEmpty is true.
2. A shopping Cart
(1) for an empty shopping cart, ItemCount is 0,iterator for empty set, Additems, item is null, for nonexistent merchandise, or quantity is-N,-1, 0, 1, N, For negative negativecountexception, it is possible to throw a negativecountexception if the previous count is negative. DeleteItems throws Nosuchitemexception anomaly.
2. If the quantity parameter exceeds the original quantity, it is equal to the original quantity, quantity=0, or negative, or the item does not exist in the shopping cart,
3. If the shopping cart is empty, the shopping cart a product quantity is 0, the product quantity is negative, itemcount return value
4. The shopping cart is empty is the iterator, the shopping cart only has one commodity, the shopping cart has only one kind of goods multiple, the shopping cart has a variety of goods only one time, the shopping cart has many kinds of goods multiple times.
3. A fax Scheduler
1. Telephone number length of more than 12, there is no first or second "-" number or none, format is not the first three, middle three, the second four format, the phone number is null or "", X is not 2~9 between the number, Xn is not a number
2. FileName is null or "",
4. An automatic sewing machine for embroidery
1. There is no need to sew the workpiece, workpiece size than sewing machine, workpiece size of 0
2. Width<1,width=1,width>1. Height<1,height=1,height>1
3. Tablesize is null
5. An audio/video displacement editor
1. Fast Forward fast Retreat is located in the bot or eot or between a position, fast forward fast rewind of the seconds number is 0, negative, or more than the current position to the bot (EOT) distance, should return to the bot (EOT)
2. Get the current position at a location between Bot,eot or
3. When the mark is located at or between Bot,eot or somewhere between, the name is a null tag fails, then the mark fails, then the duplicate tag fails
4. Gotomark when name is null,name as "", name does not exist
5. Using Mock objects
1. Simple replacement--"replace the real object with" "Substitute"
2. Mock Object--the substitute of real object in commissioning period
3. Test servlet--"Use mockhttpservletrequest and mockhttpservletresponse to simulate request and response
4. Easy Mock Object--Specifies which method in the interface to mock is allowed, and what is its return value
5. Exercise-write mock implementation based on test
6. The quality of a good test--"A-trip
1. Automation (Automatic)--"unit testing needs to be able to be automatically run, including: Invoke test automation and inspection results automation
2. Thorough (thorough)-tests all possible scenarios, code coverage, or only the most likely scenarios: boundary conditions, incomplete and malformed data.
(1) Coverage tool: Nounit,quilt,clover
3. Repeatable (repeatable)--each test should produce the same result each time
4. Independent (independent)--each test is independent of the environment and other tests
5. Professional (Professional)-use and product code the same professional level to write and maintain test code, in the test code, for good design of all the general rules-maintenance encapsulation, the adoption of dry principles, reduce coupling, etc., must be as follows in the product code
6. Test test--revise bugs while improving the test, by introducing bugs to prove the test
(1) How to fix bugs
(2) Spring the trap (self-setting)
7. Testing in the project
1. Where to put the test code
(1) The same directory
(2) Sub-directory
(3) Parallel tree
2. The test courtesy
3. Frequency of testing
(1) Writing a new function
(2) Correcting bugs
(3) after each successful compilation
(4) Check in version control every time
(5) to continuously
4. Testing and Legacy code
5. Testing and Review
8. Design Topics
1. Test-oriented design
2. Refactoring for testing
3. The invariance of the test class
(1) Structured
(2) Mathematical invariance
(3) Data consistency
4. Test-driven design--"test first"
(1) Practice
9. Gotchas
1. As long as the code can work
2. Smoke test-----"not only smoke
3. Please let my machine run--"all machines should be able to run
4. Floating point number problem--"precision"
5. The test cost too much time
6. Test always fails--"coupling too high"
7. Test failure on some machines--"environmental issues: machines, systems, versions, running speed
8. My main is not running--"JUnit does not run the main method in the class
10. Resources
1. easy-mock:http://www.easymock.org
2. junit:http://junit.org
3. junitperf:http://www.clarkware.com
4. MockObject:www.mockobjects.com
5. Nounit:nounit.sourceforge.net
6. Quilt:quilt.sourceforge.net