CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just...

56
CodeCeption & PHPCI Test driven development framework for PHP & CI tool for LAMP Platform

Transcript of CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just...

Page 1: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

CodeCeption &PHPCITestdrivendevelopmentframeworkforPHP&CItool

forLAMPPlatform

Page 2: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

WhoamI?

Mizanur RahmanCTO,Informatix technologiesSeniorConsultant,TelenorHealthAS

CSM,CSD,CSP

Page 3: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Scrum&DevTeam

• ThemostimportantroleofScrum• Ensuretechnicalexcellencewithqualitydevelopmentthroughbestpractices• Focusoncontinuousdelivery,deploymentandintegration

Page 4: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

RemembertheAgileprinciple

Page 5: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

#9: Continuous attention to technical excellence and good design

Page 6: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

LAMPplatform

• Linux• Apache• MySQL• PHP

WearegoingtotalkaboutCD&CIwithPHP.ButwhyPHP?

• oneofthemostpopularlanguageforwebbasedapplicationdevelopment.• Veryeasytostartwith• Asaresultcreateslotsofgapsinstandarddevelopment

Page 7: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

WhatsortoftestwecanapplyforPHP

• UnitTest• Acceptancetest• Featuretest• Integrationtest• BDD• Webservices• Manymore

Page 8: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

TestinginPHPcanbepainful

• Manyframeworksorlibrariestochooseforeachparticulartestpart• ShouldIusePHPUnit orSimpleTest?• WhatisthereforBDDinPHP?• Managingmultiplelibrariescanbepainfulanderrorprone• Developerscanbereluctanttotesttheircodes

Page 9: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

CODECEPTION

• Codeception PHPTestingFrameworkisdesignedtoworkjustoutofthebox.• Thismeansitsinstallationrequiresminimalstepsandnoexternaldependenciespreinstalled(exceptPHP,ofcourse).• Onlyoneconfigurationstepshouldbetakenandyouarereadytotestyourwebapplicationfromaneyeofactualuser.• YoucandoUnittest,Featuretest,Acceptancetest,BDD,APItestingandintegrationtestingusingasingleframework

Page 10: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Codeception features

• SeleniumWebDriverintegration• Elementsmatchedbyname,CSS,Xpath• Symfony2,Laravel,Yii,Phalcon,ZendFramework• PageObjects andStepObjects included• BDD-stylereadabletests• PoweredbyPHPUnit• APItesting:REST,SOAP,XML-RPC• FacebookAPItesting• DataCleanup• HTML,XML,TAP,JSONreports• CodeCoverageandRemoteCodeCoverage• ParallelExecution

Page 11: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Installing&usingcodeception

$composerrequire"codeception/codeception”

$php vendor/bin/codeceptbootstrap

$php vendor/bin/codecept run

Page 12: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external
Page 13: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

StartingwithUnittesting

AssumingwealreadyknowwhatisUnitTesting

• Codeception usesPHPUnit asabackendforrunningtests.• anyPHPUnit testcanbeaddedtoCodeception testsuiteandthenexecuted.• NoneedtoinstallPHPUnit separately.

Page 14: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Creatingfirstunittest

php vendor/bin/codeceptgenerate:phpunit unitExample

Thiswillcreateanewunittest

Testwascreatedin/Applications/MAMP/htdocs/RPN/tests/unit/ExampleTest.php

Page 15: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

<?php

class ExampleTest extends \PHPUnit_Framework_TestCase{protected function setUp(){}

protected function tearDown(){}

// testspublic function testMe(){}

}

Page 16: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

• Codeception hasitsaddons tostandardunittests,wecanuseanothercommandtogeneratethefile.

php vendor/bin/codeceptgenerate:test unitNewExample

Page 17: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

<?php

class NewExampleTestextends \Codeception\Test\Unit{protected $tester;

protected function _before(){

}

protected function _after(){

}

// testspublic function testMe(){

}}

Page 18: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

• Thisclasshaspredefined _before and _after methodstostartwith.Youcanusethemtocreateatestedobjectbeforeeachtest,anddestroyitafterwards.• Asyousee,unlikeinPHPUnit, setUp and tearDown methodsarereplacedwiththeiraliases: _before, _after.• Theactual setUp and tearDown wereimplementedbyparentclass\Codeception\TestCase\Test

Page 19: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Someunittestcases

public function testEqual(){$rpn = new RPN();

$this->assertEquals(5,$rpn->add(2,3));

$this->assertEquals(15,$rpn->add(12,3));}

public function testNotEqual(){$rpn = new RPN();

$this->assertNotEquals(6,$rpn->add(2,3));

$this->assertNotEquals(8,$rpn->add(2,3));}

Page 20: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external
Page 21: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external
Page 22: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

WecanalsogenerateHTMLreport

php vendor/bin/codecept run–html

Page 23: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Pros• fastest(well,inthecurrentexample,youstillneeddatabaserepopulation)• cancoverrarelyusedfeatures• canteststabilityofapplicationcore• youcanonlybeconsideredagooddeveloperifyouwritethem:)

Cons• doesn’ttestconnectionsbetweenunits• unstableinsupport:verysensitivetocodechanges

Page 24: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Acceptancetest

• Acceptancetestingcanbeperformedbyanyone.• Needsawebbrowsertotesttheapplicationifyouarebuildingawebbasedapplication• YoucanreproduceaAcceptanceTester’s actionsinscenariosandrunthemautomaticallyaftereachsitechange• Codeception keepstestscleanandsimple

Page 25: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

php vendor/bin/codecept generate:cept acceptanceLogin

thiswillcreateanewfiletests/acceptance/LoginCept.php

Page 26: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

WritingourfirstScenario

Page 27: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Weneedtosetupthelocalurl inacceptance.suite.yml file

Page 28: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Runningthescenario

Page 29: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Usefultermstoknow

• wantTo• amOnPage• Click• fillField• selectOption• submitForm• See• dontSee• seeCheckboxIsChecked• seeInField• seeLink

Page 30: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Pros• canberunonanywebsite• cantestjavascript andajaxrequests• canbeshowntoyourclientsandmanagers• moststableinsupport:lessaffectedbychangesinsourcecodeortechnologies

Cons• theslowest:requiresrunningbrowseranddatabaserepopulation• fewercheckscanleadtofalse-positiveresults• yep,theyarereallyslow• notstableinexecution:renderingandjavascript issuescanleadtounpredictableresults

Page 31: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

FunctionalTests

• Nowthatwe’vewrittensomeacceptancetests,functionaltestsarealmostthesame,withjustonemajordifference:functionaltestsdon’trequireawebservertoruntests.• Insimpletermsweset $_REQUEST, $_GET and $_POST variablesandthenweexecuteapplicationfromatest.Thismaybevaluableasfunctionaltestsarefasterandprovidedetailedstacktracesonfailures.• Pitfalls:Acceptancetestsareusuallymuchslowerthanfunctionaltests.ButfunctionaltestsarelessstableastheyrunCodeception andapplicationinoneenvironment.Ifyourapplicationwasnotdesignedtoruninlonglivingprocess,forinstanceyouuse exitoperatororglobalvariables,probablyfunctionaltestsarenotforyou.

Page 32: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external
Page 33: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Pros• likeacceptancetests,butmuchfaster• canprovidemoredetailedreports• youcanstillshowthiscodetomanagersandclients• stableenough:onlymajorcodechanges,ormovingtootherframework,canbreakthem

Cons• javascript andajaxcan’tbetested• byemulatingthebrowseryoumightgetmorefalse-positiveresults• requiresaframework

Page 34: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

BDD

• BehaviorDrivenDevelopmentisapopularmethodologyofsoftwaredevelopment.• theideaofstoryBDDcanbenarrowedto:• describefeaturesinascenariowithaformaltext• useexamplestomakeabstractthingsconcrete• implementeachstepofascenariofortesting• writeactualcodeimplementingthefeature

Page 35: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Asimplestory

• As a customer I want to buy several products• I put first productwith 600 $ price to my cart• And then another one with 1000 $ price• When I go to checkout process• I should see that total number of products I want to buy is 2• And my order amount is 1600 $

Page 36: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Convertingtofeatureusinggherkin

gherkinFeature: checkout processIn order to buy productsAs a customerI want to be able to buy several products

Scenario:Given I have productwith 600 $ price in my cartAnd I have productwith 1000 $ priceWhen I go to checkout processThen I should see that total number of products is 2And my order amount is 1600 $

Page 37: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

php vendor/bin/codeceptg:feature acceptancecheckout

Page 38: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

php vendor/bin/codeceptdry-runacceptancecheckout.feature

Page 39: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

php vendor/bin/codecept gherkin:snippets acceptance

<?php

class AcceptanceTester extends \Codeception\Actor{/*** @Given i have product with :num1:num2:num2$ price in my cart*/public function iHaveProductWithPriceInMyCart($num1, $num2, $num3){throw new \Codeception\Exception\Incomplete("Step `i have product with :num1:num2:num2$ price in my cart` is not defined"

);}

/*** @Given i have product with :num1:num2:num2:num2$price in my cart*/public function iHaveProductWithPriceInMyCart($num1, $num2, $num3, $num4){throw new \Codeception\Exception\Incomplete("Step `i have product with :num1:num2:num2:num2$price in my cart` is not de

fined");}

Page 40: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

TestingAPI

• WecantestAPIswithcodeception• AllowsbothREST&SOAP

FirstgenerateAPIsuitephp vendor/bin/codecept generate:suite api

Configuremodulesin api.suite.yml:

class_name: ApiTestermodules:enabled:- REST:url: http://serviceapp/api/v1/depends: PhpBrowserpart: Json

Page 41: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

php vendor/bin/codeceptgenerate:cept api CreateUser

Page 42: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Whatiscodecoverage?

• Atsomepointyouwanttoreviewwhichpartsofyourapplicationaretestedwellandwhicharenot• Whenyouexecuteyourteststocollectcoveragereport,youwillreceivestatisticsofallclasses,methods,andlinestriggeredbythesetests.• Theratiobetweenalllinesinscriptandalltouchedlinesisamaincoveragecriterion.• Tocollectcoverageinformation xdebug isrequired

Page 43: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Toenablecodecoverageputtheselinesintheglobal configuration filecodeception.yml:

coverage:enabled: true

Wecanalsodefinewhichfilestoexcludefromthecoverage

coverage:enabled: truewhitelist:

include: - app/*exclude: - app/cache/*

Page 44: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

codecept run--coverage--coverage-xml--coverage-html

Page 45: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

WhatisCI?

• Wedonotwanttomanuallyrunourtestsuiteseverytimethecodeisupdated.• Wedonotliketoperformmanualtesting,speciallydevelopers• Thesolutionissimple,testexecutionshouldbeautomated

Page 46: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external
Page 47: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

AvailabletoolsforCI

• Jenkins• Teamcity• PHPCI• Bamboo• TravisCI

Page 48: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

WhyPHPCI?

Page 49: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Setup

• Dependingonwhatyouwanttodo,youhavetoinstallsometools.• Afterloggingin,youcangoto admin manageplugins andinstallanynecessaryplugins.• Byinstallingaplugin,youareupdatingthecomposer.json filewithnewrequirements• Runcomposerupdateeverytimeyouupdatethesettings.

Page 50: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external
Page 51: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Addproject

• Byclickingthe addproject buttonintheheader,youcancreateanewproject• Eachbuildprocessconsistsof5phases.• Setup.Thephasewereeverythingisinitialized• Test.Thephasewerealltestsareexecuted• Complete.Successorfailure,thispartwillalwaysrun• Success.Willonlyberunincaseofsuccess• Failure.Willonlyberunincaseoffailure

Page 52: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

build_settings:ignore:- "vendor"- "bin"- "app"

setup:composer:action: "install"

test:php_unit:config:- "app/phpunit.xml.dist"

coverage: "coverage"args: "--stderr"

php_mess_detector:allow_failures: true

php_code_sniffer:standard: "PSR2"

php_cpd:allow_failures: true

php_docblock_checker:allowed_warnings: 10skip_classes: true

php_loc:directory: "src"

Page 53: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Buildresults

Page 54: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

incaseofafailure,PHPCIwillclearlyindicatewhatiswrong

Page 55: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Thankyou

Page 56: CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just out of the box. • This means its installation requires minimal steps and no external

Questions?