SlideShare a Scribd company logo
UNIT TESTING
with PHPUnit
about me
        @ferca_tw
             ferran caellas puig

             Core Developer at eyeOS
Computer geek and Open Source enthusiast
What is
UNIT TESTING
class CoreTest extends PHPUnit_framework_TestCase {

    public function TestCount() {
       $testArray = array(‘value1’,’value2’,’value3’);
       $countResult = count($testArray);
       $this->assertEquals(3, $countResult);
    }
}
What you want is to write tests that fail
even though you think they should work,
or tests that succeed even though you
think they should fail.

You want to write tests that will pay you
back with information.
                               Erich Gamma
Writing tests
During development
TDD
TEST-DRIVEN DEVELOPMENT
Writing tests
During debugging
STEPS
1. Verify that you can reproduce the bug.
2. Find the smallest-scale demonstration of the
bug in the code.
3. Write an automated test that fails now but
will succeed when the bug is fixed.
4. Fix the bug.
CODE COVERAGE   The beauty of testing is found not in
                the effort but in the effiency.

                Knowing what should be tested is
                beautiful, and knowing what is being
                tested is beautiful.
                                     Murali Nandigama
public function testEmpty()
           {
             $stack = array();
@depends     $this->assertEmpty($stack);

               return $stack;
           }

           /**
            * @depends testEmpty
            */
           public function testPush(array $stack)
           {
             array_push($stack, 'foo');
             $this->assertEquals('foo', $stack[count($stack)-1]);
             $this->assertNotEmpty($stack);

               return $stack;
           }
/**
                 * @dataProvider provider
                 */
@dataProvider   public function testAdd($a, $b, $c)
                {
                  $this->assertEquals($c, $a + $b);
                }

                public function provider()
                {
                  return array(
                   array(0, 0, 0),
                   array(0, 1, 1),
                   array(1, 0, 1),
                   array(1, 1, 3)
                   );
                }
@expectedException
                     /**
                      * @expectedException InvalidArgumentException
                      * @expectedExceptionMessage Right Message
                      */
                     public function testExceptionHasRightMessage()
                     {
                       throw new InvalidArgumentException('Some Message', 10);
                     }

                     /**
                      * @expectedException InvalidArgumentException
                      * @expectedExceptionCode 20
                      */
                     public function testExceptionHasRightCode()
                     {
                       throw new InvalidArgumentException('Some Message', 10);
                     }
expectOutputString
                     public function testExpectFooActualFoo()
                       {
                         $this->expectOutputString('foo');
                         print 'foo';
                       }
assertArrayHasKey()	
               assertObjectHasA/ribute()	
  
             assertClassHasA/ribute()	
          assertRegExp()	
  
             assertClassHasSta4cA/ribute()	
     assertStringMatchesFormat()	
  
             assertContains()	
                  assertStringMatchesFormatFile()	
  
Assertions
             assertContainsOnly()	
              assertSame()	
  
             assertCount()	
                     assertSelectCount()	
  
             assertEmpty()	
                     assertSelectEquals()	
  
             assertEqualXMLStructure()	
         assertSelectRegExp()	
  
             assertEquals()	
                    assertStringEndsWith()	
  
             assertFalse()	
                     assertStringEqualsFile()	
  
             assertFileEquals()	
                assertStringStartsWith()	
  
             assertFileExists()	
                assertTag()	
  
             assertGreaterThan()	
               assertThat()	
  
             assertGreaterThanOrEqual()	
        assertTrue()	
  
             assertInstanceOf()	
                assertXmlFileEqualsXmlFile()	
  
             assertInternalType()	
              assertXmlStringEqualsXmlFile()	
  
             assertLessThan()	
                  assertXmlStringEqualsXmlString()	
  
             assertLessThanOrEqual()	
  
             assertNull()	
  
setUp()                        setUpBeforeClass()
           tearDown()                     tearDownAfterClass()

            protected function setUp()
Fixtures
             {
               $this->stack = array();
             }

             public function testEmpty()
             {
               $this->assertTrue(empty($this->stack));
             }

             public function testPush()
             {
               array_push($this->stack, 'foo');
               $this->assertEquals('foo', $this->stack[count($this->stack)-1]);
               $this->assertFalse(empty($this->stack));
             }
markTestIncomplete()

Incomplete & Skipped   markTestSkipped()

                       $this->markTestIncomplete(
                            'This test has not been implemented yet.'
                           );



                         phpunit	
  -­‐-­‐verbose	
  SampleTest	
  
                         PHPUnit	
  3.6.0	
  by	
  Sebas4an	
  Bergmann.	
  
                         I	
  
                         Time:	
  0	
  seconds,	
  Memory:	
  3.75Mb	
  
                         	
  
                         There	
  was	
  1	
  incomplete	
  test:	
  
                         	
  
                         1)	
  SampleTest::testSomething	
  
                         This	
  test	
  has	
  not	
  been	
  implemented	
  yet.	
  
                         	
  
                         /home/sb/SampleTest.php:12	
  
                         OK,	
  but	
  incomplete	
  or	
  skipped	
  tests!	
  
                         Tests:	
  1,	
  Asser4ons:	
  1,	
  Incomplete:	
  1.	
  
UNIT TESTING
with PHPUnit

More Related Content

What's hot

javascript function & closure
javascript function & closurejavascript function & closure
javascript function & closure
Hika Maeng
 
Google guava
Google guavaGoogle guava
Django
Django Django
Django
Toru Furukawa
 
Unit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by ExampleUnit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by Example
Benjamin Eberlei
 
Grammatical Optimization
Grammatical OptimizationGrammatical Optimization
Grammatical Optimization
adil raja
 
Unittesting Bad-Practices by Example
Unittesting Bad-Practices by ExampleUnittesting Bad-Practices by Example
Unittesting Bad-Practices by Example
Benjamin Eberlei
 
Google Guava
Google GuavaGoogle Guava
Google Guava
Alexander Korotkikh
 
Bad test, good test
Bad test, good testBad test, good test
Bad test, good test
Seb Rose
 
Next Level Testing
Next Level TestingNext Level Testing
Next Level Testing
James Saryerwinnie
 
The Ring programming language version 1.9 book - Part 91 of 210
The Ring programming language version 1.9 book - Part 91 of 210The Ring programming language version 1.9 book - Part 91 of 210
The Ring programming language version 1.9 book - Part 91 of 210
Mahmoud Samir Fayed
 
Message-based communication patterns in distributed Akka applications
Message-based communication patterns in distributed Akka applicationsMessage-based communication patterns in distributed Akka applications
Message-based communication patterns in distributed Akka applications
Andrii Lashchenko
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner code
Mite Mitreski
 
Unittesting JavaScript with Evidence
Unittesting JavaScript with EvidenceUnittesting JavaScript with Evidence
Unittesting JavaScript with Evidence
Tobie Langel
 
4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка
DEVTYPE
 
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, python
Robert Lujo
 
The Ring programming language version 1.5.3 book - Part 88 of 184
The Ring programming language version 1.5.3 book - Part 88 of 184The Ring programming language version 1.5.3 book - Part 88 of 184
The Ring programming language version 1.5.3 book - Part 88 of 184
Mahmoud Samir Fayed
 
zinno
zinnozinno
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with Javassist
Anton Arhipov
 
The Ring programming language version 1.7 book - Part 84 of 196
The Ring programming language version 1.7 book - Part 84 of 196The Ring programming language version 1.7 book - Part 84 of 196
The Ring programming language version 1.7 book - Part 84 of 196
Mahmoud Samir Fayed
 
Unit Testing in Angular(7/8/9) Using Jasmine and Karma Part-2
Unit Testing in Angular(7/8/9) Using Jasmine and Karma Part-2Unit Testing in Angular(7/8/9) Using Jasmine and Karma Part-2
Unit Testing in Angular(7/8/9) Using Jasmine and Karma Part-2
Katy Slemon
 

What's hot (20)

javascript function & closure
javascript function & closurejavascript function & closure
javascript function & closure
 
Google guava
Google guavaGoogle guava
Google guava
 
Django
Django Django
Django
 
Unit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by ExampleUnit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by Example
 
Grammatical Optimization
Grammatical OptimizationGrammatical Optimization
Grammatical Optimization
 
Unittesting Bad-Practices by Example
Unittesting Bad-Practices by ExampleUnittesting Bad-Practices by Example
Unittesting Bad-Practices by Example
 
Google Guava
Google GuavaGoogle Guava
Google Guava
 
Bad test, good test
Bad test, good testBad test, good test
Bad test, good test
 
Next Level Testing
Next Level TestingNext Level Testing
Next Level Testing
 
The Ring programming language version 1.9 book - Part 91 of 210
The Ring programming language version 1.9 book - Part 91 of 210The Ring programming language version 1.9 book - Part 91 of 210
The Ring programming language version 1.9 book - Part 91 of 210
 
Message-based communication patterns in distributed Akka applications
Message-based communication patterns in distributed Akka applicationsMessage-based communication patterns in distributed Akka applications
Message-based communication patterns in distributed Akka applications
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner code
 
Unittesting JavaScript with Evidence
Unittesting JavaScript with EvidenceUnittesting JavaScript with Evidence
Unittesting JavaScript with Evidence
 
4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка
 
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, python
 
The Ring programming language version 1.5.3 book - Part 88 of 184
The Ring programming language version 1.5.3 book - Part 88 of 184The Ring programming language version 1.5.3 book - Part 88 of 184
The Ring programming language version 1.5.3 book - Part 88 of 184
 
zinno
zinnozinno
zinno
 
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with Javassist
 
The Ring programming language version 1.7 book - Part 84 of 196
The Ring programming language version 1.7 book - Part 84 of 196The Ring programming language version 1.7 book - Part 84 of 196
The Ring programming language version 1.7 book - Part 84 of 196
 
Unit Testing in Angular(7/8/9) Using Jasmine and Karma Part-2
Unit Testing in Angular(7/8/9) Using Jasmine and Karma Part-2Unit Testing in Angular(7/8/9) Using Jasmine and Karma Part-2
Unit Testing in Angular(7/8/9) Using Jasmine and Karma Part-2
 

Viewers also liked

Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
SLA - Alignment and Interaction
SLA - Alignment and InteractionSLA - Alignment and Interaction
SLA - Alignment and Interaction
Ich_brenne
 
Slide Show
Slide ShowSlide Show
Slide Show
choii587
 
SOLID
SOLIDSOLID
SOLID
ferca_sl
 
Echoi slide
Echoi slideEchoi slide
Echoi slide
choii587
 
Túlavé Garmonty
Túlavé GarmontyTúlavé Garmonty
Túlavé Garmontybastion46
 
Wal-Mart Harrison NJ
Wal-Mart Harrison NJWal-Mart Harrison NJ
Wal-Mart Harrison NJ
choii587
 
Ataque electronico
Ataque electronicoAtaque electronico
Ataque electronico
Luis Tume
 

Viewers also liked (10)

Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
 
My five
My fiveMy five
My five
 
SLA - Alignment and Interaction
SLA - Alignment and InteractionSLA - Alignment and Interaction
SLA - Alignment and Interaction
 
Slide Show
Slide ShowSlide Show
Slide Show
 
SOLID
SOLIDSOLID
SOLID
 
Echoi slide
Echoi slideEchoi slide
Echoi slide
 
Túlavé Garmonty
Túlavé GarmontyTúlavé Garmonty
Túlavé Garmonty
 
My five
My fiveMy five
My five
 
Wal-Mart Harrison NJ
Wal-Mart Harrison NJWal-Mart Harrison NJ
Wal-Mart Harrison NJ
 
Ataque electronico
Ataque electronicoAtaque electronico
Ataque electronico
 

Similar to Unit testing with PHPUnit

Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
jeresig
 
Test-driven Development for TYPO3
Test-driven Development for TYPO3Test-driven Development for TYPO3
Test-driven Development for TYPO3
Oliver Klee
 
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitinternational PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
smueller_sandsmedia
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
varuntaliyan
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
mfrost503
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4
jeresig
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
Bastian Feder
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and you
markstory
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
Yi-Huan Chan
 
Test driven node.js
Test driven node.jsTest driven node.js
Test driven node.js
Jay Harris
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
mfrost503
 
Executable documentation
Executable documentationExecutable documentation
Executable documentation
Russell Gold
 
Js 单元测试框架介绍
Js 单元测试框架介绍Js 单元测试框架介绍
Js 单元测试框架介绍
louieuser
 
Unit testing concurrent code
Unit testing concurrent codeUnit testing concurrent code
Unit testing concurrent code
Rafael Winterhalter
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
Tomek Kaczanowski
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxsrcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
whitneyleman54422
 
Writing Test Cases with PHPUnit
Writing Test Cases with PHPUnitWriting Test Cases with PHPUnit
Writing Test Cases with PHPUnit
Shouvik Chatterjee
 
Symfony (Unit, Functional) Testing.
Symfony (Unit, Functional) Testing.Symfony (Unit, Functional) Testing.
Symfony (Unit, Functional) Testing.
Basel Issmail
 
JUnit
JUnitJUnit
Test driven development_for_php
Test driven development_for_phpTest driven development_for_php
Test driven development_for_php
Lean Teams Consultancy
 

Similar to Unit testing with PHPUnit (20)

Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testing
 
Test-driven Development for TYPO3
Test-driven Development for TYPO3Test-driven Development for TYPO3
Test-driven Development for TYPO3
 
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitinternational PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and you
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
 
Test driven node.js
Test driven node.jsTest driven node.js
Test driven node.js
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
 
Executable documentation
Executable documentationExecutable documentation
Executable documentation
 
Js 单元测试框架介绍
Js 单元测试框架介绍Js 单元测试框架介绍
Js 单元测试框架介绍
 
Unit testing concurrent code
Unit testing concurrent codeUnit testing concurrent code
Unit testing concurrent code
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxsrcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
 
Writing Test Cases with PHPUnit
Writing Test Cases with PHPUnitWriting Test Cases with PHPUnit
Writing Test Cases with PHPUnit
 
Symfony (Unit, Functional) Testing.
Symfony (Unit, Functional) Testing.Symfony (Unit, Functional) Testing.
Symfony (Unit, Functional) Testing.
 
JUnit
JUnitJUnit
JUnit
 
Test driven development_for_php
Test driven development_for_phpTest driven development_for_php
Test driven development_for_php
 

Recently uploaded

Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Things to Consider When Choosing a Website Developer for your Website | FODUU
Things to Consider When Choosing a Website Developer for your Website | FODUUThings to Consider When Choosing a Website Developer for your Website | FODUU
Things to Consider When Choosing a Website Developer for your Website | FODUU
FODUU
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
CAKE: Sharing Slices of Confidential Data on Blockchain
CAKE: Sharing Slices of Confidential Data on BlockchainCAKE: Sharing Slices of Confidential Data on Blockchain
CAKE: Sharing Slices of Confidential Data on Blockchain
Claudio Di Ciccio
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 

Recently uploaded (20)

Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Things to Consider When Choosing a Website Developer for your Website | FODUU
Things to Consider When Choosing a Website Developer for your Website | FODUUThings to Consider When Choosing a Website Developer for your Website | FODUU
Things to Consider When Choosing a Website Developer for your Website | FODUU
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
CAKE: Sharing Slices of Confidential Data on Blockchain
CAKE: Sharing Slices of Confidential Data on BlockchainCAKE: Sharing Slices of Confidential Data on Blockchain
CAKE: Sharing Slices of Confidential Data on Blockchain
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 

Unit testing with PHPUnit

  • 2. about me @ferca_tw ferran caellas puig Core Developer at eyeOS Computer geek and Open Source enthusiast
  • 4. class CoreTest extends PHPUnit_framework_TestCase { public function TestCount() { $testArray = array(‘value1’,’value2’,’value3’); $countResult = count($testArray); $this->assertEquals(3, $countResult); } }
  • 5. What you want is to write tests that fail even though you think they should work, or tests that succeed even though you think they should fail. You want to write tests that will pay you back with information. Erich Gamma
  • 9. STEPS 1. Verify that you can reproduce the bug. 2. Find the smallest-scale demonstration of the bug in the code. 3. Write an automated test that fails now but will succeed when the bug is fixed. 4. Fix the bug.
  • 10. CODE COVERAGE The beauty of testing is found not in the effort but in the effiency. Knowing what should be tested is beautiful, and knowing what is being tested is beautiful. Murali Nandigama
  • 11.
  • 12. public function testEmpty() { $stack = array(); @depends $this->assertEmpty($stack); return $stack; } /** * @depends testEmpty */ public function testPush(array $stack) { array_push($stack, 'foo'); $this->assertEquals('foo', $stack[count($stack)-1]); $this->assertNotEmpty($stack); return $stack; }
  • 13. /** * @dataProvider provider */ @dataProvider public function testAdd($a, $b, $c) { $this->assertEquals($c, $a + $b); } public function provider() { return array( array(0, 0, 0), array(0, 1, 1), array(1, 0, 1), array(1, 1, 3) ); }
  • 14. @expectedException /** * @expectedException InvalidArgumentException * @expectedExceptionMessage Right Message */ public function testExceptionHasRightMessage() { throw new InvalidArgumentException('Some Message', 10); } /** * @expectedException InvalidArgumentException * @expectedExceptionCode 20 */ public function testExceptionHasRightCode() { throw new InvalidArgumentException('Some Message', 10); }
  • 15. expectOutputString public function testExpectFooActualFoo() { $this->expectOutputString('foo'); print 'foo'; }
  • 16. assertArrayHasKey()   assertObjectHasA/ribute()   assertClassHasA/ribute()   assertRegExp()   assertClassHasSta4cA/ribute()   assertStringMatchesFormat()   assertContains()   assertStringMatchesFormatFile()   Assertions assertContainsOnly()   assertSame()   assertCount()   assertSelectCount()   assertEmpty()   assertSelectEquals()   assertEqualXMLStructure()   assertSelectRegExp()   assertEquals()   assertStringEndsWith()   assertFalse()   assertStringEqualsFile()   assertFileEquals()   assertStringStartsWith()   assertFileExists()   assertTag()   assertGreaterThan()   assertThat()   assertGreaterThanOrEqual()   assertTrue()   assertInstanceOf()   assertXmlFileEqualsXmlFile()   assertInternalType()   assertXmlStringEqualsXmlFile()   assertLessThan()   assertXmlStringEqualsXmlString()   assertLessThanOrEqual()   assertNull()  
  • 17. setUp() setUpBeforeClass() tearDown() tearDownAfterClass() protected function setUp() Fixtures { $this->stack = array(); } public function testEmpty() { $this->assertTrue(empty($this->stack)); } public function testPush() { array_push($this->stack, 'foo'); $this->assertEquals('foo', $this->stack[count($this->stack)-1]); $this->assertFalse(empty($this->stack)); }
  • 18. markTestIncomplete() Incomplete & Skipped markTestSkipped() $this->markTestIncomplete( 'This test has not been implemented yet.' ); phpunit  -­‐-­‐verbose  SampleTest   PHPUnit  3.6.0  by  Sebas4an  Bergmann.   I   Time:  0  seconds,  Memory:  3.75Mb     There  was  1  incomplete  test:     1)  SampleTest::testSomething   This  test  has  not  been  implemented  yet.     /home/sb/SampleTest.php:12   OK,  but  incomplete  or  skipped  tests!   Tests:  1,  Asser4ons:  1,  Incomplete:  1.  
  • 19.

Editor's Notes

  1.  phpunit --coverage-html ./result TwitterTest.php