PHPUnit: 03 - Writing the First Test

 

tests/SampleTest.php

<?php

use PHPUnit\Framework\TestCase;


class SampleTest extends TestCase
{
    
}

 

 


Execution

w:\w\git\phpunitbasics (develop-init-setup -> origin)
λ vendor\bin\phpunit
PHPUnit 7.4.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.2.9
Configuration: W:\w\git\phpunitbasics\phpunit.xml

W                                                                   1 / 1 (100%)

Time: 45 ms, Memory: 4.00MB

There was 1 warning:

1) Warning
No tests found in class "SampleTest".

WARNINGS!
Tests: 1, Assertions: 0, Warnings: 1.

 

 

 

 


 

 

 

 


Add a function

 

<?php

use PHPUnit\Framework\TestCase;


class SampleTest extends TestCase
{
 
    public function testTrueAssertsToTrue()
    {
        //
    }
    
}

 

Then Run it

 

w:\w\git\phpunitbasics (develop-init-setup -> origin)
λ vendor\bin\phpunit
PHPUnit 7.4.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.2.9
Configuration: W:\w\git\phpunitbasics\phpunit.xml

R                                                                   1 / 1 (100%)

Time: 48 ms, Memory: 4.00MB

There was 1 risky test:

1) SampleTest::testTrueAssertsToTrue
This test did not perform any assertions

W:\w\git\phpunitbasics\tests\SampleTest.php:9

OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Risky: 1.

 


Add an assertion to the test

<?php

use PHPUnit\Framework\TestCase;


class SampleTest extends TestCase
{
 
    public function testTrueAssertsToTrue()
    {
        $this->assertTrue(true);
    }
    
}

 

And Run

w:\w\git\phpunitbasics (develop-init-setup -> origin)
λ vendor\bin\phpunit
PHPUnit 7.4.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.2.9
Configuration: W:\w\git\phpunitbasics\phpunit.xml

.                                                                   1 / 1 (100%)

Time: 47 ms, Memory: 4.00MB

OK (1 test, 1 assertion)

 

 


Set assertion to fail

<?php

use PHPUnit\Framework\TestCase;


class SampleTest extends TestCase
{
 
    public function testTrueAssertsToTrue()
    {
        $this->assertTrue(false);
    }
    
}

And Run

w:\w\git\phpunitbasics (develop-init-setup -> origin)
λ vendor\bin\phpunit
PHPUnit 7.4.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.2.9
Configuration: W:\w\git\phpunitbasics\phpunit.xml

F                                                                   1 / 1 (100%)

Time: 45 ms, Memory: 4.00MB

There was 1 failure:

1) SampleTest::testTrueAssertsToTrue
Failed asserting that false is true.

W:\w\git\phpunitbasics\tests\SampleTest.php:11

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

 

 


 

 

 

 


 

Tags