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 will be 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 assertions techniques below.
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. when the asserted value is the true test case will pass or else fail.
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. This string message comes as an error when the test will failed.
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.
assertContains() Function :
The assertContains() function is a built-in function in PHPUnit and it is assert 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 test case got failed.
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.
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.
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 and in the case of true the asserted test case got passed else test case got failed.
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.
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.
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.
assertTrue() Function :
The assertTrue() is a PHPUnit built-in function. It is reporting an error according to the condition.
assertFalse() Function :
The assertFlase() is a PHPUnit built-in function. It is reporting an error according to the condition.
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
.
assertFileExists() Function :
The assertFileExists() reports an error identified by message
if the file specified by filename does not exist.
These are some important assertions that we used in Magento for Unit Testing.
Thank’s for reading this. If you have any queries please comment below.