PHP Test Driven Development Part 1: Introduction

  1. Write a test (enough to make it fail)
  2. Run it, and see it fail.
  3. Write the code.
  4. Run the test again and see it pass.
  5. Repeat Step 1
  1. It helps keep the distance between what you know works and what doesn’t in your code very thin. You can pretty much figure out what went wrong, since you had the tests working perfectly a few seconds ago.
  2. It helps to minimize the time needed to refactor your code, since you can easily test if something breaks when you do refactor.
  3. It makes adding new features that much easy, since you can change code to accommodate new features very easily.
  4. It makes you confident as a programmer. You do not have to fear the code anymore. This is due to the above points 2 and 3. You can easily add new features and refactor your code without worry.
  5. It helps to break down software development into chunks that you can manage. So long gone are the fear of starting out on a new feature, since the TDD loop is short enough to easily get started.
  6. It helps to stop the exponential cost of change as the software grows, since you can add features without it taking more time.
Exponential cost of change without tests
Capped cost of change with tests
  1. It is not easy to get started with TDD. It requires you to know a lot of new practices and techniques. Example: unit testing, mocks, assertions etc.
  2. It takes more time upfront when doing the development, since you are testing your code as you write.
  1. Unit Testing: Unit testing is the lowest level of testing you can do. It is usually testing a method 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.
  2. Integration Testing: Integration testing touches more than one class. Hence, it tests the integration between classes i.e dependencies. It is used to test if the database is giving us the correct results, if an external API is providing us with correct data etc. It tests the real classes and functionalities rather than using mocks. It is significantly slower compared to unit tests to run, since they interact with databases and external providers.
  3. Functional Testing: Functional testing is a type of testing that tests a whole feature which may call a lot of 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 touch a lot more dependencies.
  4. Acceptance Testing: Acceptance testing is the highest level of testing. It only cares if the feature works through the vantage point of customers. The test flow goes through the website clicking and submitting forms and expecting correct results. We use tools like Selenium for this purpose.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store