The assertion is a boolean expression at a specific point in the program which will be true and it will match the expected and actual value in Unit Testing when the expected and actual values are the same the test will pass and fail when the result is not the same.
There are different types of assertions in Unit Testing. We have described some of the assertion techniques below.
Assertions using assertEquals() Function :
The assertEquals() is a built-in function in PHPUnit and it is assert whether the actual obtained value is equal to the expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false.
<?php namespace namespace Webkul\UnitTest\Test\Unit\Helper; class DataTest extends \PHPUnit\Framework\TestCase { public function testcaseForAssertEquals() { $expected = "Webkul"; $actual = "Webkul"; // Assert function to test whether expected // value is equal to actual or not $this->assertEquals( $expected, $actual, "actual value is equals to expected" ); } }
Assertions using assertSame() Function :
The assertSame() function is a built-in function in PHPUnit and it is assert whether the actually obtained value is the same as the expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false.
<?php namespace namespace Webkul\UnitTest\Test\Unit\Helper; class DataTest extends \PHPUnit\Framework\TestCase { public function testcaseForAssertSame() { $expected = "Webkul"; $actual = "Webkul"; // Assert function to test whether expected // value is equal to actual or not $this->assertSame( $expected, $actual, "actual value is equals to expected" ); } }
Parameters: This function accepts three parameters as shown in the above syntax. Description of parameters are below :
- $expected: This parameter is of any type which represents the expected data.
- $actual: This parameter is of any type which represents the actual data.
- $message: This parameter takes a string value. This string message comes as an error when the test will failed.
Assertions using assertContains() Function :
The assertContains() function is a built-in function in PHPUnit and it is asserting an array having a value. This assertion will return true in the case if the array contains the provided value else return false and in the case of true the asserted test case got passed else the test case failed.
<?php namespace namespace Webkul\UnitTest\Test\Unit\Helper; class DataTest extends \PHPUnit\Framework\TestCase { public function testcaseForAssertContains() { $testArray = array("a"=>"value a", "b" =>"value b"); $value = "value b"; // assert function to test whether 'value' is a value of array $this->assertContains( $value, $testArray, "testArray contains value as value" ) ; } }
Parameters: This function accepts three parameters as shown in the above syntax. Description of parameters are below :
- $value: This parameter represents the value contained in the array.
- $testArray: This parameter is an array for which the assert function will check whether it contains a value or not.
- $message: This parameter takes a string value. This string message comes as an error when the test will failed.
<?php namespace namespace Webkul\UnitTest\Test\Unit\Helper; class DataTest extends \PHPUnit\Framework\TestCase { public function testcaseForAssertContains() { $testArray = array(1, 2, 3); // Assert function to test whether testArray contains // same number of elements as expectedCount $expectedCount = 3; $this->assertCount( $expectedCount, $testArray, "testArray contains 3 elements" ); } }
Parameters: This function accepts three parameters as shown in the above syntax. Description of parameters are below :
- $expectedCount: The parameter is the expected count of value in an array.
- $testArray: This parameter is the array for which the assert function will check whether it contains the same number of elements as the given count value or not.
- $message: This parameter takes a string value. This string message comes as an error when the test will failed.
Assertions using assertArrayHasKey() Function :
The assertArrayHasKey() function is a built-in function in PHPUnit and is used to assert whether an array has a particular key or not. This assertion will return true in the case if the array has the provided key else return false.
<?php namespace namespace Webkul\UnitTest\Test\Unit\Helper; class DataTest extends \PHPUnit\Framework\TestCase { public function testcaseForArrayHasKey() { // array to be tested $array = array('test' => 'webkul'); // assert function to test whether 'test' is a key of array $this->assertArrayHasKey( 'test', $array, "Array contains 'test' as key" ); } }
Parameters: This function accepts three parameters as shown in the above syntax. Description of parameters are below :
- test: This parameter is the expected key of the array.
- $array: This parameter is the array for which the assert function will check whether it contains the same key of elements as the given key value or not.
- $message: This parameter takes a string value. This string message comes as an error when the test will failed.
Assertions using assertArraySubset() Function :
The assertArraySubset() function is a built-in function in PHPUnit and it is assert an array having a subset. his assertion will return true in the case if the array contains the provided subset or else return false. In the case of true the asserted test case got passed else test case got failed.
<?php namespace namespace Webkul\UnitTest\Test\Unit\Helper; class DataTest extends \PHPUnit\Framework\TestCase { public function testcaseForArrayHasSubset() { $array = array('test' => 'webkul', 'test1' => 'noida'); $subset = array('test' => 'webkul'); // assert function to test whether 'subset' is a subset of array $this->assertArraySubset( $subset, $array, $flag = false, "array contains subset as subset" ) ; } }
Parameters: This function accepts three parameters as shown in the above syntax. Description of parameters are below :
- $subset: This parameter represent the subset to be present in an array.
- $array: This parameter is an array for which the assert function will check whether the provided subset is available or not.
- $flag: This parameter is a flag and it will compare the identity of objects within arrays.
- $message: This parameter takes a string value. This string message comes as an error when the test will failed.
Assertions using assertTrue() Function :
The assertTrue() is a PHPUnit built-in function. It is reporting an error according to the condition.
<?php namespace namespace Webkul\UnitTest\Test\Unit\Helper; class DataTest extends \PHPUnit\Framework\TestCase { public function testcaseForAssertTrue() { // assert function $this->assertTrue(true) ; } }
Assertions using assertFalse() Function :
The assertFlase() is a PHPUnit built-in function. It is reporting an error according to the condition.
<?php namespace namespace Webkul\UnitTest\Test\Unit\Helper; class DataTest extends \PHPUnit\Framework\TestCase { public function testcaseForAssertFalse() { // assert function $this->assertFalse(false); } }
Assertions using assertFileEquals() Function :
The assertFileEquals() reports an error identified by message
if the file specified by expected
does not have the same contents as the file specified by actual
.
<?php namespace namespace Webkul\UnitTest\Test\Unit\Helper; class DataTest extends \PHPUnit\Framework\TestCase { public function testcaseForAssertFileEquals() { // assert function for two files are equal or not $this->assertFileEquals('/home/sb/expected', '/home/sb/actual'); } }
Assertions using assertFileExists() Function :
The assertFileExists() reports an error identified by message
if the file specified by filename does not exist.
<?php namespace namespace Webkul\UnitTest\Test\Unit\Helper; class DataTest extends \PHPUnit\Framework\TestCase { public function testcaseForAssertFileExists() { // assert function for two files are equal or not $this->assertFileExists('path to file'); } }
These are some important assertions that we used in Magento for Unit Testing.
Thanks for reading this. If you have any queries please comment below.
Be the first to comment.