Unit Testing

Unit testing tests individual components. Each test runs in isolation, and tests only a single item, such as a function or method. This function or method is considered the unit.

An example of a unit test is shown below. This test is describing a system where we may have a database model that represents information about various novels. We are testing to verify that the getHorrorRating method of the model works correctly. This method should return an integer between 0 and 5 describing how high that novel rated in the "Horror" genre.

As our example, we pull a Stephen King novel, as King is largely known as a Horror author.


use Example\Assertions;
use PHPUnit\Framework\TestCase;

class AssertionsTest extends TestCase {
    public function test_get_number_returns_5(): void {
        $assertions = new Assertions();
        $this->assertEquals(5, $assertions->getNumber());

The test here is limited to the single line with the assertion. Everything else is setup.

Tools for Unit Testing

Helpful Projects and Further Reading

Last updated