Testing with PHPUnit and Codeception
This repository only for learning testing. You can find original links below:
All about testing tutorial: http://www.elisdn.ru/blog/78/yii2-codeception-testing
Code of this testing tutorial: https://github.com/ElisDN/webinar-tests
REQUIREMENTS
- PHP >= 5.4 with modules
- SQLite
INSTALLATION
- Got to working directory:
cd path_to_working_directory
- Install Yii2:
composer require yiisoft/yii2
RUN CUSTOM TESTS
Extend class from \tests\TestCase
Execute following for run test:
php test/app
Dealing with real data:
./yii migrate
Dealing with test data:
php tests/bin/yii migrate
Run PHPUnit Tests
- Install phpunit:
composer require phpunit/phpunit
- Add configuration file: phpunit.xml to project root
- Extend test classes from \PHPUnit\Framework\TestCase
- Execute command for run tests:
./vendor/bin/phpunit
Run PHPUnit Test through PHPStorm
-
Set PHP interpreter in settings: File->Settings->PHP and set need version.
-
Download phpunit.phar file
-
Set PHPUnit settings: File->Settings->PHP->PHPUnit and choose Path to PHPUnit.phar and set it. Then choose phpunit.xml config for project.
-
Edit configuration-> + -> PHPUnit. Rename to Unit, set config file if need.
-
Choose Unit and click to green arrow.
DbUnit
- Install
composer require phpunit/dbunit
-
Extend Test class from PHPUnit\DbUnit\TestCase
-
Realize abstract methods:
public function getConnection()
{
$pdo = new \PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
return $this->createDefaultDBConnection($pdo, $GLOBALS['DB_DBNAME']);
}
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__).'/../_data/users.xml');
}
Code coverage
- Add whitelist to phpunit.xml and add directory for checking code coverage
<whitelist>
<directory suffix=".php">./models</directory>
</whitelist>
- Execute command:
./vendor/bin/phpunit --coverage-html ./report
This command will generate report to directory ./report
Codeception
- Prepare. Remove all new versions of phpunit and dbunit
composer remove phpunit/dbunit
composer remove phpunit/phpunit
composer require phpunit/phpunit:"~4.8"
composer require phpunit/dbunit:"~1.4"
In your composer.json file you should see:
"phpunit/phpunit": "~4.8",
"phpunit/dbunit": "~1.4",
- Installation
composer require "codeception/codeception=2.1.*"
composer require "codeception/specify=*"
composer require "codeception/verify=*"
- Checking is codeception work
./vendor/bin/codecept
- Delete ./test folder and ./phpunit.xml
- Create configuration
./vendor/bin/codecept bootstrap
-
Copy ./tests/bin/yii from previous deleted version, add configuration in ./tests/config directory
-
Generate UserTest:
./vendor/bin/codecept generate:test unit UserTest
- Copy non DbUnit methods from previous UserTest class.
- Build
./vendor/bin/codecept build
- Run it
./vendor/bin/codecept run
./vendor/bin/codecept run unit
- Add Db module
- Add Db module to codeception.yml
- Add Db to unit.suit.yml
- add SQL to tests/d_data/dump.sql
- delete Db changing in _before()
See commit: 18371e765242d80766ab15633fa3eee63ce8dcc6