CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just...
-
Upload
truonghanh -
Category
Documents
-
view
221 -
download
1
Transcript of CodeCeption & PHPCI · CODECEPTION • CodeceptionPHP Testing Framework is designed to work just...
CodeCeption &PHPCITestdrivendevelopmentframeworkforPHP&CItool
forLAMPPlatform
WhoamI?
Mizanur RahmanCTO,Informatix technologiesSeniorConsultant,TelenorHealthAS
CSM,CSD,CSP
Scrum&DevTeam
• ThemostimportantroleofScrum• Ensuretechnicalexcellencewithqualitydevelopmentthroughbestpractices• Focusoncontinuousdelivery,deploymentandintegration
RemembertheAgileprinciple
#9: Continuous attention to technical excellence and good design
LAMPplatform
• Linux• Apache• MySQL• PHP
WearegoingtotalkaboutCD&CIwithPHP.ButwhyPHP?
• oneofthemostpopularlanguageforwebbasedapplicationdevelopment.• Veryeasytostartwith• Asaresultcreateslotsofgapsinstandarddevelopment
WhatsortoftestwecanapplyforPHP
• UnitTest• Acceptancetest• Featuretest• Integrationtest• BDD• Webservices• Manymore
TestinginPHPcanbepainful
• Manyframeworksorlibrariestochooseforeachparticulartestpart• ShouldIusePHPUnit orSimpleTest?• WhatisthereforBDDinPHP?• Managingmultiplelibrariescanbepainfulanderrorprone• Developerscanbereluctanttotesttheircodes
CODECEPTION
• Codeception PHPTestingFrameworkisdesignedtoworkjustoutofthebox.• Thismeansitsinstallationrequiresminimalstepsandnoexternaldependenciespreinstalled(exceptPHP,ofcourse).• Onlyoneconfigurationstepshouldbetakenandyouarereadytotestyourwebapplicationfromaneyeofactualuser.• YoucandoUnittest,Featuretest,Acceptancetest,BDD,APItestingandintegrationtestingusingasingleframework
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
Installing&usingcodeception
$composerrequire"codeception/codeception”
$php vendor/bin/codeceptbootstrap
$php vendor/bin/codecept run
StartingwithUnittesting
AssumingwealreadyknowwhatisUnitTesting
• Codeception usesPHPUnit asabackendforrunningtests.• anyPHPUnit testcanbeaddedtoCodeception testsuiteandthenexecuted.• NoneedtoinstallPHPUnit separately.
Creatingfirstunittest
php vendor/bin/codeceptgenerate:phpunit unitExample
Thiswillcreateanewunittest
Testwascreatedin/Applications/MAMP/htdocs/RPN/tests/unit/ExampleTest.php
<?php
class ExampleTest extends \PHPUnit_Framework_TestCase{protected function setUp(){}
protected function tearDown(){}
// testspublic function testMe(){}
}
• Codeception hasitsaddons tostandardunittests,wecanuseanothercommandtogeneratethefile.
php vendor/bin/codeceptgenerate:test unitNewExample
<?php
class NewExampleTestextends \Codeception\Test\Unit{protected $tester;
protected function _before(){
}
protected function _after(){
}
// testspublic function testMe(){
}}
• Thisclasshaspredefined _before and _after methodstostartwith.Youcanusethemtocreateatestedobjectbeforeeachtest,anddestroyitafterwards.• Asyousee,unlikeinPHPUnit, setUp and tearDown methodsarereplacedwiththeiraliases: _before, _after.• Theactual setUp and tearDown wereimplementedbyparentclass\Codeception\TestCase\Test
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));}
WecanalsogenerateHTMLreport
php vendor/bin/codecept run–html
Pros• fastest(well,inthecurrentexample,youstillneeddatabaserepopulation)• cancoverrarelyusedfeatures• canteststabilityofapplicationcore• youcanonlybeconsideredagooddeveloperifyouwritethem:)
Cons• doesn’ttestconnectionsbetweenunits• unstableinsupport:verysensitivetocodechanges
Acceptancetest
• Acceptancetestingcanbeperformedbyanyone.• Needsawebbrowsertotesttheapplicationifyouarebuildingawebbasedapplication• YoucanreproduceaAcceptanceTester’s actionsinscenariosandrunthemautomaticallyaftereachsitechange• Codeception keepstestscleanandsimple
php vendor/bin/codecept generate:cept acceptanceLogin
thiswillcreateanewfiletests/acceptance/LoginCept.php
WritingourfirstScenario
Weneedtosetupthelocalurl inacceptance.suite.yml file
Runningthescenario
Usefultermstoknow
• wantTo• amOnPage• Click• fillField• selectOption• submitForm• See• dontSee• seeCheckboxIsChecked• seeInField• seeLink
Pros• canberunonanywebsite• cantestjavascript andajaxrequests• canbeshowntoyourclientsandmanagers• moststableinsupport:lessaffectedbychangesinsourcecodeortechnologies
Cons• theslowest:requiresrunningbrowseranddatabaserepopulation• fewercheckscanleadtofalse-positiveresults• yep,theyarereallyslow• notstableinexecution:renderingandjavascript issuescanleadtounpredictableresults
FunctionalTests
• Nowthatwe’vewrittensomeacceptancetests,functionaltestsarealmostthesame,withjustonemajordifference:functionaltestsdon’trequireawebservertoruntests.• Insimpletermsweset $_REQUEST, $_GET and $_POST variablesandthenweexecuteapplicationfromatest.Thismaybevaluableasfunctionaltestsarefasterandprovidedetailedstacktracesonfailures.• Pitfalls:Acceptancetestsareusuallymuchslowerthanfunctionaltests.ButfunctionaltestsarelessstableastheyrunCodeception andapplicationinoneenvironment.Ifyourapplicationwasnotdesignedtoruninlonglivingprocess,forinstanceyouuse exitoperatororglobalvariables,probablyfunctionaltestsarenotforyou.
Pros• likeacceptancetests,butmuchfaster• canprovidemoredetailedreports• youcanstillshowthiscodetomanagersandclients• stableenough:onlymajorcodechanges,ormovingtootherframework,canbreakthem
Cons• javascript andajaxcan’tbetested• byemulatingthebrowseryoumightgetmorefalse-positiveresults• requiresaframework
BDD
• BehaviorDrivenDevelopmentisapopularmethodologyofsoftwaredevelopment.• theideaofstoryBDDcanbenarrowedto:• describefeaturesinascenariowithaformaltext• useexamplestomakeabstractthingsconcrete• implementeachstepofascenariofortesting• writeactualcodeimplementingthefeature
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 $
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 $
php vendor/bin/codeceptg:feature acceptancecheckout
php vendor/bin/codeceptdry-runacceptancecheckout.feature
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");}
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
php vendor/bin/codeceptgenerate:cept api CreateUser
Whatiscodecoverage?
• Atsomepointyouwanttoreviewwhichpartsofyourapplicationaretestedwellandwhicharenot• Whenyouexecuteyourteststocollectcoveragereport,youwillreceivestatisticsofallclasses,methods,andlinestriggeredbythesetests.• Theratiobetweenalllinesinscriptandalltouchedlinesisamaincoveragecriterion.• Tocollectcoverageinformation xdebug isrequired
Toenablecodecoverageputtheselinesintheglobal configuration filecodeception.yml:
coverage:enabled: true
Wecanalsodefinewhichfilestoexcludefromthecoverage
coverage:enabled: truewhitelist:
include: - app/*exclude: - app/cache/*
codecept run--coverage--coverage-xml--coverage-html
WhatisCI?
• Wedonotwanttomanuallyrunourtestsuiteseverytimethecodeisupdated.• Wedonotliketoperformmanualtesting,speciallydevelopers• Thesolutionissimple,testexecutionshouldbeautomated
AvailabletoolsforCI
• Jenkins• Teamcity• PHPCI• Bamboo• TravisCI
WhyPHPCI?
Setup
• Dependingonwhatyouwanttodo,youhavetoinstallsometools.• Afterloggingin,youcangoto admin manageplugins andinstallanynecessaryplugins.• Byinstallingaplugin,youareupdatingthecomposer.json filewithnewrequirements• Runcomposerupdateeverytimeyouupdatethesettings.
Addproject
• Byclickingthe addproject buttonintheheader,youcancreateanewproject• Eachbuildprocessconsistsof5phases.• Setup.Thephasewereeverythingisinitialized• Test.Thephasewerealltestsareexecuted• Complete.Successorfailure,thispartwillalwaysrun• Success.Willonlyberunincaseofsuccess• Failure.Willonlyberunincaseoffailure
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"
Buildresults
incaseofafailure,PHPCIwillclearlyindicatewhatiswrong
Thankyou
Questions?