22/10/2017 - PHPUNIT
Bu uygulamada örnek bir uygulama içinde PHPUnit kurup, testleri çalıştıracağız.
project
|-- src
|---- Application
|------ Service
|-------- TwitterService.php
|-- tests
|---- Application
|------ Service
|-------- TwitterServiceTest.php
|-- vendor
|---- ...
|-- composer.lock
|-- composer.json
|-- phpunit.xml
PHPUnit paketini kurmak için $ composer require phpunit/phpunit --dev
komutunu çalıştırın.
Aşağıdaki dosyayı projenin ana klasörüne phpunit.xml
olarak kaydedin.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="ApplicationSuite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
Test takımınızın ismi ApplicationSuite
ve test dosyalarının kaydedileceği yer ise projenin ana klasöründeki tests
klasörüdür.
Elimizde aşağıdaki test classının olduğunu varsayalım.
namespace tests\Application\Service;
use PHPUnit\Framework\TestCase;
class TwitterServiceTest extends TestCase
{
public function testSetMessage()
{
}
public function testGetMessage()
{
}
}
Tüm testleri çalıştırmak.
$ vendor/bin/phpunit
Tüm testleri takım ismiyle çalıştırmak.
$ vendor/bin/phpunit --configuration phpunit.xml --testsuite ApplicationSuite
Belirli bir klasördeki tüm testleri çalıştırmak
$ vendor/bin/phpunit tests/Application/Service
Tek bir class içindeki sadece bir test methodunu çalıştırmak. Not: Eğer test class içinde testSetMessage
ile başlayan başka bir test methodu varsa, PHPUnit onu da çalıştırır. Örnek: testSetMessageAnother
$ vendor/bin/phpunit --filter testSetMessage TwitterServiceTest tests/Application/Service/TwitterServiceTest.php
Tek bir class içindeki tüm test methodlarını çalıştırmak.
$ vendor/bin/phpunit --filter TwitterServiceTest tests/Application/Service/TwitterServiceTest.php