PHP TDD (Test Driven Development)

Nowadays almost all back-end developers are familiar with TDD. It has much more upsides and very little downsides once the actual classes and the test are in place. If you are just experimenting with your code and do not need it for a longer term, you may not need TDD in that case. TDD is actually essential if you want to maintain your code base for a long time.

So what does the Test Driven Development consist of? Well, there is the unit test that you write before you write most of any new code. Some of the code we have written actually are tested with integration tests. So for code that interacts with third party API or library/database etc, we write integration tests instead of unit tests.

Here are the different types of tests:

  1. Unit Testing: Unit testing is the lowest level of testing. It is usually testing methods inside a class. Unit tests do not interact with other classes directly, but instead with mocks. This makes unit tests isolated and easy to debug and refactor. The test loop would be a 3 steps: Fail, Pass, and Refactoring.
  2. Functional Testing: Functional testing is a type of testing that tests a whole feature which may call many other dependencies. Typically you would test a route for a correct response, or a Controller method that relates to a certain feature in the application. They are slower than integration tests as they require a lot more dependencies.
  3. Integration Testing: Integration testing requires more than one class/object. Hence, it tests the integration between classes i.e dependencies. It is used to test if the database is returning the correct results, if an external API is returning with proper data and data structure, etc. It tests the real classes and functionalities rather than using mocks. It is significantly slower compared to unit tests, since they interact with databases and external providers.
  4. Acceptance Testing / UI Testing: Acceptance testing is the highest level of testing. It relates more on the front-end UI side. It only cares if the feature works through the vantage point of end-users/customers. The test flow goes through the website clicking and submitting forms and expecting correct results for failed results based on data submitted. We use tools like Selenium for this purpose. It’s automating web applications for testing purposes..