TEST-DRIVEN-DEVELOPMENT WITH PHPUnit

INTRODUCTION

Image for post
$ sudo apt install phpunit

TESTING A NEW CLASS

$ mkdir  Ferro; 
$ mkdir Ferro/includes; mkdir Ferro/includes/classes
$ mkdir Ferro/tests; mkdir Ferro/tests/classes
$ cd Ferro$ vim tests/classes/FerroTest.php
<?php
/**
* testing class Ferro
*/
// include our class arquive for test
require ("includes/classes/Ferro.php");
/**
* @covers Ferro
*/
final class FerroTest extends PHPUnit_Framework_TestCase
{
public function testNew()
{
$this->assertInstanceOf(
Ferro::class,
new Ferro
);
}
}
?>
$ touch includes/classes/Ferro.php
$ phpunit tests/classes/FerroTest.phpPHPUnit 5.1.3 by Sebastian Bergmann and contributors.E 1 / 1 (100%)Time: 33 ms, Memory: 4.00MbThere was 1 error:1) FerroTest::testNew
Error: Class 'Ferro' not found
/home/filipo/Ferro/tests/classes/FerroTest.php:18FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
$ vi includes/classes/Ferro.php
<?php
/**
* class Ferro
*/
class Ferro
{ } // end of Ferro
?>
$ phpunit testes/classes/FerroTest.php 
PHPUnit 5.1.3 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)Time: 70 ms, Memory: 4.00MbOK (1 test, 1 assertion)
em PHP