PHP 单元 | assertNotEquals()函数


PHP 单元 | assertNotEquals()函数

assertNotEquals()函数是 PHPUnit 中的内置函数,用于断言实际获得的值不等于预期值。如果预期值不等于实际值,则此断言将返回 true,否则返回 false。如果为真,则断言的测试用例通过,否则测试用例失败。

句法:

assertNotEquals( mixed $expected, mixed $actual, string $message = '' )

参数:此函数接受三个参数,如上述语法所示。参数说明如下:

  • $expected:此参数是代表预期数据的任何类型。
  • $actual:这个参数是代表实际数据的任何类型。
  • $message:此参数采用字符串值。当测试用例失败时,此字符串消息显示为错误消息。

下面的程序说明了 PHPUnit 中的 assertNotEquals()函数:

方案一:

<?php
use PHPUnit\Framework\TestCase;
  
class GeeksPhpunitTestCase extends TestCase
{
    public function testNegativeTestcaseForAssertNotEquals()
    {
        $expected = "geeks";
        $actual = "geeks";
  
        // Assert function to test whether expected
        // value is unequal to actual or not
        $this->assertNotEquals(
            $expected,
            $actual,
            "actual value is equals to expected"
        );
    }
}
  
?>

输出:

PHPUnit 8.2.5 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 67 ms, Memory: 10.00 MB

There was 1 failure:

1) GeeksPhpunitTestCase::testNegativeTestcaseForAssertNotEquals
actual value is equals to expected
Failed asserting that 'geeks' is not equal to 'geeks'.

/home/shivam/Documents/geeks/phpunit/abc.php:15

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

方案二:

<?php
use PHPUnit\Framework\TestCase;
  
class GeeksPhpunitTestCase extends TestCase
{
    public function testPositiveTestcaseForAssertNotEquals()
    {
        $expected = "geeks";
        $actual = "Geeks";
  
        // Assert function to test whether expected
        // value is unequal to actual or not
        $this->assertNotEquals(
            $expected,
            $actual,
            "actual value is not equals to expected"
        );
    }
}
  
?>

输出:

PHPUnit 8.2.5 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 67 ms, Memory: 10.00 MB

OK (1 test, 1 assertion)

注意:要使用 phpunit 运行测试用例,请按照此处的步骤操作。此外,phpunit 7 及更高版本支持 assertNotEquals()。


Made with ❤️ in Chengdu. Copyright reserved 2019-2022.

蜀ICP备20006366号-1