UNIT TESTING
Learn to Survive
Agenda
1. Introduction
2. Hierarchy
3. Installation & configuration
4. Directory Structure
5. Writing & Running Unit Test
6. Assertions
7. Annotations
Installation
● composer require --dev phpunit/phpunit ^7
● Other methods -> pear , wget
Configuration : The XML WAY
<phpunit>
<testsuites> </testsuites>
</phpunit>
Directory Structure
Project |--
|--src
|--tests
|--unit
|--integration
index.php
.gitignore
.env
Writing Unit Test
● Class must extends PHPUnitFrameworkTestCase class
● Class Name must suffixed with “Test” e.g. NumberTest
● All testing function must be prefixed with “test” e.g. function testAdd()
● All testing functions must be public.
Writing Unit Test : Sample Source Code
class Add {
public function add($x, $y) {
return $x+$y;
}
}
Writing Unit Test : Actual Test
class AddTest extends PHPUnitFrameworkTestCase {
public function testAdd(){
$no1 = 1;
$no2 = 2;
$addObj = new Add;
$result = $addObj->add($no1, $no2);
$this->assertEquals( 3, $result);
}
}
Running Test
#phpunit <test dir>/<testfile.php --options
$ vendor/bin/phpunit tests/Add.php --colors --bootstrap=”vendor/autoload.php”
Php Unit Characters
. Printed when the test succeeds.
F Printed when an assertion fails while running the test
method.
E Printed when an error occurs while running the test
method.
R Printed when the test has been marked as risky
S Printed when the test has been skipped
Assertions
PHPUnit's assertions are implemented in PHPUnitFrameworkAssert.
PHPUnitFrameworkTestCase inherits from PHPUnitFrameworkAssert.
● assertTrue
● assertFalse
● assertEquals - ignore data type
● assertSame - checks data type
● assertInstanceOf
○ - e.g. $this->assertInstanceOf(Add::class, $addObj);
● assertEmpty
● assertNull
● assertFileExists
...
Annotations
An annotation is a special form of syntactic metadata that can be added to the source
code of some programming languages.
● @depends - depends on output of the other function
● @dataProvider - depends on test cases
Some Best Practices
Best practices in Unit Testing are:
· Unit Test cases should be independent. In case of any enhancements or change in
requirements, unit test cases should not be affected.
· Test only one code at a time.
· Follow clear and consistent naming conventions for your unit tests
· In case of change in code in any module, ensure there is a corresponding unit test case
for the module and the module passes the tests before changing the implementation
· Bugs identified during unit testing must be fixed before proceeding to the next phase in
SDLC
· Adopt a "test as your code" approach.
Unit Testing in Action
Github repo
Time for Question(s)
References
● https://phpunit.de
● https://phpunit.readthedocs.io
Unit testing

Unit testing

  • 1.
  • 2.
    Agenda 1. Introduction 2. Hierarchy 3.Installation & configuration 4. Directory Structure 5. Writing & Running Unit Test 6. Assertions 7. Annotations
  • 3.
    Installation ● composer require--dev phpunit/phpunit ^7 ● Other methods -> pear , wget
  • 4.
    Configuration : TheXML WAY <phpunit> <testsuites> </testsuites> </phpunit>
  • 5.
  • 6.
    Writing Unit Test ●Class must extends PHPUnitFrameworkTestCase class ● Class Name must suffixed with “Test” e.g. NumberTest ● All testing function must be prefixed with “test” e.g. function testAdd() ● All testing functions must be public.
  • 7.
    Writing Unit Test: Sample Source Code class Add { public function add($x, $y) { return $x+$y; } }
  • 8.
    Writing Unit Test: Actual Test class AddTest extends PHPUnitFrameworkTestCase { public function testAdd(){ $no1 = 1; $no2 = 2; $addObj = new Add; $result = $addObj->add($no1, $no2); $this->assertEquals( 3, $result); } }
  • 9.
    Running Test #phpunit <testdir>/<testfile.php --options $ vendor/bin/phpunit tests/Add.php --colors --bootstrap=”vendor/autoload.php”
  • 10.
    Php Unit Characters .Printed when the test succeeds. F Printed when an assertion fails while running the test method. E Printed when an error occurs while running the test method. R Printed when the test has been marked as risky S Printed when the test has been skipped
  • 11.
    Assertions PHPUnit's assertions areimplemented in PHPUnitFrameworkAssert. PHPUnitFrameworkTestCase inherits from PHPUnitFrameworkAssert. ● assertTrue ● assertFalse ● assertEquals - ignore data type ● assertSame - checks data type ● assertInstanceOf ○ - e.g. $this->assertInstanceOf(Add::class, $addObj); ● assertEmpty ● assertNull ● assertFileExists ...
  • 12.
    Annotations An annotation isa special form of syntactic metadata that can be added to the source code of some programming languages. ● @depends - depends on output of the other function ● @dataProvider - depends on test cases
  • 13.
    Some Best Practices Bestpractices in Unit Testing are: · Unit Test cases should be independent. In case of any enhancements or change in requirements, unit test cases should not be affected. · Test only one code at a time. · Follow clear and consistent naming conventions for your unit tests · In case of change in code in any module, ensure there is a corresponding unit test case for the module and the module passes the tests before changing the implementation · Bugs identified during unit testing must be fixed before proceeding to the next phase in SDLC · Adopt a "test as your code" approach.
  • 14.
    Unit Testing inAction Github repo
  • 15.
  • 16.