Home > Articles > Best practice PHPUnit

PHPUnit dos and don'ts

When writing a unit tests. There are couple of things to do and couple of things which should be avoided. In this article I have listed the dos and don'ts with regards to unit testing.


Test only the public functions.

Test each and every public function. In a ideal world a class should have only one public function and its dependencies.

Mock every dependency.

Write meaningful names which explain what the test is doing.

Test should cover success and failure, both aspects of a code.

Test that every raised exception is tested.

Always specify how many times a function is expected to be called while testing a piece of code.

Test setup pain is a smell, which means your code needs to be refactored.

Code coverage should be 100%, which means each and every line of the tested class was reached.

Live deployment should not work with failing unit tests.

Add tests to classes which are missing unit tests.


Tested class should never talk to database, file, network resources etc.

Dependencies in tested class should not create real class objects.

what clients say about us

Client testimonials

Our sites look great and run smoothly. Thanks for all your hard work and patience in getting us to where we are today.

contact us

Contact Us

If you have questions or want us to send you a quote for project please email us.

latest news


PHPUnit best practice
In this article I have listed the dos and don'ts with regards to unit testing.