assertequals() php,PHP PHPUnit assertNotEquals()用法及代码示例

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

用法:

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

参数:该函数接受三个参数,如上面的语法所示。参数说明如下:

$expected:该参数可以是代表期望数据的任何类型。

$actual:此参数可以是代表实际数据的任何类型。

$message:此参数采用字符串值。当测试用例失败时,此字符串消息将显示为错误消息。

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

程序1:

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.

程序2:

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()。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值