廖泽华 a37f2bbff5 init code | 4 gadi atpakaļ | |
---|---|---|
.. | ||
angular | 4 gadi atpakaļ | |
cli | 4 gadi atpakaļ | |
coverage | 4 gadi atpakaļ | |
data | 4 gadi atpakaļ | |
4 gadi atpakaļ | ||
log | 4 gadi atpakaļ | |
support | 4 gadi atpakaļ | |
unit | 4 gadi atpakaļ | |
web | 4 gadi atpakaļ | |
README.md | 4 gadi atpakaļ | |
angular.suite.yml | 4 gadi atpakaļ | |
cli.suite.yml | 4 gadi atpakaļ | |
coverage.suite.yml | 4 gadi atpakaļ | |
docker-compose.yml | 4 gadi atpakaļ | |
facebook.suite.yml | 4 gadi atpakaļ | |
unit.suite.yml | 4 gadi atpakaļ | |
web.suite.yml | 4 gadi atpakaļ |
In case you submit a pull request, you will be asked for writing a test.
There are 3 suites for testing
composer install
in the cloned project directoryTo run the web tests:
php -S 127.0.0.1:8000 -t tests/data/app
php codecept run web --env chrome
The most important tests in this suite are Module tests located in test/unit/Codeception/Module
. Unlike you would expect, most of tests there are integrational tests. For example, WebDriverTest
requires an actual Selenium Server to be running.
These are basic steps if you want to add a test for a module:
tests/unit/Codeception/Module
Requirements:
When a module requires a web server with the demo application running, you can find this app in tests/data/app
. To execute tests for PhpBrowser or WebDriver you need to start a web server for this dir:
php -S 127.0.0.1:8000 -t tests/data/app
If you run FrameworkTest
for various frameworks, you don't need a web server running.
It is a very basic PHP application developed with glue
microframework. To add a new html page for a test:
tests/data/app/view
tests/data/app/index.php
tests/data/app/controllers.php
To see the page in the browser, open http://localhost:8000/your-route
Then create a test in tests/web/WebDriverTest.php
, and run it with php codecept run web WebDriverTest::yourTest --env chrome
Learn by examples! There are pretty much other tests already written. Please follow their structure.
By default you are supposed to access module methods via module
property.
<?php
$this->module->amOnPage('/form/checkbox');
$this->module->appendField('form input[name=terms]', 'I Agree123');
?>
To verify that form was submitted correctly you can use data::get
method.
Example:
<?php
function testPasswordField()
{
$this->module->amOnPage('/form/complex');
$this->module->submitForm('form', array(
'password' => '123456'
));
$form = data::get('form');
$this->assertEquals('123456', $form['password']);
}
?>
There is also a convention to use shouldFail
method to set expected fail.
<?php
protected function shouldFail()
{
$this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
}
public function testAppendFieldRadioButtonByValueFails()
{
$this->shouldFail();
$this->module->amOnPage('/form/radio');
$this->module->appendField('form input[name=terms]','disagree123');
}
?>
For most cases Codeception is self-tested using acceptance tests in cli suite. That is how Codeception core classes are tested. And actually there is no possibility to unit test many cases. Because you can't ask PHPUnit to mock PHPUnit classes.
If you send Pull Request to Codeception core and you don't know how to get it tested, just create new cli test for that. Probably you will need some additional files, maybe another suite configurations, so add them.
That is why Codeception can't have code coverage reports, as we rely on acceptance tests in testing core.
You can run all cli tests with
codecept run cli
Test cases are:
Before each test tests/data/claypit
is copied to tests/data/sandbox
, and all the test actions will be executed inside that sandbox. In the end this directory is removed. In sandbox different codeception tests may be executed and checked for exepected output.
Example test:
<?php
$I = new CliGuy($scenario);
$I->wantToTest('build command');
$I->runShellCommand('php codecept build');
$I->seeInShellOutput('generated successfully');
$I->seeFileFound('CodeGuy.php','tests/unit');
$I->seeFileFound('CliGuy.php','tests/cli');
$I->seeInThisFile('seeFileFound(');
?>
There are various test suites in tests/data/claypit
. Maybe you will need to create a new suite for your tests.
Acceptance tests that use demo application and c3
collector, to check that a code coverage can be collected remotely. This tests are rarely updated, they should just work )
docker-compose
Using docker-compose
for test configurations
cd tests
Build the codeception/codeception
image
docker-compose build
Start
docker-compose up -d
By default the image has codecept
as its entrypoint, to run the tests simply supply the run
command
docker-compose run --rm codecept help
Run suite
docker-compose run --rm codecept run cli
Run folder
docker-compose run --rm codecept run unit Codeception/Command
Run single test
docker-compose run --rm codecept run cli ExtensionsCest
Development bash
docker-compose run --rm --entrypoint bash codecept
Cleanup
docker-compose run --rm codecept clean
In parallel
docker-compose --project-name test-cli run -d --rm codecept run --html report-cli.html cli & \
docker-compose --project-name test-unit-command run -d --rm codecept run --html report-unit-command.html unit Codeception/Command & \
docker-compose --project-name test-unit-constraints run -d --rm codecept run --html report-unit-constraints.html unit Codeception/Constraints
Add Redis to docker-compose.yml
services:
[...]
redis:
image: redis:3
Update host
protected static $config = [
'database' => 15,
'host' => 'redis'
];
Run Redis tests
docker-compose run --rm codecept run unit Codeception/Module/RedisTest
Further Examples
firefox:
image: selenium/standalone-firefox-debug:2.52.0
chrome:
image: selenium/standalone-chrome-debug:2.52.0
mongo:
image: mongo