PHPUnit

PHPUnit

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks.

Why use phpunit?

phpunit belongs to the family of xUnit test libraries. You use these libraries to create automatically executable tests which verify your application's behavior. This is important to ensure, among others, that your changes don't break existing functionality.

Listing all reasons for automated testing, the various kinds and technologies would be too much. Check out the following links to read about:

  • Why to write unit tests and this link
  • How to write good tests (also search for unit tests and xUnit test patterns)
  • How to test the user interfaces (also search for selenium)
Types of tests

Before we dive into PHPUnit, let's understand different types of tests. Depends on how you want to categorize it, there are all kinds of tests in software development.

We would like to categorize tests based on the level of specificity of it. According to Wikipedia. There are generally four recognized levels of tests:

  • Unit testing: It tests the smallest unit of functionality. From a developer's point of view, its focus is to make sure one function does what it is supposed to do. Thus it should have minimum or no dependence on other function/class. It should be done in memory, which means it should not connect to database, access the network or use the file system and so on. Unit testing should be as simple as possible.
  • Integration testing: It combines units of codes and tests the combination works correctly. It is built on top of unit testing and it is able to catch bugs we could not spot by unit testing. Because integration testing checks if Class A works with Class B.
  • System testing: It is created to simulate the real time scenarios in a simulated real life environment. It is built on top of integration testing. While integration testing ensure different parts of system work together. System testing ensures the whole system works as user expected before sending it to acceptance testing.
  • Acceptance testing: When tests above are for developers at development stage. Acceptance tests are actually done by the users of the software. Users do not care about the internal details of the software. They only care how the software works.