Java Reference
In-Depth Information
Tests
Testing is an important part of programming that can often be overlooked. Writing good
tests means that your code will be less brittle as it develops, and any errors will be identified
early on.
A test can simply be a function that tests that a piece of code runs as it should. For example,
we could test that the squareRoot() returns the correct answer with the following func-
tion:
function itSquareRoots4() {
return squareRoot(4) === 2;
}
Here we're comparing the result of squareRoot(4) with the number 2 . This will return
true if our function works as expected, which it does:
itSquareRoots4();
<< true
Clearly this is in no way a thorough test of the function, but I hope it illustrates how it works.
Test-driven Development
Test-driven development (TDD) is the process of writing tests before any code. Obviously
these tests will initially fail, because there is no code. The next step is to write some code
so that the tests pass. After this the code is refactored to make it more readable, faster, and
remove repetition. This process should be followed in small piecemeal chunks every time a
new feature is implemented. It gives the following workflow:
1. write tests (that initially fail)
2. write code to pass the tests
3. refactor the code
4. write more tests for new features
 
Search WWH ::




Custom Search