paint-brush
PHP Test Driven Development Part 1: Introductionby@sameernyaupane
11,191 reads
11,191 reads

PHP Test Driven Development Part 1: Introduction

by Sameer NyaupaneDecember 1st, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Test Driven Development is a coding practice where you write a test first then write the code to pass that test, usually in short iterative cycle.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coin Mentioned

Mention Thumbnail
featured image - PHP Test Driven Development Part 1: Introduction
Sameer Nyaupane HackerNoon profile picture

Test Driven Development is a coding practice where you write a test first then write the code to pass that test, usually in short iterative cycle.

Test Driven Development(TDD), was popularized by Kent Beck. TDD is one of the main techniques followed in his Extreme Programming software development methodology.

Now let’s visualize TDD in steps:

  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

So now you can see that TDD is a closed loop process. A programmer is immersed into a flow that keeps him tied into the practice of either writing a test or writing the feature implementation code.

“So what are the benefits?” you may ask. There are several benefits to following a TDD approach.

  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.

One very good talk regarding this is Min-maxing Software Costs by Konstantin Kudryashov.

In the talk Konstantin talks about the exponential increase in cost of change(adding new features) as project time goes on if there are no tests.

Exponential cost of change without tests

But if there are tests, the cost of change gets capped, making it much easier to add new features.

Capped cost of change with tests

There are of course some disadvantages too.

  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.

One interesting series of debate is the Is TDD Dead? series by Martin Fowler, Kent Beck and David Heinemeier Hansson. David argues that TDD is not suitable for all types of coding styles. And argues that TDD has actually introduced more problems that it solves.

But all in all, I prefer TDD since it has much more upsides and very little downsides. I say, TDD is actually essential if you want to maintain your code base for a long time. 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.

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 write 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. Don’t worry, we will go through each type of tests in detail soon on the upcoming parts.

Let’s look at the different types of tests in brief:

  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 testing 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.

In the next part we will go through how to setup PHPUnit and get started with unit testing. We will first do it without following TDD approach. This is because it will be hard to grasp both concepts while just starting to learn how to test. We will follow TDD once we are comfortable with writing tests. So we’ll save that for a later part. :)

Hope you enjoyed the article. Don’t forget to hit the 👏 icon below. And subscribe for more!