Skip to content
Related Articles

Related Articles

Improve Article

PHPUnit assertGreaterThanOrEqual() Function

  • Last Updated : 07 Aug, 2020

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:

  • $expected: This parameter is of any type of numeric value which represents the expected data.
  • $actual: This parameter is of any type of numeric value which represents the actual data.
  • $message: This parameter takes a string value.  When the test case got failed this string message got displayed as an error message.

Below examples illustrate the assertGreaterThanOrEqual() function in PHPUnit:

Example 1:



PHP




<?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




<?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




My Personal Notes arrow_drop_up
Recommended Articles
Page :