Introducción a PHPUnit
-
Upload
alicantephp -
Category
Documents
-
view
1.541 -
download
0
Transcript of Introducción a PHPUnit
![Page 1: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/1.jpg)
![Page 2: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/2.jpg)
![Page 3: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/3.jpg)
Introducción a PHPUnit
¿Qué es PHPUnit?
> Estándar de-facto para tests unitarios en proyectos PHP.
Nos proporciona un framework para:
> Escribir los tests de una manera sencilla.
> Ejecutarlos con facilidad.
> Analizar los resultados.
![Page 4: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/4.jpg)
Introducción a PHPUnit
¿De dónde viene?
> Pertenece a la familia de frameworks de xUnit (escrito por Kent Beck, @KentBeck - JUnit en Java).
Creador
> Sebastian Bergmann, @s_bergmann
Código
> GitHub → https://github.com/sebastianbergmann/phpunit/
![Page 5: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/5.jpg)
Introducción a PHPUnit
Instalación con PEAR
PHP Extension and Application Repository http://pear.php.net/
> $ pear upgrade PEAR
> $ pear config-set auto_discover 1
> $ pear install pear.phpunit.de/PHPUnit
![Page 6: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/6.jpg)
Introducción a PHPUnit
Instalación con Composer
Dependency Manager for PHP http://getcomposer.org/
> composer.json
{ "require": { "phpunit/phpunit": "3.7.*" }, "config": { "bin-dir": "/usr/local/bin/" } }
> $ composer install|update (php composer.phar install|update)
![Page 7: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/7.jpg)
Introducción a PHPUnit
Instalación con Phar
PHP Compressed files http://php.net/manual/es/book.phar.php
> $ wget http://pear.phpunit.de/get/phpunit.phar > $ chmod +x phpunit.phar
¿Cómo comprobamos que está instalado?
> $ phpunit --version PHPUnit 3.7.18 by Sebastian Bergmann.
![Page 8: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/8.jpg)
Introducción a PHPUnit
Mi primer test
> Creamos una clase MyFirstTest (MyFirstTest.php)
<?php
class MyFirstTest extends PHPUnit_Framework_TestCase {
public function testAlwaysOK() { $this->assertNull(null); }
}
> $ phpunit --colors MyFirstTest
![Page 9: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/9.jpg)
Introducción a PHPUnit
¿Cómo pasar el test?
> $ phpunit MyFirstTest
PHPUnit 3.7.18 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 1.25Mb
OK (1 test, 1 assertion)
![Page 10: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/10.jpg)
Introducción a PHPUnit
PHPUnit command-line tool
. F E S I
![Page 11: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/11.jpg)
Introducción a PHPUnit
Demo de ejemplo (DemoTest.php)
> Asserts > Anotaciones > Data providers
![Page 12: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/12.jpg)
Introducción a PHPUnit
Test Suite usando configuración XML
> config.xml
<phpunit> <testsuites> <testsuite name="AlicantePHP"> <file>DemoTest.php</file> </testsuite>
<testsuite name="Basic"> <file>MyFirstTest.php</file> </testsuite> </testsuites> </phpunit>
> $ phpunit --configuration config.xml --testsuite AlicantePHP > $ phpunit --configuration config.xml --testsuite Basic
![Page 13: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/13.jpg)
Introducción a PHPUnit
Conclusión
> Testing: buenas prácticas. > Equipos de trabajo grandes.
> Tranquilidad.
> Confianza para hacer cambios.
> Calidad en el código.
> No es gratis, cuesta desarrollar test y cuesta más desarrollar buenos Tests. ...
![Page 14: Introducción a PHPUnit](https://reader036.fdocuments.mx/reader036/viewer/2022071814/55a790881a28ab29478b46a7/html5/thumbnails/14.jpg)
Introducción a PHPUnit
muchas gracias