dagblog.
← Archive

Programming means writing tests


Programming and programming techniques keep evolving. Today we’re beginning to see 
 methods from a branch of the field that may one day go mainstream. It’s called
 the Extreme Programming approach. Its emphasis
 on testing and testing techniques, and the systematic way it made them 
 an essential part of software development, gave rise to what we now call
 test-driven development (TDD).

The first step is always to write a test that checks the behavior 
 we intend to implement. For example, to implement a simple object for basic arithmetic, we would
 first write a test for the operations. A test is essentially a statement of what we expect from the implementation.
 If we expect a call to public int secti(int a, int b) to return the sum of a
 and b, we can write a simple test.

int ocekavanaHodnota = 2;
int vracenaHodnota = secti(1, 1);
if(ocekavanaHodnota != vracenaHodnota) {
//Implementace metody secti nenaplnila naše očekávání,
//je třeba jí opravit.
}

The beauty of tests is that our expectations about a piece of behavior stay fixed. They’re set by the object’s interface. The
 implementation can change over time. If I have a method for sorting an array, it’s up to the implementation
 whether to use bubble sort or quicksort. The expected result is still a sorted array. In other words, we write the tests
 once and keep them. Tests accumulate over time, and it’s easy 
 to automate running them and reporting the results for evaluation.

The example above is a little clumsy, but it does its job. Today there are plenty of tools that
 help us write tests efficiently as we develop. One of them is 
 JUnit. Tests and the testing process reduce the cost of development and maintenance by helping catch
 bugs early. Testing has its limits, of course. For me, one example is 
 testing logic that works with database data.

If this quick introduction to tests and testing caught your interest, try these links. They’re
 a little Java-heavy, but well worth a look. Using JUnit With Eclipse IDE
 and Effective Unit Testing with DbUnit.

Related articles

Medium version

Related articles

Related CZ Podcast episodes