3. ACCEPTANCE TESTING
This scenario can be performed either
by a
simple PHP browser or by a browser
with
Selenium WebDriver.
Acceptance testing can be
performed by a non-technical person.
You can reproduce a
AcceptanceTester‘s actions in
scenarios and run them
automatically after each site change.
Generated scenarios will be stored in
your _data directory in text files.
4. PHP BROWSER
This is the fastest way to run acceptance tests, since it doesn't require running an
actual browser. We use a PHP web scrapper, which acts like a browser:
it sends a request, then receives and parses the response.
Before we start we need a local copy of
the site running on your host.
We need to specify the url parameter in the
acceptance suite config
(tests/acceptance.suite.yml).
Get Start
5. start by creating a 'Cept' file in the tests/acceptance directory.
Let's call it SigninCept.php
The wantTo section describes
your scenario in brief.
The $I object is used to write all
interactions.
The methods of the $I object are
taken from thePHPBrowser and Db
modules.If you have ever written a BDD scenario in Gherkin,
you can write a classic feature story:
6. CLICK
Emulates a click on valid anchors
As a parameter you can specify
the link name or a valid CSS or
XPath selector.
You can specify locator type manually
by passing array as a parameter.
We call this a strict locator. Available strict locator types are:
• id
• name
• css
• xpath
• link
• class
7. SEELINK
Before clicking the link you can
perform a check if the link really
exists on a page. This can be
done by the seeLink action
8. FORMS
Let's submit this sample form inside the Codeception test.
Rewitten scenarioTesting scenario
9. AJAX EMULATION
PHP browser can't process JavaScript. Still, all the ajax calls can be easily
emulated
by sending the proper requests to the server.
10. ASSERTIONS
Test the page contents
The most useful command for this is see.
check that specific element exists (or not) other useful commands to perform checks.
Please note that they all start with the see prefix.
11. CONDITIONAL ASSERTIONS
Sometimes you don't want the test to be stopped when an assertion fails
Maybe you have a long-running test and you want it to run to the end
REPLACEMENT :
see => canSee
dontSee => cantSee
12. GRABBERS
These commands retrieves data that can be used in test.
Imagine, your site generates a password for every user and you want to
check the user can log into the site using this password.
Grabbers allow you to get a single value from the current page with
commands.
15. SELENIUM WEBDRIVER
Most scenarios can be easily ported between the testing backends
Your PhpBrowser tests we wrote previously can be executed inside a
real browser (or even PhantomJS) with Selenium WebDriver.
Change
reconfigure and rebuild the AcceptanceTester class,
to use WebDriver instead of PhpBrowser.
16. SELENIUM WEBDRIVER
In order to run Selenium tests you need to download Selenium Server and
get it running (Alternatively you may use PhantomJS headless browser
in ghostdriver mode).
If you run acceptance tests with Selenium, Firefox will be started and all actions
will
be performed step by step using browser engine.
17. In this case seeElement won't just check that the element exists on a page, but it
will
also check that element is actually visible to user.
18. WAIT
While testing web application, you may need to wait for JavaScript events
to occur.
Due to its asynchronous nature, complex JavaScript interactions are hard
to test.
19. MULTI SESSION TESTING
Codeception allows you to execute actions in concurrent session. The
most obvious
case for it - testing realtime messaging between users on site.
In order to do it you will need to launch 2 browser windows in a same time
for the
same test. Codeception has very smart concept for doing this. It is called
Friends.
20. CLEANING THINGS UP
While testing, your actions may change the data on the site. Tests will fail if
trying to
create or update the same data twice.
To avoid this problem, your database should be repopulated for each test.
To make repopulation work, create an sql dump of your database and put it
into the
/tests/_data directory
21. DEBUGGING
Codeception modules can print valuable information while running.
Just execute tests with the --debug option to see running details.
For any custom output use codecept_debug function.
On each fail, the snapshot of the last shown page will be stored in the
tests/_log
directory.
PHPBrowser will store html code and WebDriver will save the screenshot of a
page.
22. ACCEPTANCE TESTING
Selenium WebDriver, PhpBrowser.
Data cleanup.
Continuous Integration.
Remote CodeCoverage.
Symfony2, Laravel, Yii, Phalcon,
Zend Framework, Kohana,
Databases, REST, SOAP, CodeCoverage
FUNCTIONAL TESTING
API TESTING
REST, SOAP, XML-RPC
via PHPBrowser or PHP
Frameworks.
23. FUNCTIONAL TESTS
Functional tests don't require a web server to run tests.
In simple terms we set $_REQUEST, $_GET and$_POST variables then we
execute
application from a test.
Modules for all of these frameworks share the same interface, and thus
your tests
are not bound to any one of them. This is a sample functional test.
As you see you can use same tests for functional and acceptance testing.
24. PITFALLS
less stable
Headers, Cookies, Sessions
One of the common issues problems with functional tests are usage of PHP
functions
that deal with headers, sessions, cookies.
For instance, Header function triggers an error if it is executed more then once.
In functional tests, we run application multiple times thus, we will get lots of
trash errors in
the result.
Shared Memory
PHP application does not stop after it finished processing a request
As all requests run in one memory container they are not isolated.
So if you see that your tests are mysteriously failing when they shouldn't –
try to execute a single test.
25. ENABLING FRAMEWORK MODULES
Symfony2
Just need to include the Symfony2 module into your test suite.
If you also use Doctrine2, don't forget to include it either.
By default this module will search for App Kernel in the app directory.
The module uses the Symfony Profiler to provide additional information
and assertions.
26. LARAVEL 4
YII
YII2
Yii2 tests are included in Basic and Advanced application templates.
Follow Yii2 guides to start.
By itself Yii framework does not have an engine for functional testing.
So Codeception is the first and only functional testing framework for Yii.
To use it with Yii include Yii1 module into config.
27. Zend Framework 2
Zend Framework 1.x
The module for Zend Framework is highly inspired by ControllerTestCase
class, used
for functional testing with PHPUnit.
It follows similar approaches for bootstrapping and cleaning up.
To start using Zend Framework in your functional tests, include the ZF1
module.
28. Phalcon 1.x
Phalcon1 module requires creating bootstrap file which returns instance of
PhalconMvcApplication.
To start functional tests with Phalcon you should enable Phalon1 module
and provid
path to this bootstrap file:
29. WRITING FUNCTIONAL TESTS
Functional tests are written in the same manner as Acceptance Tests with
PhpBrowser module enabled.
Click
submit form
31. ERROR REPORTING
Uses E_ALL & ~E_STRICT & ~E_DEPRECATED error reporting value
In functional tests you might want to change this values depending on
framework's error policy.
error_level can be set globally in codeception.yml file.
32. ACCEPTANCE TESTING
Selenium WebDriver, PhpBrowser.
Data cleanup.
Continuous Integration.
Remote CodeCoverage.
Symfony2, Laravel, Yii, Phalcon,
Zend Framework, Kohana,
Databases, REST, SOAP, CodeCoverage
FUNCTIONAL TESTING
API TESTING
REST, SOAP, XML-RPC
via PHPBrowser or PHP
Frameworks.
33. UNIT TESTS
Codeception uses PHPUnit as a backend for running tests. Thus, any
PHPUnit test
can be added to Codeception test suite and then executed.
If you ever wrote a PHPUnit test then do it just as you did before.
Codeception adds
some nice helpers to simplify common tasks.
To say it again: you don't need to install PHPUnit to run its tests.
Codeception can run
them too.
34. CREATING TEST
Nice generators to simplify test creation.
start with generating a classical PHPUnit test extending
PHPUnit_Framework_TestCase class
Need another command to create Codeception-powered unit test
Both tests will create a new ExampleTest file located in tests/unit directory.
A test created by generate:test command will look like this:
35. Has predefined _before and _after methods to start with.
Use them to create a tested object before each test, and destroy it
afterwards.
36. BDD SPECIFICATION TESTING
For this case we have a stand-alone project Specify(included in phar
package) for writing specifications inside a unit test.
37. USING MODULES
As in scenario-driven functional or acceptance tests you can access actor
class
methods.
If you write integration tests, it may be useful to include Db module for
database
testing.
To access UnitTester methods you can use UnitTester property in a test.
38. TESTING DATABASE
Database will be cleaned and populated after each test, as it happens for
acceptance
and functional tests.
If it's not your required behavior, please change the settings of Db module
for current
suite.
39. ACCESSING MODULE
Codeception allows you to access properties and methods of all modules
defined for
this suite.
Unlike using the UnitTester class for this purpose, using module directly
grants you
access to all public properties of that module.
All public variables are listed in references for corresponding modules.
40. CEST
Alternatively to testcases extended from PHPUnit_Framework_TestCase
you may use
Codeception-specific Cest format.
It does not require to be extended from any other class.
All public methods of this class is a test.
The example above can be rewritten in scenario-driven manner like this:
41. For unit testing you may include Asserts module, that adds regular
assertions to
UnitTester which we access from $t variable.
42. STUBS
Codeception provides a tiny wrapper over PHPUnit mocking framework to
create stubs
easily. Include CodeceptionUtilStub to start creating dummy objects.
In this example we instantiate object without calling a constructor and
replace
getName method to return jon value.
Stubs are created with PHPUnit's mocking framework. Alternatively you can
use
Mockery (with Mockery module), AspectMock or others.