CodeCeptionintroduction and use in Yii
Yii London Meetup - 15 April 2014by Matteo ‘Peach’ Pescarin - @ilPeach
CodeCeption introduction and use in Yii
The current situation
(Potentially) fiddly system configurationunless
the framework ships a testing environmente.g.
db connection, access to magic functions, autoloading functionality,
...
CodeCeption introduction and use in Yii
Yet another tool?
NOPE
It’s been designed to ease the testing process.It’s meant to be extensible and modular.
Creates uniformity across different test suites.
Works on top of other well known technologies,e.g. PHPUnit, PHPBrowser, Selenium, etc...
CodeCeption introduction and use in Yii
Should you bother writing tests?
YES
CodeCeption introduction and use in Yii
Should you bother writing tests?
Yes, you really should.
And no, you don’t need to test everything. You need a QA strategy,
which comes with proper planningand a desire to avoid spending the weekend fixing bugs.
Unless you’re a maniac who loves to deliver buggier code in production.
CodeCeption introduction and use in Yii
What kind of tests?Acceptance High-level tests, can have no knowledge of the technologies used.
Testing done from the non-technical person PoV (called WebGuy): “uses the browser to test the website works correctly.”
Can be plugged into different testing suites: e.g. PHPBrowser, Selenium, Sahi,..
CodeCeption introduction and use in Yii
What kind of tests?Acceptance High-level tests, can have no knowledge of the technologies used.
Testing done from the non-technical person PoV (called WebGuy): “uses the browser to test the website works correctly.”
Can be plugged into different testing suites: e.g. PHPBrowser, Selenium, Sahi,..
Functional Mid-level tests. Covers functionality from the server perspective.The person testing (called TestGuy) knows how the application works, passes different $_GET, $_POST and $_REQUEST variables to ensure the functionality covers all known and corner cases.Simpler than Acceptance, does not need a webserver, uses PHPBrowser.
CodeCeption introduction and use in Yii
What kind of tests?Acceptance High-level tests, can have no knowledge of the technologies used.
Testing done from the non-technical person PoV (called WebGuy): “uses the browser to test the website works correctly.”
Can be plugged into different testing suites: e.g. PHPBrowser, Selenium, Sahi,..
Functional
Unit
Mid-level tests. Covers functionality from the server perspective.The person testing (called TestGuy) knows how the application works, passes different $_GET, $_POST and $_REQUEST variables to ensure the functionality covers all known and corner cases.Simpler than Acceptance, does not need a webserver, uses PHPBrowser.
Low-level tests. Single isolated tests.The person testing, CodeGuy, knows the internals of the application and tests database operations and anything else that might need proof of concept.Packages PHPUnit and provides a further abstraction over it to simplify its use.
CodeCeption introduction and use in Yii
Preliminary steps in Yii2using the Yii2-app-base, read /tests/README.md first:$ composer require --dev "codeception/codeception: 1.8.*@dev" \"codeception/specify: *" \"codeception/verify: *"
Then run the build script in order to populate the missing bits
$ vendor/bin/codecept buildBuilding Guy classes for suites: functional, acceptance, unitTestGuy includes modules: Filesystem, TestHelper, Yii2TestGuy.php generated successfully. 53 methods addedWebGuy includes modules: WebHelper, PhpBrowserWebGuy.php generated successfully. 48 methods addedCodeGuy includes modules: CodeHelperCodeGuy.php generated successfully. 1 methods added
CodeCeption introduction and use in Yii
Configure your entry URLsconfigure the TEST_ENTRY_URL variable in tests/_boostrap.php
$ grep TEST_ENTRY_URL tests/_bootstrap.php
defined('TEST_ENTRY_URL') or define('TEST_ENTRY_URL', '/web/index-test.php');$_SERVER['SCRIPT_NAME'] = TEST_ENTRY_URL;
Set the URL for the acceptance tests (based on the module you want to use)
$ grep -B1 url tests/acceptance.suite.yml PhpBrowser: url: 'http://sandbox/yii2-test/'# WebDriver:# url: 'http://localhost'
CodeCeption introduction and use in Yii
Implement and run the testsGenerate and implement the tests in the template given:$ vendor/bin/codecept generate:cept acceptance Homepage
Test was created in HomepageCept.php
$ vim tests/acceptance/HomepageCept.php
Run the tests!$ vendor/bin/codecept run
[snip]
OK (13 tests, 63 assertions)
$
CodeCeption introduction and use in Yii
Acceptance tests using PHPBrowser
BDD scenarios can be easily translated into acceptance tests, e.g.:
“As an account holderI want to be able to login
so I can check my dashboard”
CodeCeption introduction and use in Yii
A “practical” example
<?php
$I = new WebGuy($scenario);
$I->wantTo(‘login to check the dashboard’);
$I->amOnPage(‘/’);
$I->see(‘Yii2 test’);
$I->seeLink(‘login’, ‘site/login’);
$I->click(‘login’);
$I->see(‘Login’, ‘h1’);
// fillField() on the form
$I->click(‘login-button’);
$I->seeLink(‘Logout (admin)’);
$I->see(‘Admin dashboard’);
“As an account holderI want to be able to login
so I can check my dashboard”
CodeCeption introduction and use in Yii
Very similar to each other in terms of commands, but...Acceptance tests can run cross-browser compatibility checks using Selenium Webdriver, ZombieJS, etc
Functional are simpler and more straight forward to implement.Functional are good for testing APIs and REST interfaces.The Goutte engine in functional does not know how to JS!
Acceptance vs Functional tests
CodeCeption introduction and use in Yii
So what about Yii1?
❖ Functional tests using Selenium RC.❖ Unit tests using PHPUnit (via PEAR).
Since PHPUnit >= 3.6 and the Composer Revolution, things started to go awry.Yii’s autoloaders and the new PHPUnit’s don’t fit together.Cannot take full advantage of newest Selenium Webdriver.
CodeCeption introduction and use in Yii
Using CodeCeption in Yii1follow the CodeCeption quickstart guide (http://codeception.com/quickstart)
$ mkdir protected/vendor/bin && cd protected/vendor/bin
$ wget http://codeception.com/codecept.phar && chmod a+x codecept.phar
Initialise the directory structure
$ cd protected/ && vendor/bin/codecept.phar bootstrap
Initializing Codeception in /mnt/workspace/yii1-test/protected
[snip]
Bootstrap is done. Check out /mnt/workspace/yii1-test/protected/tests directory
$
CodeCeption introduction and use in Yii
Additional modules and configurationinstall the Yii1 CodeCeption Bridge (https://github.com/Codeception/YiiBridge)
$ git clone [email protected]:Codeception/YiiBridge.git tests/_data/YiiBridge
$ echo "require_once __DIR__.'/_data/YiiBridge/yiit.php';" >> tests/_bootstrap.php
Configure your tests/<type>.suite.yaml file(s) and add Yii1, configuring it:
class_name: MyGuy
modules:
enabled: [Yii1, Filesystem, MyHelper]
config:
Yii1:
appPath: '/mnt/workspace/yii1-test/index-test.php'
url: 'http://sandbox/yii1-test/index-test.php'
CodeCeption introduction and use in Yii
$ vendor/bin/codecept.phar buildBuilding Guy classes for suites: functional, acceptance, unit
Build and runRe-run the build script now that Yii1 has been setup.This is needed for any change made on the yaml files.
Create and implement your tests and run the suite(s)
$ vendor/bin/codecept.phar generate:cept functional HomepageTest was created in HomepageCept.php
$ vim tests/functional/HomepageCept.php
$ vendor/bin/codecept.phar run functional
CodeCeption introduction and use in Yii
Few notes on Unit tests
CodeCeption unit tests won’t be available, but PHPUnit:
$ vendor/bin/codecept.phar generate:phpunit unit LoginFormTest was created in /mnt/workspace/yii1-test/protected/tests/unit/LoginFormTest.php
When creating the tests you need to adjust the extended class to be:
class LoginFormTest extends CTestCase # or CDbTestCase
{
If using CDbTestCase, remember to call the parent classes’ in the setUp() and tearDown() methods to make fixtures work as expected.
CodeCeption introduction and use in Yii
Other cool stuff
❖ Interactive console❖ Grouping❖ Dependencies❖ Cest classes❖ PageObjects❖ StepObjects❖ Environments
CodeCeption introduction and use in Yii
Some live examples and Q&A
CodeCeption introduction and use in Yii
Now, go and test stuff!
and when in doubt:read the generated code (e.g. Guys, Pages, etc...)
check the documentation of CodeCeption:http://codeception.com
and its integration into Yii2:http://www.yiiframework.com/doc-2.0/ext-codeception-index.html
Thank you for listening!
Yii London Meetup - 15 April 2014
Matteo ‘Peach’ Pescarin @ilPeach
http://peach.smartart.it
Top Related