SlideShare a Scribd company logo
1 of 101
Download to read offline
11
Automated Tests
Rodrigo Paiva
1. Why should I write tests?
2. Unit Tests with PHPUnit
- Characteristics
- Anatomy of a test
3. Functional Tests
- Overview
- Client Object
- Crawler Object
- Profile Object
- Framework Object Access
- Client Configuration
- Request and Response objects introspection
4. References
22
1. Why should I
write tests?
33
Automated Tests
Why should I write tests?
● To find bugs before our customers do it for us
44
Automated Tests
Why should I write tests?
● To find bugs before our customers do it for us
● To not be called in the middle of the night
55
Automated Tests
Why should I write tests?
● To find bugs before our customers do it for us
● To not be called in the middle of the night
● Professionalism
66
Automated Tests
Why should I write tests?
Every time a bug goes to production
it means you as a programmer failed to do your job.
Robert C. Martin (Uncle Bob) in “The Clean Coder”
77
Automated Tests
Why should I write tests?
● To find bugs before our customers do it for us
● To not be called in the middle of the night
● Professionalism
● Customers lose confidence when they find bugs
88
Automated Tests
Why should I write tests?
● To find bugs before our customers do it for us
● To not be called in the middle of the night
● Professionalism
● Customers lose confidence when they find bugs
● Increases the developer confidence in refactoring and changing things
99
Automated Tests
Why should I write tests?
● To find bugs before our customers do it for us
● To not be called in the middle of the night
● Professionalism
● Customers lose confidence when they find bugs
● Increases the developer confidence in refactoring and changing things
● Actually reduces costs in a long term
1010
Automated Tests
Why should I write tests?
1111
Automated Tests
Why should I write tests?
How many tests of each type should I have?
Test pyramid for the win!
1212
Automated Tests
Why should I write tests?
Test Pyramid
Speed of execution
Slow
Fast
Cost
Expensive
Cheap
Debugging
Exploratory
Precise
1313
2. Unit Tests
with PHPUnit
1414
Automated Tests
Unit Tests with PHPUnit
Installation:
composer require --dev phpunit/phpunit
1515
Automated Tests
Unit Tests with PHPUnit
Installation:
composer require --dev phpunit/phpunit
Symfony suggests using the .phar file but the composer version is more maintainable* and
it’s available for everyone in the project.
*My opinion :)
1616
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests:
● Isolated
1717
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests:
● Isolated
● Runs very fast
1818
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests:
● Isolated
● Runs very fast
● Reliable (not failing randomly)
1919
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests:
● Isolated
● Runs very fast
● Reliable (not failing randomly)
● No logic inside
2020
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests:
● Isolated
● Runs very fast
● Reliable (not failing randomly)
● No logic inside
● Assertion on only one object
2121
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests: Isolated
● No actual access to other components, e.g.: API, database, queue server or file system
2222
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests: Isolated
● No actual access to other components, e.g.: API, database, queue server or file system
● Tests shouldn’t depend on the behaviour of other systems
2323
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests: Isolated
● No actual access to other components, e.g.: API, database, queue server or file system
● Tests shouldn’t depend on the behaviour of other systems
● Mock everything that the tested code doesn’t control
2424
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests: Isolated
● No actual access to other components, e.g.: API, database, queue server or file system
● Tests shouldn’t depend on the behaviour of other systems
● Mock everything that the tested code doesn’t control
● Produces deterministic behavior
2525
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests: Runs very fast
● Some milliseconds
2626
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests: Runs very fast
● Some milliseconds
● Feedback loop is instantaneous
2727
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests: Runs very fast
● Some milliseconds
● Feedback loop is instantaneous
● Developers won’t be discouraged from running them
2828
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests: No logic inside
● No flow control (if, else, etc.)
2929
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests: No logic inside
● No flow control (if, else, etc.)
● No loops (while, for, etc.)
3030
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests: No logic inside
● No flow control (if, else, etc.)
● No loops (while, for, etc.)
● No goto
3131
Automated Tests
Unit Tests with PHPUnit
Characteristics of good unit tests: No logic inside
● No flow control (if, else, etc.)
● No loops (while, for, etc.)
● No goto - I’m kidding about this one
3232
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
1. Extends from TestCase
3333
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
1. Extends from TestCase
2. Class name ends with *Test
3434
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
1. Extends from TestCase
2. Class name ends with *Test
3. Method names start with test*
3535
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
1. Extends from TestCase
2. Class name ends with *Test
3. Method names start with test*
a. You can also use the annotation @test instead of the prefix. Use your project’s standard.
3636
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
1. Extends from TestCase
2. Class name ends with *Test
3. Method names start with test*
a. You can also use the annotation @test instead of the prefix. Use your project’s standard.
4. Method names describe what is being tested
3737
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
1. Extends from TestCase
2. Class name ends with *Test
3. Method names start with test*
a. You can also use the annotation @test instead of the prefix. Use your project’s standard.
4. Method names describe what is being tested
5. Body has three stages
3838
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
1. Extends from TestCase
2. Class name ends with *Test
3. Method names start with test*
a. You can also use the annotation @test instead of the prefix. Use your project’s standard.
4. Method names describe what is being tested
5. Body has three stages
a. Prepare
3939
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
1. Extends from TestCase
2. Class name ends with *Test
3. Method names start with test*
a. You can also use the annotation @test instead of the prefix. Use your project’s standard.
4. Method names describe what is being tested
5. Body has three stages
a. Prepare
b. Execute
4040
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
1. Extends from TestCase
2. Class name ends with *Test
3. Method names start with test*
a. You can also use the annotation @test instead of the prefix. Use your project’s standard.
4. Method names describe what is being tested
5. Body has three stages
a. Prepare
b. Execute
c. Assert
4141
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
3
4242
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
3 4
4343
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
3 4
5a
4444
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
3 4
5a
5b
4545
Automated Tests
Unit Tests with PHPUnit
Anatomy of an Unit Test
3 4
5a
5b
5c
4646
Automated Tests
Unit Tests with PHPUnit
Location of test files: tests/<bundleName>
Running the tests via console:
4747
Automated Tests
Unit Tests with PHPUnit
Location of test files: tests/<bundleName>
Running the tests via console:
This is nice, but you should integrate
phpunit into your PHPStorm, so that
running tests becomes a matter of
pressing a key combination.
4848
3. Functional Tests
4949
Automated Tests
Functional Tests
I have 100% code coverage with Unit Tests. Why should I additionally write functional tests?
5050
Automated Tests
Functional Tests
I have 100% code coverage with Unit Tests. Why should I additionally write functional tests?
5151
Automated Tests
Functional Tests - Overview
Functional Tests simulate the user behavior throughout the system and guarantee that:
● Components and systems work together as expected
5252
Automated Tests
Functional Tests - Overview
Functional Tests simulate the user behavior throughout the system and guarantee that:
● Components and systems work together as expected
● Your system functions as it should as a whole
5353
Automated Tests
Functional Tests - Overview
Functional Tests simulate the user behavior throughout the system and guarantee that:
● Components and systems work together as expected
● Your system functions as it should as a whole
● The acceptance criterias in the story are met
5454
Automated Tests
Functional Tests - Overview
Functional Tests simulate the user behavior throughout the system and guarantee that:
● Components and systems work together as expected
● Your system functions as it should as a whole
● The acceptance criterias in the story are met
● Your Product Owner/Manager is happy
5555
Automated Tests
Functional Tests - Overview
The anatomy of a functional test with PHPUnit is not very different from an unit test.
5656
Automated Tests
Functional Tests - Overview
The anatomy of a functional test with PHPUnit is not very different from an unit test.
With Symfony, you extend it from
WebTestCase instead of TestCase.
5757
Automated Tests
Functional Tests - Overview
The anatomy of a functional test with PHPUnit is not very different from an unit test.
With Symfony, you extend it from
WebTestCase instead of TestCase.
1. Prepare (create the client)
5858
Automated Tests
Functional Tests - Overview
The anatomy of a functional test with PHPUnit is not very different from an unit test.
With Symfony, you extend it from
WebTestCase instead of TestCase.
1. Prepare (create the client)
2. Execute (make an actual request)
5959
Automated Tests
Functional Tests - Overview
The anatomy of a functional test with PHPUnit is not very different from an unit test.
With Symfony, you extend it from
WebTestCase instead of TestCase.
1. Prepare (create the client)
2. Execute (make an actual request)
3. Assert (navigate through the DOM)
6060
Automated Tests
Functional Tests - Overview
Location of your tests: tests/<bundleName>/Controller
6161
Automated Tests
Functional Tests - Overview
Location of your tests: tests/<bundleName>/Controller
In case your kernel is not in the standard directory, add this to your phpunit.xml:
6262
Automated Tests
Functional Tests - Client Object
When you create a client you have access to an object with browsing capabilities. You can:
● Execute requests: $client->request(‘httpMethod’, ‘url’);
6363
Automated Tests
Functional Tests - Client Object
When you create a client you have access to an object with browsing capabilities. You can:
● Execute requests: $client->request(‘httpMethod’, ‘url’);
● Navigate:
○ $client->back();
○ $client->forward();
○ $client->reload();
6464
Automated Tests
Functional Tests - Client Object
When you create a client you have access to an object with browsing capabilities. You can:
● Execute requests: $client->request(‘httpMethod’, ‘url’);
● Navigate:
○ $client->back();
○ $client->forward();
○ $client->reload();
● Submit forms: $client->submit($domElement, [formInfo, ...]);
6565
Automated Tests
Functional Tests - Client Object
When you create a client you have access to an object with browsing capabilities. You can:
● Execute requests: $client->request(‘httpMethod’, ‘url’);
● Navigate:
○ $client->back();
○ $client->forward();
○ $client->reload();
● Submit forms: $client->submit($domElement, [formInfo, ...]);
● Execute clicks on links: $client->click($domElement);
6666
Automated Tests
Functional Tests - Client Object
When you create a client you have access to an object with browsing capabilities. You can:
● Execute requests: $client->request(‘httpMethod’, ‘url’);
● Navigate:
○ $client->back();
○ $client->forward();
○ $client->reload();
● Submit forms: $client->submit($domElement, [formInfo, ...]);
● Execute clicks on links: $client->click($domElement);
6767
Automated Tests
Functional Tests - Crawler Object
The Crawler allows you to navigate/traverse through a DOM structure in HTML or XML.
6868
Automated Tests
Functional Tests - Crawler Object
The Crawler allows you to navigate/traverse through a DOM structure in HTML or XML.
6969
Automated Tests
Functional Tests - Crawler Object
The Crawler allows you to navigate/traverse through a DOM structure in HTML or XML.
Not like this, though…
just because mooountains! :)
7070
Automated Tests
Functional Tests - Crawler Object
The Crawler allows you to navigate/traverse through a DOM structure in HTML or XML.
Actually like this!
7171
Automated Tests
Functional Tests - Crawler Object
E.g.: You want to select the first parent of the last submit button.
7272
Automated Tests
Functional Tests - Crawler Object
You can also extract information from the selected nodes:
7373
Automated Tests
Functional Tests - Crawler Object
You can also extract information from the selected nodes:
● Attribute value: $crawler->attr(‘attributeName’);
7474
Automated Tests
Functional Tests - Crawler Object
You can also extract information from the selected nodes:
● Attribute value: $crawler->attr(‘attributeName’);
● Text: $crawler->text();
7575
Automated Tests
Functional Tests - Crawler Object
You can also extract information from the selected nodes:
● Attribute value: $crawler->attr(‘attributeName’);
● Text: $crawler->text();
● Multiple attributes: $crawler->extract([‘attr1’, ‘attr2’, ...]);
7676
Automated Tests
Functional Tests - Crawler Object
You can also extract information from the selected nodes:
● Attribute value: $crawler->attr(‘attributeName’);
● Text: $crawler->text();
● Multiple attributes: $crawler->extract([‘attr1’, ‘attr2’, ...]);
Or use shortcuts provided by the Crawler Object:
7777
Automated Tests
Functional Tests - Crawler Object
You can also extract information from the selected nodes:
● Attribute value: $crawler->attr(‘attributeName’);
● Text: $crawler->text();
● Multiple attributes: $crawler->extract([‘attr1’, ‘attr2’, ...]);
Or use shortcuts provided by the Crawler Object:
● Select link by name: $crawler->selectLink(‘seeFreeStuff’);
7878
Automated Tests
Functional Tests - Crawler Object
You can also extract information from the selected nodes:
● Attribute value: $crawler->attr(‘attributeName’);
● Text: $crawler->text();
● Multiple attributes: $crawler->extract([‘attr1’, ‘attr2’, ...]);
Or use shortcuts provided by the Crawler Object:
● Select link by name: $crawler->selectLink(‘seeFreeStuff’);
● Select button by name: $crawler->selectButton(‘getFreeStuff’);
7979
Automated Tests
Functional Tests - Crawler Object
Pro tip (they won’t tell you this for the certification):
8080
Automated Tests
Functional Tests - Crawler Object
Pro tip (they won’t tell you this for the certification):
8181
Automated Tests
Functional Tests - Profile Object
To enable the profiler to be used in tests:
8282
Automated Tests
Functional Tests - Profile Object
To enable the profiler to be used in tests:
This tells the profiler to only collect the data when $client->enableProfiler() is called.
8383
Automated Tests
Functional Tests - Profile Object
To use the profile data in your tests:
8484
Automated Tests
Functional Tests - Profile Object
To use the profile data in your tests:
1. Enable the profiler locally.
8585
Automated Tests
Functional Tests - Profile Object
To use the profile data in your tests:
1. Enable the profiler locally.
2. Execute the request.
8686
Automated Tests
Functional Tests - Profile Object
To use the profile data in your tests:
1. Enable the profiler locally.
2. Execute the request.
3. Assert against the metrics provided by
the profiler.
8787
Automated Tests
Functional Tests - Framework Objects Access
A.k.a. “Accessing the Container”.
8888
Automated Tests
Functional Tests - Framework Objects Access
A.k.a. “Accessing the Container”.
Although it’s recommended to test only the response, you can access the DI Container:
8989
Automated Tests
Functional Tests - Framework Objects Access
A.k.a. “Accessing the Container”.
Although it’s recommended to test only the response, you can access the DI Container:
$client->getContainer();
9090
When using Symfony’s client, it creates a Kernel with the environment set to “test”.
Symfony then loads the config from app/config/config_test.yml.
Automated Tests
Functional Tests - Client Configuration
9191
When using Symfony’s client, it creates a Kernel with the environment set to “test”.
Symfony then loads the config from app/config/config_test.yml.
When creating the client you can override some of its options:
Automated Tests
Functional Tests - Client Configuration
9292
When using Symfony’s client, it creates a Kernel with the environment set to “test”.
Symfony then loads the config from app/config/config_test.yml.
When creating the client you can override some of its options:
● The environment:
○ static::createClient([‘environment’ => ‘my_own_env’]);
Automated Tests
Functional Tests - Client Configuration
9393
When using Symfony’s client, it creates a Kernel with the environment set to “test”.
Symfony then loads the config from app/config/config_test.yml.
When creating the client you can override some of its options:
● The environment:
○ static::createClient([‘environment’ => ‘my_own_env’]);
● The debug mode:
○ static::createClient([‘debug’ => false]);
Automated Tests
Functional Tests - Client Configuration
9494
When using Symfony’s client, it creates a Kernel with the environment set to “test”.
Symfony then loads the config from app/config/config_test.yml.
When creating the client you can override some of its options:
● The environment:
○ static::createClient([‘environment’ => ‘my_own_env’]);
● The debug mode:
○ static::createClient([‘debug’ => false]);
● HTTP Headers:
○ static::createClient([], [‘HTTP_USER_AGENT’ = > ‘Exoclient/1.0’]);
Automated Tests
Functional Tests - Client Configuration
9595
When using Symfony’s client, it creates a Kernel with the environment set to “test”.
Symfony then loads the config from app/config/config_test.yml.
When creating the client you can override some of its options:
● The environment:
○ static::createClient([‘environment’ => ‘my_own_env’]);
● The debug mode:
○ static::createClient([‘debug’ => false]);
● HTTP Headers:
○ static::createClient([], [‘HTTP_USER_AGENT’ => ‘Exoclient/1.0’]);
You can also override the HTTP Headers for a specific request:
$client->request->(‘METHOD’, ‘URL’, [], [], [‘HTTP_USER_AGENT’ => ‘Exoclient/1.0’]);
Automated Tests
Functional Tests - Client Configuration
9696
Automated Tests
Functional Tests - Request and Response Introspection
You also have access to internal objects of the client, e.g.:
● History: $client->getHistory();
9797
Automated Tests
Functional Tests - Request and Response Introspection
You also have access to internal objects of the client, e.g.:
● History: $client->getHistory();
● Cookies: $client->getCookieJar();
9898
Automated Tests
Functional Tests - Request and Response Introspection
You also have access to internal objects of the client, e.g.:
● History: $client->getHistory();
● Cookies: $client->getCookieJar();
● Response: $client->getResponse();
9999
Automated Tests
Functional Tests - Request and Response Introspection
You also have access to internal objects of the client, e.g.:
● History: $client->getHistory();
● Cookies: $client->getCookieJar();
● Response: $client->getResponse();
● Crawler (DOM Navigator): $client->getCrawler();
100100
4. References
101101
Automated Tests
References
● Symfony and tests
○ https://symfony.com/doc/3.4/testing.html
● PHPUnit Bridge
○ https://symfony.com/doc/3.4/components/phpunit_bridge.html
● PHPUnit Manual
○ https://phpunit.de/manual/current/en/index.html
● Book: “The Clean Coder” by Uncle Bob
○ https://ww.goodreads.com/book/show/10284614-the-clean-coder
● Book: “Test Driven Development” by Kent Beck
○ https://www.goodreads.com/book/show/387190.Test_Driven_Development
● Marting Fowler about the “Test Pyramid”
○ https://martinfowler.com/bliki/TestPyramid.html

More Related Content

What's hot

Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.Deepak Singhvi
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDDDror Helper
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing FundamentalsRichard Paul
 
Unit & integration testing
Unit & integration testingUnit & integration testing
Unit & integration testingPavlo Hodysh
 
Intro To Unit and integration Testing
Intro To Unit and integration TestingIntro To Unit and integration Testing
Intro To Unit and integration TestingPaul Churchward
 
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockitoshaunthomas999
 
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019Sam Brannen
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveAlvaro Videla
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingJoe Tremblay
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonCefalo
 
Practical unit testing in c & c++
Practical unit testing in c & c++Practical unit testing in c & c++
Practical unit testing in c & c++Matt Hargett
 
Unit Testing Done Right
Unit Testing Done RightUnit Testing Done Right
Unit Testing Done RightBrian Fenton
 
JUnit 5 - The Next Generation
JUnit 5 - The Next GenerationJUnit 5 - The Next Generation
JUnit 5 - The Next GenerationKostadin Golev
 
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Jacinto Limjap
 

What's hot (20)

Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
Testing In Java
Testing In JavaTesting In Java
Testing In Java
 
Unit & integration testing
Unit & integration testingUnit & integration testing
Unit & integration testing
 
Intro To Unit and integration Testing
Intro To Unit and integration TestingIntro To Unit and integration Testing
Intro To Unit and integration Testing
 
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
 
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud Shaon
 
Unit test
Unit testUnit test
Unit test
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
Practical unit testing in c & c++
Practical unit testing in c & c++Practical unit testing in c & c++
Practical unit testing in c & c++
 
Unit Testing Done Right
Unit Testing Done RightUnit Testing Done Right
Unit Testing Done Right
 
JUnit 5 - The Next Generation
JUnit 5 - The Next GenerationJUnit 5 - The Next Generation
JUnit 5 - The Next Generation
 
Modern Python Testing
Modern Python TestingModern Python Testing
Modern Python Testing
 
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
 

Similar to Test Automation

How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.Matt Eland
 
Software Testing
Software TestingSoftware Testing
Software TestingAdroitLogic
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingSahar Nofal
 
Agile Software Testing the Agilogy Way
Agile Software Testing the Agilogy WayAgile Software Testing the Agilogy Way
Agile Software Testing the Agilogy WayJordi Pradel
 
Getting Started with Test-Driven Development at Longhorn PHP 2023
Getting Started with Test-Driven Development at Longhorn PHP 2023Getting Started with Test-Driven Development at Longhorn PHP 2023
Getting Started with Test-Driven Development at Longhorn PHP 2023Scott Keck-Warren
 
Test automation principles, terminologies and implementations
Test automation principles, terminologies and implementationsTest automation principles, terminologies and implementations
Test automation principles, terminologies and implementationsSteven Li
 
Test automation engineer
Test automation engineerTest automation engineer
Test automation engineerSadaaki Emura
 
Cypress Best Pratices for Test Automation
Cypress Best Pratices for Test AutomationCypress Best Pratices for Test Automation
Cypress Best Pratices for Test AutomationKnoldus Inc.
 
Agile Acceptance testing with Fitnesse
Agile Acceptance testing with FitnesseAgile Acceptance testing with Fitnesse
Agile Acceptance testing with FitnesseClareMcLennan
 
JAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineJAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineAnup Singh
 
Automated testing overview
Automated testing overviewAutomated testing overview
Automated testing overviewAlex Pop
 
Context Driven Automation Gtac 2008
Context Driven Automation Gtac 2008Context Driven Automation Gtac 2008
Context Driven Automation Gtac 2008Pete Schneider
 
An Automation Framework That Really Works
An Automation Framework That Really WorksAn Automation Framework That Really Works
An Automation Framework That Really WorksBasivi Reddy Junna
 
Test-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in SwiftTest-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in SwiftAmey Tavkar
 
WSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
WSO2Con Asia 2014 - Effective Test Automation in an Agile EnvironmentWSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
WSO2Con Asia 2014 - Effective Test Automation in an Agile EnvironmentWSO2
 

Similar to Test Automation (20)

Automated testing
Automated testingAutomated testing
Automated testing
 
How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Agile Software Testing the Agilogy Way
Agile Software Testing the Agilogy WayAgile Software Testing the Agilogy Way
Agile Software Testing the Agilogy Way
 
Getting Started with Test-Driven Development at Longhorn PHP 2023
Getting Started with Test-Driven Development at Longhorn PHP 2023Getting Started with Test-Driven Development at Longhorn PHP 2023
Getting Started with Test-Driven Development at Longhorn PHP 2023
 
Test automation principles, terminologies and implementations
Test automation principles, terminologies and implementationsTest automation principles, terminologies and implementations
Test automation principles, terminologies and implementations
 
Test automation engineer
Test automation engineerTest automation engineer
Test automation engineer
 
Ui Testing with Ghost Inspector
Ui Testing with Ghost InspectorUi Testing with Ghost Inspector
Ui Testing with Ghost Inspector
 
Cypress Best Pratices for Test Automation
Cypress Best Pratices for Test AutomationCypress Best Pratices for Test Automation
Cypress Best Pratices for Test Automation
 
Agile Acceptance testing with Fitnesse
Agile Acceptance testing with FitnesseAgile Acceptance testing with Fitnesse
Agile Acceptance testing with Fitnesse
 
Ch11lect1 ud
Ch11lect1 udCh11lect1 ud
Ch11lect1 ud
 
JAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineJAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & Jasmine
 
Automated testing overview
Automated testing overviewAutomated testing overview
Automated testing overview
 
Context Driven Automation Gtac 2008
Context Driven Automation Gtac 2008Context Driven Automation Gtac 2008
Context Driven Automation Gtac 2008
 
An Automation Framework That Really Works
An Automation Framework That Really WorksAn Automation Framework That Really Works
An Automation Framework That Really Works
 
Test-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in SwiftTest-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in Swift
 
Tdd in swift
Tdd in swiftTdd in swift
Tdd in swift
 
WSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
WSO2Con Asia 2014 - Effective Test Automation in an Agile EnvironmentWSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
WSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
 
Wso2con test-automation
Wso2con test-automationWso2con test-automation
Wso2con test-automation
 

Recently uploaded

KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 

Recently uploaded (20)

KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 

Test Automation

  • 1. 11 Automated Tests Rodrigo Paiva 1. Why should I write tests? 2. Unit Tests with PHPUnit - Characteristics - Anatomy of a test 3. Functional Tests - Overview - Client Object - Crawler Object - Profile Object - Framework Object Access - Client Configuration - Request and Response objects introspection 4. References
  • 2. 22 1. Why should I write tests?
  • 3. 33 Automated Tests Why should I write tests? ● To find bugs before our customers do it for us
  • 4. 44 Automated Tests Why should I write tests? ● To find bugs before our customers do it for us ● To not be called in the middle of the night
  • 5. 55 Automated Tests Why should I write tests? ● To find bugs before our customers do it for us ● To not be called in the middle of the night ● Professionalism
  • 6. 66 Automated Tests Why should I write tests? Every time a bug goes to production it means you as a programmer failed to do your job. Robert C. Martin (Uncle Bob) in “The Clean Coder”
  • 7. 77 Automated Tests Why should I write tests? ● To find bugs before our customers do it for us ● To not be called in the middle of the night ● Professionalism ● Customers lose confidence when they find bugs
  • 8. 88 Automated Tests Why should I write tests? ● To find bugs before our customers do it for us ● To not be called in the middle of the night ● Professionalism ● Customers lose confidence when they find bugs ● Increases the developer confidence in refactoring and changing things
  • 9. 99 Automated Tests Why should I write tests? ● To find bugs before our customers do it for us ● To not be called in the middle of the night ● Professionalism ● Customers lose confidence when they find bugs ● Increases the developer confidence in refactoring and changing things ● Actually reduces costs in a long term
  • 11. 1111 Automated Tests Why should I write tests? How many tests of each type should I have? Test pyramid for the win!
  • 12. 1212 Automated Tests Why should I write tests? Test Pyramid Speed of execution Slow Fast Cost Expensive Cheap Debugging Exploratory Precise
  • 14. 1414 Automated Tests Unit Tests with PHPUnit Installation: composer require --dev phpunit/phpunit
  • 15. 1515 Automated Tests Unit Tests with PHPUnit Installation: composer require --dev phpunit/phpunit Symfony suggests using the .phar file but the composer version is more maintainable* and it’s available for everyone in the project. *My opinion :)
  • 16. 1616 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: ● Isolated
  • 17. 1717 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: ● Isolated ● Runs very fast
  • 18. 1818 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: ● Isolated ● Runs very fast ● Reliable (not failing randomly)
  • 19. 1919 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: ● Isolated ● Runs very fast ● Reliable (not failing randomly) ● No logic inside
  • 20. 2020 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: ● Isolated ● Runs very fast ● Reliable (not failing randomly) ● No logic inside ● Assertion on only one object
  • 21. 2121 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: Isolated ● No actual access to other components, e.g.: API, database, queue server or file system
  • 22. 2222 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: Isolated ● No actual access to other components, e.g.: API, database, queue server or file system ● Tests shouldn’t depend on the behaviour of other systems
  • 23. 2323 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: Isolated ● No actual access to other components, e.g.: API, database, queue server or file system ● Tests shouldn’t depend on the behaviour of other systems ● Mock everything that the tested code doesn’t control
  • 24. 2424 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: Isolated ● No actual access to other components, e.g.: API, database, queue server or file system ● Tests shouldn’t depend on the behaviour of other systems ● Mock everything that the tested code doesn’t control ● Produces deterministic behavior
  • 25. 2525 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: Runs very fast ● Some milliseconds
  • 26. 2626 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: Runs very fast ● Some milliseconds ● Feedback loop is instantaneous
  • 27. 2727 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: Runs very fast ● Some milliseconds ● Feedback loop is instantaneous ● Developers won’t be discouraged from running them
  • 28. 2828 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: No logic inside ● No flow control (if, else, etc.)
  • 29. 2929 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: No logic inside ● No flow control (if, else, etc.) ● No loops (while, for, etc.)
  • 30. 3030 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: No logic inside ● No flow control (if, else, etc.) ● No loops (while, for, etc.) ● No goto
  • 31. 3131 Automated Tests Unit Tests with PHPUnit Characteristics of good unit tests: No logic inside ● No flow control (if, else, etc.) ● No loops (while, for, etc.) ● No goto - I’m kidding about this one
  • 32. 3232 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 1. Extends from TestCase
  • 33. 3333 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 1. Extends from TestCase 2. Class name ends with *Test
  • 34. 3434 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 1. Extends from TestCase 2. Class name ends with *Test 3. Method names start with test*
  • 35. 3535 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 1. Extends from TestCase 2. Class name ends with *Test 3. Method names start with test* a. You can also use the annotation @test instead of the prefix. Use your project’s standard.
  • 36. 3636 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 1. Extends from TestCase 2. Class name ends with *Test 3. Method names start with test* a. You can also use the annotation @test instead of the prefix. Use your project’s standard. 4. Method names describe what is being tested
  • 37. 3737 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 1. Extends from TestCase 2. Class name ends with *Test 3. Method names start with test* a. You can also use the annotation @test instead of the prefix. Use your project’s standard. 4. Method names describe what is being tested 5. Body has three stages
  • 38. 3838 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 1. Extends from TestCase 2. Class name ends with *Test 3. Method names start with test* a. You can also use the annotation @test instead of the prefix. Use your project’s standard. 4. Method names describe what is being tested 5. Body has three stages a. Prepare
  • 39. 3939 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 1. Extends from TestCase 2. Class name ends with *Test 3. Method names start with test* a. You can also use the annotation @test instead of the prefix. Use your project’s standard. 4. Method names describe what is being tested 5. Body has three stages a. Prepare b. Execute
  • 40. 4040 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 1. Extends from TestCase 2. Class name ends with *Test 3. Method names start with test* a. You can also use the annotation @test instead of the prefix. Use your project’s standard. 4. Method names describe what is being tested 5. Body has three stages a. Prepare b. Execute c. Assert
  • 41. 4141 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 3
  • 42. 4242 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 3 4
  • 43. 4343 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 3 4 5a
  • 44. 4444 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 3 4 5a 5b
  • 45. 4545 Automated Tests Unit Tests with PHPUnit Anatomy of an Unit Test 3 4 5a 5b 5c
  • 46. 4646 Automated Tests Unit Tests with PHPUnit Location of test files: tests/<bundleName> Running the tests via console:
  • 47. 4747 Automated Tests Unit Tests with PHPUnit Location of test files: tests/<bundleName> Running the tests via console: This is nice, but you should integrate phpunit into your PHPStorm, so that running tests becomes a matter of pressing a key combination.
  • 49. 4949 Automated Tests Functional Tests I have 100% code coverage with Unit Tests. Why should I additionally write functional tests?
  • 50. 5050 Automated Tests Functional Tests I have 100% code coverage with Unit Tests. Why should I additionally write functional tests?
  • 51. 5151 Automated Tests Functional Tests - Overview Functional Tests simulate the user behavior throughout the system and guarantee that: ● Components and systems work together as expected
  • 52. 5252 Automated Tests Functional Tests - Overview Functional Tests simulate the user behavior throughout the system and guarantee that: ● Components and systems work together as expected ● Your system functions as it should as a whole
  • 53. 5353 Automated Tests Functional Tests - Overview Functional Tests simulate the user behavior throughout the system and guarantee that: ● Components and systems work together as expected ● Your system functions as it should as a whole ● The acceptance criterias in the story are met
  • 54. 5454 Automated Tests Functional Tests - Overview Functional Tests simulate the user behavior throughout the system and guarantee that: ● Components and systems work together as expected ● Your system functions as it should as a whole ● The acceptance criterias in the story are met ● Your Product Owner/Manager is happy
  • 55. 5555 Automated Tests Functional Tests - Overview The anatomy of a functional test with PHPUnit is not very different from an unit test.
  • 56. 5656 Automated Tests Functional Tests - Overview The anatomy of a functional test with PHPUnit is not very different from an unit test. With Symfony, you extend it from WebTestCase instead of TestCase.
  • 57. 5757 Automated Tests Functional Tests - Overview The anatomy of a functional test with PHPUnit is not very different from an unit test. With Symfony, you extend it from WebTestCase instead of TestCase. 1. Prepare (create the client)
  • 58. 5858 Automated Tests Functional Tests - Overview The anatomy of a functional test with PHPUnit is not very different from an unit test. With Symfony, you extend it from WebTestCase instead of TestCase. 1. Prepare (create the client) 2. Execute (make an actual request)
  • 59. 5959 Automated Tests Functional Tests - Overview The anatomy of a functional test with PHPUnit is not very different from an unit test. With Symfony, you extend it from WebTestCase instead of TestCase. 1. Prepare (create the client) 2. Execute (make an actual request) 3. Assert (navigate through the DOM)
  • 60. 6060 Automated Tests Functional Tests - Overview Location of your tests: tests/<bundleName>/Controller
  • 61. 6161 Automated Tests Functional Tests - Overview Location of your tests: tests/<bundleName>/Controller In case your kernel is not in the standard directory, add this to your phpunit.xml:
  • 62. 6262 Automated Tests Functional Tests - Client Object When you create a client you have access to an object with browsing capabilities. You can: ● Execute requests: $client->request(‘httpMethod’, ‘url’);
  • 63. 6363 Automated Tests Functional Tests - Client Object When you create a client you have access to an object with browsing capabilities. You can: ● Execute requests: $client->request(‘httpMethod’, ‘url’); ● Navigate: ○ $client->back(); ○ $client->forward(); ○ $client->reload();
  • 64. 6464 Automated Tests Functional Tests - Client Object When you create a client you have access to an object with browsing capabilities. You can: ● Execute requests: $client->request(‘httpMethod’, ‘url’); ● Navigate: ○ $client->back(); ○ $client->forward(); ○ $client->reload(); ● Submit forms: $client->submit($domElement, [formInfo, ...]);
  • 65. 6565 Automated Tests Functional Tests - Client Object When you create a client you have access to an object with browsing capabilities. You can: ● Execute requests: $client->request(‘httpMethod’, ‘url’); ● Navigate: ○ $client->back(); ○ $client->forward(); ○ $client->reload(); ● Submit forms: $client->submit($domElement, [formInfo, ...]); ● Execute clicks on links: $client->click($domElement);
  • 66. 6666 Automated Tests Functional Tests - Client Object When you create a client you have access to an object with browsing capabilities. You can: ● Execute requests: $client->request(‘httpMethod’, ‘url’); ● Navigate: ○ $client->back(); ○ $client->forward(); ○ $client->reload(); ● Submit forms: $client->submit($domElement, [formInfo, ...]); ● Execute clicks on links: $client->click($domElement);
  • 67. 6767 Automated Tests Functional Tests - Crawler Object The Crawler allows you to navigate/traverse through a DOM structure in HTML or XML.
  • 68. 6868 Automated Tests Functional Tests - Crawler Object The Crawler allows you to navigate/traverse through a DOM structure in HTML or XML.
  • 69. 6969 Automated Tests Functional Tests - Crawler Object The Crawler allows you to navigate/traverse through a DOM structure in HTML or XML. Not like this, though… just because mooountains! :)
  • 70. 7070 Automated Tests Functional Tests - Crawler Object The Crawler allows you to navigate/traverse through a DOM structure in HTML or XML. Actually like this!
  • 71. 7171 Automated Tests Functional Tests - Crawler Object E.g.: You want to select the first parent of the last submit button.
  • 72. 7272 Automated Tests Functional Tests - Crawler Object You can also extract information from the selected nodes:
  • 73. 7373 Automated Tests Functional Tests - Crawler Object You can also extract information from the selected nodes: ● Attribute value: $crawler->attr(‘attributeName’);
  • 74. 7474 Automated Tests Functional Tests - Crawler Object You can also extract information from the selected nodes: ● Attribute value: $crawler->attr(‘attributeName’); ● Text: $crawler->text();
  • 75. 7575 Automated Tests Functional Tests - Crawler Object You can also extract information from the selected nodes: ● Attribute value: $crawler->attr(‘attributeName’); ● Text: $crawler->text(); ● Multiple attributes: $crawler->extract([‘attr1’, ‘attr2’, ...]);
  • 76. 7676 Automated Tests Functional Tests - Crawler Object You can also extract information from the selected nodes: ● Attribute value: $crawler->attr(‘attributeName’); ● Text: $crawler->text(); ● Multiple attributes: $crawler->extract([‘attr1’, ‘attr2’, ...]); Or use shortcuts provided by the Crawler Object:
  • 77. 7777 Automated Tests Functional Tests - Crawler Object You can also extract information from the selected nodes: ● Attribute value: $crawler->attr(‘attributeName’); ● Text: $crawler->text(); ● Multiple attributes: $crawler->extract([‘attr1’, ‘attr2’, ...]); Or use shortcuts provided by the Crawler Object: ● Select link by name: $crawler->selectLink(‘seeFreeStuff’);
  • 78. 7878 Automated Tests Functional Tests - Crawler Object You can also extract information from the selected nodes: ● Attribute value: $crawler->attr(‘attributeName’); ● Text: $crawler->text(); ● Multiple attributes: $crawler->extract([‘attr1’, ‘attr2’, ...]); Or use shortcuts provided by the Crawler Object: ● Select link by name: $crawler->selectLink(‘seeFreeStuff’); ● Select button by name: $crawler->selectButton(‘getFreeStuff’);
  • 79. 7979 Automated Tests Functional Tests - Crawler Object Pro tip (they won’t tell you this for the certification):
  • 80. 8080 Automated Tests Functional Tests - Crawler Object Pro tip (they won’t tell you this for the certification):
  • 81. 8181 Automated Tests Functional Tests - Profile Object To enable the profiler to be used in tests:
  • 82. 8282 Automated Tests Functional Tests - Profile Object To enable the profiler to be used in tests: This tells the profiler to only collect the data when $client->enableProfiler() is called.
  • 83. 8383 Automated Tests Functional Tests - Profile Object To use the profile data in your tests:
  • 84. 8484 Automated Tests Functional Tests - Profile Object To use the profile data in your tests: 1. Enable the profiler locally.
  • 85. 8585 Automated Tests Functional Tests - Profile Object To use the profile data in your tests: 1. Enable the profiler locally. 2. Execute the request.
  • 86. 8686 Automated Tests Functional Tests - Profile Object To use the profile data in your tests: 1. Enable the profiler locally. 2. Execute the request. 3. Assert against the metrics provided by the profiler.
  • 87. 8787 Automated Tests Functional Tests - Framework Objects Access A.k.a. “Accessing the Container”.
  • 88. 8888 Automated Tests Functional Tests - Framework Objects Access A.k.a. “Accessing the Container”. Although it’s recommended to test only the response, you can access the DI Container:
  • 89. 8989 Automated Tests Functional Tests - Framework Objects Access A.k.a. “Accessing the Container”. Although it’s recommended to test only the response, you can access the DI Container: $client->getContainer();
  • 90. 9090 When using Symfony’s client, it creates a Kernel with the environment set to “test”. Symfony then loads the config from app/config/config_test.yml. Automated Tests Functional Tests - Client Configuration
  • 91. 9191 When using Symfony’s client, it creates a Kernel with the environment set to “test”. Symfony then loads the config from app/config/config_test.yml. When creating the client you can override some of its options: Automated Tests Functional Tests - Client Configuration
  • 92. 9292 When using Symfony’s client, it creates a Kernel with the environment set to “test”. Symfony then loads the config from app/config/config_test.yml. When creating the client you can override some of its options: ● The environment: ○ static::createClient([‘environment’ => ‘my_own_env’]); Automated Tests Functional Tests - Client Configuration
  • 93. 9393 When using Symfony’s client, it creates a Kernel with the environment set to “test”. Symfony then loads the config from app/config/config_test.yml. When creating the client you can override some of its options: ● The environment: ○ static::createClient([‘environment’ => ‘my_own_env’]); ● The debug mode: ○ static::createClient([‘debug’ => false]); Automated Tests Functional Tests - Client Configuration
  • 94. 9494 When using Symfony’s client, it creates a Kernel with the environment set to “test”. Symfony then loads the config from app/config/config_test.yml. When creating the client you can override some of its options: ● The environment: ○ static::createClient([‘environment’ => ‘my_own_env’]); ● The debug mode: ○ static::createClient([‘debug’ => false]); ● HTTP Headers: ○ static::createClient([], [‘HTTP_USER_AGENT’ = > ‘Exoclient/1.0’]); Automated Tests Functional Tests - Client Configuration
  • 95. 9595 When using Symfony’s client, it creates a Kernel with the environment set to “test”. Symfony then loads the config from app/config/config_test.yml. When creating the client you can override some of its options: ● The environment: ○ static::createClient([‘environment’ => ‘my_own_env’]); ● The debug mode: ○ static::createClient([‘debug’ => false]); ● HTTP Headers: ○ static::createClient([], [‘HTTP_USER_AGENT’ => ‘Exoclient/1.0’]); You can also override the HTTP Headers for a specific request: $client->request->(‘METHOD’, ‘URL’, [], [], [‘HTTP_USER_AGENT’ => ‘Exoclient/1.0’]); Automated Tests Functional Tests - Client Configuration
  • 96. 9696 Automated Tests Functional Tests - Request and Response Introspection You also have access to internal objects of the client, e.g.: ● History: $client->getHistory();
  • 97. 9797 Automated Tests Functional Tests - Request and Response Introspection You also have access to internal objects of the client, e.g.: ● History: $client->getHistory(); ● Cookies: $client->getCookieJar();
  • 98. 9898 Automated Tests Functional Tests - Request and Response Introspection You also have access to internal objects of the client, e.g.: ● History: $client->getHistory(); ● Cookies: $client->getCookieJar(); ● Response: $client->getResponse();
  • 99. 9999 Automated Tests Functional Tests - Request and Response Introspection You also have access to internal objects of the client, e.g.: ● History: $client->getHistory(); ● Cookies: $client->getCookieJar(); ● Response: $client->getResponse(); ● Crawler (DOM Navigator): $client->getCrawler();
  • 101. 101101 Automated Tests References ● Symfony and tests ○ https://symfony.com/doc/3.4/testing.html ● PHPUnit Bridge ○ https://symfony.com/doc/3.4/components/phpunit_bridge.html ● PHPUnit Manual ○ https://phpunit.de/manual/current/en/index.html ● Book: “The Clean Coder” by Uncle Bob ○ https://ww.goodreads.com/book/show/10284614-the-clean-coder ● Book: “Test Driven Development” by Kent Beck ○ https://www.goodreads.com/book/show/387190.Test_Driven_Development ● Marting Fowler about the “Test Pyramid” ○ https://martinfowler.com/bliki/TestPyramid.html