30 Days of Automated Testing:Using PHPUnit【Day 1】

Meeting PHPUnit

WilliamP
4 min readDec 30, 2022

First Encounter with Automated Testing

A few years ago, I had just left my first job and transferred to my second job, which was a backend engineer position in the IT department of a large group. At the time, the group was preparing to introduce a microservice system developed by a subsidiary, using PHP 8 and Laravel 9. Because the system worked well in the subsidiary, the top management of the group wanted to expand it to be used by the entire group, so they needed to recruit staff to help (and that staff was me).

On my first day at work, the department vice manager helped me set up the environment and told me that the engineers at the subsidiary had written many automated tests when developing this microservice system. He suggested that I could learn about the functions of the system by reading the automated test cases. This was my first encounter with automated testing and PHPUnit.

What Is Automated Testing?

Let’s look at the definition from Wikipedia:


In software testing, test automation is the use of software separate from
the software being tested to control the execution of tests and the comparison
of actual outcomes with predicted outcomes.

In other words, test automation refers to the process of using other software
to automatically test the software under test, compare actual results with
expected results, and generate test reports.

In short, it is the use of test programs to test the logic of the original program to see if it meets expectations.

Why Use Automated Testing?

At this point, someone might ask, why use automated testing instead of manual testing? Is it worth spending more time writing tests? Indeed, most things that automated testing can do can also be done manually, and writing automated tests is not cost-free. Sometimes it takes more human resources to write tests than to develop the feature itself.

But if we look at it from another perspective, automated testing basically takes the most costs only when writing it for the first time. Afterwards, tests are executed in seconds and are carried out by the computer automatically executing the test program. As long as there are no problems with the test script, the computer will carry out the test in the correct way.

On the other hand, manual testing is carried out by humans, and as long as there is human intervention, there is a certain probability of human error. It is possible that something was missed during the test, some test data was missed, or some test case was missed when testing. These are all possible situations that may arise in manual testing, and most of them can be avoided with automated testing.

In addition, manual testing consumes human resources, usually measured in minutes, which is also a significant cost. Automated testing will probably only cost a few seconds to a few minutes to execute, and once the test script is written, it can be executed multiple times. This can greatly reduce the cost of testing.

Advantages and Disadvantages of Automated Testing

To summarize, here are the pros and cons of test automation:

Pros

  • Automated testing allows computers to execute test programs, eliminating the need for human labor and fatigue.
  • It reduces the possibility of errors caused by human factors.
  • It allows for calculating test coverage, which measures the extent to which the codebase has been tested.

Cons

  • It requires additional costs to develop test programs.
  • It requires additional costs to set up a testing environment.

Test-Driven Development and Automated Testing

In summary, automated testing refers to a method of testing software; whereas test-driven development refers to a development process in which tests are written first and then code logic is written to satisfy those tests. It is possible to use automated testing without using test-driven development, and vice versa.

For example, there is a API developer named John. He writed code logic first and then performs manual testing before writing automated testing programs.

Another API developer named Petter writed test cases on paper first, including test data preparation, test process, and expected results. Then, he uses these test cases to test the program. Since the corresponding code logic has not yet been developed, it will not meet the test expectations.

At this point, Peter began to develop the code logic for each function and runs the relevant test cases for each function after each development until all test cases are satisfied, at which point the development is complete.

In the case of John, he used automated testing to test the program, but did not use test-driven development. In the case of Peter, he used the development process of test-driven development, but did not use automated testing.

The Scope of Discussion in This Series of Articles

In this series, I will share my various experiences using PHPUnit over the years, with its various test scenarios. The main content includes commonly used test functions, API testing, database testing, screen testing, Mocking, test coverage calculation, Seeder, Data Provider, and so on, hoping to be helpful to beginners of PHPUnit and automation testing.

The next article will introduce the environment setup.

If you liked this article or found it helpful, feel free to give it some claps and follow the author!

Reference

  1. https://en.wikipedia.org/wiki/Test_automation
  2. https://en.wikipedia.org/wiki/Test-driven_development

Articles of This Series

--

--