Unit Testing
Motivation
- complex systems hard to create
- bugs in systems hard to debug
- SOLUTION : start with unit testing codeeven before writing project code.
- helps with API design
- helps with debugging
What is Unit Testing?
- For each piece of "work" code -- a class or a method, pair the work code with some "unit test" code
- unit test code calls the work code through its public API, calling it a few different ways and checking the results.
- does not need to be exhaustive -- may only/at first test obvious cases
- a standard, maintained way to keep tests in parallel with the work code
- Take time ---this is an investment. Can take sometimes as much time (or more if very extensive) then creation of the project code itself.
- Basic Unit Testing = simple testing of code, simple obvious cases, easiest to write (hopefully)
- Advanced Unit testing = I mean here that (probably after you have started working on the project code) you have a better idea of more elaborate input and output situations and problems that might arrise.
- Edge Unit Testing = here I mean possibly some unusual (hopefully simple) cases (inputs) that may not frequently occur but, stretch the use of our project code.
Test Driven Development
- Write test cases first, then write project code to get the test cases to work.
Basic Unit Testing
to give you some idea of what basic unit testing might be consider this scenario: Suppose you have a doit() method to test.
- tests should use the regular, public interface of the class used by regular client code -- call things, test results
- At first the tests fail, since the work code is not written -- unit tests can fail by not even compiling -- that's fine at this stage
- Now start writing the work code, eventually the tests pass, yay!
- Call Every Method A Few Times Differently
- Don't call doit() 5 times, but where the calls are very similar.
- When testing a same(x, y) method -- don't only give x,y where same() should return true -- call it once or twice where it should return false too!
- New Test case - take old test case and modify it a little.
|