SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

javascript function & closure
javascript function & closurejavascript function & closure
javascript function & closureHika Maeng
 
Unit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by ExampleUnit-Testing Bad-Practices by Example
Unit-Testing Bad-Practices by ExampleBenjamin Eberlei
 
Grammatical Optimization
Grammatical OptimizationGrammatical Optimization
Grammatical Optimizationadil raja
 
Unittesting Bad-Practices by Example
Unittesting Bad-Practices by ExampleUnittesting Bad-Practices by Example
Unittesting Bad-Practices by ExampleBenjamin Eberlei
 
Bad test, good test
Bad test, good testBad test, good test
Bad test, good testSeb Rose
 
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 210Mahmoud 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 applicationsAndrii Lashchenko
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner codeMite Mitreski
 
Unittesting JavaScript with Evidence
Unittesting JavaScript with EvidenceUnittesting JavaScript with Evidence
Unittesting JavaScript with EvidenceTobie Langel
 
4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладкаDEVTYPE
 
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, pythonRobert 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 184Mahmoud Samir Fayed
 
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistAnton 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 196Mahmoud 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-2Katy Slemon
 

Was ist angesagt? (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
 

Andere mochten auch

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_v2Tricode (part of Dept)
 
SLA - Alignment and Interaction
SLA - Alignment and InteractionSLA - Alignment and Interaction
SLA - Alignment and InteractionIch_brenne
 
Slide Show
Slide ShowSlide Show
Slide Showchoii587
 
Echoi slide
Echoi slideEchoi slide
Echoi slidechoii587
 
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 NJchoii587
 
Ataque electronico
Ataque electronicoAtaque electronico
Ataque electronicoLuis Tume
 

Andere mochten auch (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
 

Ähnlich wie UNIT TESTING with PHPUnit: Write Tests, Test Code

Understanding JavaScript Testing
Understanding JavaScript TestingUnderstanding JavaScript Testing
Understanding JavaScript Testingjeresig
 
Test-driven Development for TYPO3
Test-driven Development for TYPO3Test-driven Development for TYPO3
Test-driven Development for TYPO3Oliver 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 PHPUnitsmueller_sandsmedia
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnitvaruntaliyan
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitmfrost503
 
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.4jeresig
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and youmarkstory
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2Yi-Huan Chan
 
Test driven node.js
Test driven node.jsTest driven node.js
Test driven node.jsJay Harris
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitmfrost503
 
Executable documentation
Executable documentationExecutable documentation
Executable documentationRussell Gold
 
Js 单元测试框架介绍
Js 单元测试框架介绍Js 单元测试框架介绍
Js 单元测试框架介绍louieuser
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxsrcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxwhitneyleman54422
 
Writing Test Cases with PHPUnit
Writing Test Cases with PHPUnitWriting Test Cases with PHPUnit
Writing Test Cases with PHPUnitShouvik Chatterjee
 
Symfony (Unit, Functional) Testing.
Symfony (Unit, Functional) Testing.Symfony (Unit, Functional) Testing.
Symfony (Unit, Functional) Testing.Basel Issmail
 

Ähnlich wie UNIT TESTING with PHPUnit: Write Tests, Test Code (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
 

Kürzlich hochgeladen

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 

Kürzlich hochgeladen (20)

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 

UNIT TESTING with PHPUnit: Write Tests, Test Code

  • 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.

Hinweis der Redaktion

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