Вы находитесь на странице: 1из 30

Testing with Codeception

by Hafid Mukhlasin

1
Disclaimer!
I am newbie
Get this presentation : https://github.com/hscstudio/slides/JAKDIVA

2
Whats testing?
Why should we do code testing? testing dependency code

3
Q
When do we testing?
test after coding (commonly), test before coding (Test-Driven Development)

4
Q
Its about repetitive work
Please, Dont Repeat Yourself (DRY)

5
Q
XX Codeception
Modern PHP testing for everyone http://codeception.com

6
Testing type that we can do
Unit testing - verifies that a single unit of code is working as
expected.
Functional testing - verifies scenarios from a user's perspective via
browser emulation.
Acceptance testing - verifies scenarios from a user's perspective in a
browser.We can test Javascript code
etc.

7
Code Testing Yii base on Codeception
itss located in @app/test directory

8
Where we begin?

9
Install Yii 2.0.7
composer global require "fxp/composer-asset-plugin:~1.1.1"
composer create-project yiisoft/yii2-app-basic jakdiva 2.0.7

10
Run Built-in PHP Web Server
cd jakdiva
yii serve port=8080

11
Running Yii Application
http::/localhost:8080

12
Install Codeception
composer global require "codeception/codeception=2.0.*"
composer global require "codeception/specify=*"
composer global require "codeception/verify=*"

13
Add Directory of Codeception to Path
Check location of codeception directory
composer global status

C:/Users/hafid/AppData/Roaming/Composer/vendor/bin
Add directory of codeception to Path or Global Environment
Variabel
So we can run codeception only with codecept

14
Codeception ready to use!

15
We can do testing
Yii have make sample of code testing

16
Part of unit testing sample
@app\tests\codeception\unit\models\LoginFormTest.php
public function testLoginNoUser()
{
$model = new LoginForm([
'username' => 'not_existing_username',
'password' => 'not_existing_password',
]);

$this->specify('user should not be able to login, when there is no identity',


function () use ($model) {
expect('model should not login user', $model->login())->false();
expect('user should not be logged in', Yii::$app->user->isGuest)->true();
});
}

17
Part of functional testing sample
@app\tests\codeception\functional\LoginCept.php

use tests\codeception\_pages\LoginPage;

/* @var $scenario Codeception\Scenario */

$I = new FunctionalTester($scenario);
$I->wantTo('ensure that login works');

$loginPage = LoginPage::openBy($I);

$I->see('Login', 'h1');

$I->amGoingTo('try to login with empty credentials');


$loginPage->login('', '');
$I->expectTo('see validations errors');
$I->see('Username cannot be blank.');
$I->see('Password cannot be blank.');

18
Part of acceptance testing sample
@app\tests\codeception\acceptance\LoginCept.php
use tests\codeception\_pages\LoginPage;

/* @var $scenario Codeception\Scenario */

$I = new AcceptanceTester($scenario);
$I->wantTo('ensure that login works');

$loginPage = LoginPage::openBy($I);

$I->see('Login', 'h1');

$I->amGoingTo('try to login with empty credentials');


$loginPage->login('', '');
if (method_exists($I, 'wait')) {
$I->wait(3); // only for selenium
}
$I->expectTo('see validations errors');
$I->see('Username cannot be blank.');
$I->see('Password cannot be blank.');

19
Build Test Suite
run `codecept build` to build test suite
codecept build

20
Run Testing
run `codecept run` to run 3 types testing (unit, functional,
acceptance)
codecept run

21
run test per test type
codecept run unit
codecept run functional
codecept run acceptance

22
run test per file or function
codecept run unit models/LoginFormTest
codecept run unit models/LoginFormTest:testLoginNoUser

23
Install Selenium for Acceptance
http://codeception.com/docs/modules/WebDriver

24
Step By Step Installing Selenium
Download selenium http://docs.seleniumhq.org/download/
Run Selenium Server
java -jar selenium-server-standalone-2.xx.xxx.jar

or we can use selected browser


java -jar selenium-server-standalone-2.xx.xxx.jar -
Dwebdriver.firefox.bin="D:\Program Files (x86)\Mozilla
Firefox\Firefox.exe"

Enable module WebDriver @app/tests/codeception/acceptance.suite.yml

25
Running selenium server

26
Enable module WebDriver
@app/tests/codeception/acceptance.suite.yml
modules:
enabled:
- WebDriver
# you can use WebDriver instead of PhpBrowser to test javascript and ajax.
# This will require you to install selenium. See http://codeception.com/docs/04-AcceptanceTests#Selenium
# "restart" option is used by the WebDriver to start each time per test-file new session and cookies,
# it is useful if you want to login in your app in each test.
# - WebDriver
config:
PhpBrowser:
# PLEASE ADJUST IT TO THE ACTUAL ENTRY POINT WITHOUT PATH INFO
url: http://localhost:8080
WebDriver:
url: http://localhost:8080/index-test.php
browser: firefox
restart: true

27
Build n Run Testing
Build Test Suite (because we enable/modify test module)
codecept build

Run Acceptance Test


codecept run acceptance

28
Any Question?

29
Thank You
hafidmukhlasin@gmail.com

30

Вам также может понравиться