Unit Testing Best Practices

Unit TestingHere are 5 unit testing best practices:

1. Automate Unit Testing by using a unit testing framework.

Unit Testing works best in iteration. Find a unit testing framework for your chosen programming language. For PHP, the popular one is PHPUnit.

2. Write the tests first, before the code.

This is a matter of choice. But, writing tests first allow the system to be developed with the absolute minimum to get it working. The code can be refined later using refactoring.

3. Write a test case for each major component.

Unit testing, while atomic, should be used to test components, not trivial items such as getter and setter. The test case should test the behaviour of the system. Especially front-end facing functions.

4. Refactor the test code as well as the code

During code refactoring, it is also wise to visit the test code to see if it can be use refactoring as well.

5. Run test continuously

A good unit testing is only good when it is run regularly. Especially after the codebase changes.

You can also read on these resources for more tips:

Got any tips on your own ? Share with us in the comment section below.