PHP Testing with PHPUnit and Pest

PHP Testing with PHPUnit and Pest

Last Updated on Mar 22, 2023

Intro

Testing is a very important step in developing an application. There are 2 packages in php that can help you with writing tests.

Do you remember when we talked about Dependency management?

phpunit

Let’s start with the first package phpunit/phpunit

Let’s require that in our project by running:

composer require phpunit/phpunit

Now inside the vendor folder your dependencies for phpunit are added.

Now if you run

./vendor/bin/phpunit

You see a list of commands you can use with phpunit

As you can see in the usage commands we have 2 ways to use it:

phpunit [options] UnitTest.php
phpunit [options] <directory>

Let’s create a tests directory so we can put our tests there

Let’s create a file called ExampleTest.php

We should create a class and our class should extend the phpunit’s TestCase

Do you remember when we talked about extending classes?

So let’s do it. Let’s create a class called ExampleTest which extends TestCase

use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
}

Now whatever test we want to write we can write inside this class. Each test is a function inside this class and the name starters with test

The name of the function can be camelCase or snake. like: testItWorks or test_it_works

Now let’s create a simple test that checks if a value is true.

So our function can be like

function testItWorks()
{
   
}
// Or
function test_it_works()
{
   
}

Now how to check for values? We can use assert functions. And they are the functions that we get access to because we are extending the testCase class. So we can use $this keyword and access them.

For example if we want to check if a value is true we can use this function

$this->assertTrue($value);

So let’s add that to our function and now our class looks like this

use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
   function testItWorks()
   {
       $this->assertTrue(true);
   }
 
   function test_it_works()
   {
       $this->assertTrue(true);
   }
}

Now it’s time to run the tests.

./vendor/bin/phpunit tests

Great it shows something like this

OK (2 tests, 2 assertions)

It means we had 2 tests and both of them were successful and all the assertions were correct.

But what if one of our tests was not correct.

function test_it_works()
{
   $this->assertTrue(false);
}

Now when I run the tests I get this:

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.

It means one of the tests failed.

Pest

Now let’s see the other one which was pest we can use it in our current project or we can create a new project.

Let’s require it in our current project because I want to show you something cool

composer require pestphp/pest

Then run

./vendor/bin/pest --init

Now what’s the cool thing I wanted to show you? The cool thing is that it can run all your phpunit tests as well.

I didn’t change any file but when I run

./vendor/bin/pest tests

it runs the file and tells me this:

FAIL  ExampleTest
  ✓ it works
  ⨯ it works

it also shows which file at what line failed and a lot more information.

With phpunit our file looked like this:

use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
   function testItWorks()
   {
       $this->assertTrue(true);
   }
 
   function test_it_works()
   {
       $this->assertTrue(true);
   }
}

But with pest it can be much more readable and simpler. Let’s create a new file called ExamplePestTest.php

With pest we can write our tests with the help of two functions: test and it

the first argument is the description of the test and the second argument is our test itself

test('pest is working', function () {
   
});

it('is working', function () {
   
});

With pest instead of writing

$this->assert

we write

expect()->tobe

It’s like english. Expect my value to be true would be translated to

expect($myValue)->toBeTrue();

Super easy to read. right?

So let’s test both of our functions

test('pest is working', function () {
   expect(true)->toBeTrue();
});

it('is working', function () {
   expect(true)->toBeTrue();
});

And it shows me this message:

PASS Tests\ExamplePestTest
✓ pest is working
✓ it is working

Perfect.

That’s it. That’s how easy it is to write a test. You can write tests and check for every aspect of your application.Isn’t it amazing?

This was just the intro. You can learn more about testing and how to organize your tests in different folders, how to get coverage and things like that from the official documentation for phpunit and pest.

https://youtu.be/PYndEaiVAic

Conclusion

Now you know about testing with phpunit and pest in PHP.

I recommend you to create a PHP and try to write tests with both packages. read their documentations and see the possibilities.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • introduction to testing in php
  • add packages for testing to the project
  • test with phpunit and pest
  • phpunit in php
  • pest in php
  • difference between pest and phpunit

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

Courses