PHP Unit Testing Examples

141 0 0 0

Last Updated : 2023-05-28 09:40:58

In this article we will just show few examples and guide lines for Unit Testing in Laravel applications.

There are two areas for testing:



  1. Feature Tests : Full list of assertions

    //Running the test from your terminal
    vendor\bin\phpunit //Laravel 10
    phpunit //Laravel 7

    //---------------------------------------------------------- EXAMPLE TESTS
    public function test_the_application_returns_a_successful_response(): void
    {
    $response = $this->get('/');

    $response->assertStatus(200);
    }

    public function test_that_true_is_true(): void
    {
    $this->assertTrue(true);
    }

    //----------------------------------------------------A list of possible testing methods
    //Full list of assertions are found here : https://phpunit-document-english.readthedocs.io/en/latest/assertions.html
    $response->assertStatus(200);
    $response->assertSee(''Laravel); //Will check if the response page has the text 'Laravel' on it
    $response->AssertDontSee('This text shall not be on the page to pass the test'); //Will check if the text is NOT there
    $this->assertTrue(true);​


  2. Unit Tests

  3. 1

Mohammed Anwar

Mohammed Anwar

Experienced technical lead PHP, MySQL and Laravel Developer for 15+ years, with proven ability to develop and create high-quality and optimized web applications. Great ability to build and optimize database design, schema and queries. Versed programing trainer and instructor delivering web courses and helping others to get into the field in a timely manner. Fast and eager learner for new technologies .