9951 explained code solutions for 126 technologies


php-laravelHow do I write and run tests in Laravel using PHP?


Writing and running tests in Laravel using PHP is quite easy. The following steps will guide you through the process:

  1. Create a test class:
<?php

use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    // test methods
}
  1. Add test methods to the class:
<?php

use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    public function testSomething()
    {
        $this->assertTrue(true);
    }
}
  1. Run the tests using the phpunit command:
$ phpunit

PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 00:00.001, Memory: 6.00 MB

OK (2 tests, 2 assertions)
  1. Assertions can be used to verify the expected outputs:
<?php

use PHPUnit\Framework\TestCase;

class ExampleTest extends TestCase
{
    public function testSomething()
    {
        $this->assertEquals(2, 1 + 1);
    }
}
$ phpunit

PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 00:00.001, Memory: 6.00 MB

OK (1 test, 1 assertion)

For more information, please refer to the Laravel documentation and the PHPUnit documentation.

Edit this code on GitHub