Open In App

PHPUnit assertGreaterThanOrEqual() Function

The assertGreaterThanOrEqual() function is a builtin function in PHPUnit and is used to assert whether the actually obtained value is greater than or equal to the expected value or not. This assertion will return true in the case if the actual value is greater than or equal to the expected value else returns false. In case of true the asserted test case got passed else test case got failed.

Syntax:



assertGreaterThanOrEqual(mixed $expected, mixed $actual
[, string $message = ''])

Parameters: This function accepts three parameters as mentioned above and described below:

Below examples illustrate the assertGreaterThanOrEqual() function in PHPUnit:



Example 1:




<?php 
use PHPUnit\Framework\TestCase; 
    
class GeeksPhpunitTestCase extends TestCase 
    public function testNegativeForassertGreaterThanOrEqualWithEqualVal()
    {    $expected = 22; 
        $actual = 10;
        // Assert function to test whether actual 
        // value is greater than or equal to expected or not 
        $this->assertGreaterThanOrEqual( 
            $expected
            $actual
            "actual value is greater than or equal to expected"
        );
          
    }
   
  
      
    public function testNegativeForassertGreaterThanOrEqualWithGreaterVal()
    {  
     $expected = 22; 
        $actual = 10;
        // Assert function to test whether actual
        // value is greater than or equal to expected or not 
          
        $this->assertGreaterThanOrEqual( 
            $expected
            $actual
            "actual value is greater than or equal to expected"
        ); 
    }  
 
?> 

Output:

 PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

FF                                                              2 / 2 (100%)

Time: 86 ms, Memory: 10.00 MB

There were 2 failures:

1) GeeksPhpunitTestCase::testNegativeForassertGreaterThanOrEqualWithEqualVal
actual value is greater than or equal to expected
Failed asserting that 10 is equal to 22 or is greater than 22.

/home/lovely/Documents/php/test.php:15

2) GeeksPhpunitTestCase::testNegativeForassertGreaterThanOrEqualWithGreaterVal
actual value is greater than or equal to expected
Failed asserting that 11 is equal to 22 or is greater than 22.

/home/lovely/Documents/php/test.php:32

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

Example 2:




<?php 
use PHPUnit\Framework\TestCase; 
    
class GeeksPhpunitTestCase extends TestCase 
    public function testPositiveForassertGreaterThanOrEqualWithEqualVal()
    {    $expected = 11; 
        $actual = 11;
        // Assert function to test whether actual
        // value is greater than or equal to expected or not 
        $this->assertGreaterThanOrEqual( 
            $expected
            $actual
            "actual value is greater than or equal to expected"
        );
          
    }
   
  
      
    public function testPositiveForassertGreaterThanOrEqualWithGreaterVal()
    {  
     $expected = 11; 
        $actual = 12;
          // Assert function to test whether actual
        // value is greater than or equal to expected or not 
          
        $this->assertGreaterThanOrEqual( 
            $expected
            $actual
            "actual value is greater than or equal to expected"
        ); 
    }  
 
?> 

Output:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

..                                                  2 / 2 (100%)

Time: 87 ms, Memory: 10.00 MB

OK (2 tests, 4 assertions)

Reference:https://phpunit.readthedocs.io/en/9.2/assertions.html#assertgreaterthanorequal


Article Tags :