SlideShare une entreprise Scribd logo
1  sur  54
Télécharger pour lire hors ligne
LOGO
“ Add your company slogan ”
Keep it Simple Stupid
Presentation by: Thu.Nguyen
CONTENT
KiSS Concept
Q&A
Demo
PHPUnit
Unit Testing
Software Testing
KiSS Concept
What is Software Testing?
 Testing is the process of executing a program with intent
of finding errors (The art of software testing – Glenford J.Myers)
4
Our program
The output
is correct?
I1, I2, I3,
…, In, … Expected results
= ?
Obtained results
“Inputs”
- No code inspection - No code analysis
- No model checking - No bug fixing
- No debugging
Level of testing
KiSS Concept
Unit Testing
Integration Testing
Functional Testing
System Testing
Load/Stress Testing
User Accepted Testing
Regression Testing
Programmer
Tester
Testing Techniques
Functional Testing
(Black box)
 Select test cases based
on the requirement or
design specification
 Emphasize on the
external behavior of
software
Structural Testing
(White box)
 Select test cases based
on the implement of
software
 Emphasize on the internal
structure of software
KiSS Concept
Black box vs White box
KiSS Concept
Process of testing
KiSS Concept
Test
cases
Design test
cases
Prepare test
data
Run program
with test data
Test
data
Test
results
Test
reports
Compare
results to test
cases
KiSS Concept
What is unit testing?
KiSS Concept
 “In computer programming, unit testing is a method by which
individual units of source code are tested to determine if they
are fit for use. A unit is the smallest testable part of an
application.” (Wikipedia)
 A “unit ” is the smallest testable part of an application: a
function or class method.
Benefit of unit testing
 Easy to defect error
KiSS Concept
Benefit of unit testing
 Safer refactoring
KiSS Concept
Benefit of unit testing
 Automated test
KiSS Concept
Benefit of unit testing
 Long term of saving time and money
KiSS Concept
When do you write the test?
 Focus on requirements
 Thinking about how to
code will be consumed
 Stop coding when meet
requirement
 Harder initially
 Focus on code
 Thinking about algorithm
 More refactoring
 Easier initially
KiSS Concept
Before coding(TDD) After/During coding
How do I test?
 Isolate with the code being tested
 Short, simple, fast, readable
 No conditional logic or loop (if, switch, while)
 One test should be one behavior
KiSS Concept
KiSS Concept
Introduction
KiSS Concept
 Is a unit testing framework written in PHP, created by
Sebastian Bergmann.
 Part of the xUnit family of testing frameworks.
 Integrated/supported
• Zend Studio
• Zend Framework
• Symfony
• Doctrine
Installation
KiSS Concept
Install with PEAR:
pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit
Update:
pear upgrade phpunit/PHPUnit
Xdebug:
sudo apt-get install php5-xdebug php5-dev
Set include path in /etc/php5/cli/php.ini
include_path = “.:/usr/share/php”
Definitions
KiSS Concept
Test suite
Test suite
Test case
setUp()
testMethod
tearDown()
/*
*
*@annotation
*/
testMethod(){
assertion
}
Report
Test case
setUp()
testMethod
tearDown()
/*
*
*@annotation
*/
testMethod(){
assertion
}
Code coverage
Logging
Simple test
KiSS Concept
Running test
KiSS Concept
Go to command line and run:
phpunit /path/to/file/test.php or phpunit classname
/path/to/file/test.php
Running test
 Command line
 IDE
 Zend Studio
 NetBean
 Continuous Integration
 phpUnderControl
 Cruise Control
KiSS Concept
Result
 OK: all tests are successful
 FAILURES: test is fail or error
 Result of each test:
• . test succeeds
• F an assertion fails
• E an error
• S test has been skipped
• I test is marked as being incomplete or not yet
implement
KiSS Concept
Features
 @dataProvider
 @exception
 Fixture: setUp() and tearDown()
 Double: stub and mock object
 Database
 Code coverage
KiSS Concept
Data provider
 Is a method that returns an array of values to use in a test.
 Makes tests shorter and more concise.
 Data provider can use array or file csv.
KiSS Concept
Data provider
KiSS Concept
Input 1 myfunction() Report 1Result 1 Expected 1
compare
Input 2 myfunction() Report 2Result 2 Expected 2
compare
Input 3 myfunction() Report 3Result 3 Expected 3
compare
Data provider
KiSS Concept
/*
* @dataProvider
*/
testMethod($input, $expect)
{
myfunction()
}
Report 1
Data Providers
Input 1 expect1
Input 2 expect 2
Input 3 expect 3
Report 2
Report 3
Use array
KiSS Concept
Use csv file
KiSS Concept
Exception
 Test exception are thrown.
 Test expected messages of exception are thrown.
KiSS Concept
Example exception
KiSS Concept
Fixtures
 Is constructor method and destructor method in test case
 PHPUnit supports 2 methods:
• setUp(): the function run when test methods start to
run
• tearDown(): the function run after test methods end
KiSS Concept
Fixtures
KiSS Concept
Start
setUp()
testOne() testTwo() testThree()
tearDown()
End
KiSS Concept
Example fixtures
Test double
KiSS Concept
MyClass
myMethod($object)
{
…$object->otherMethod();
…
}
Database
Connection
File system
Web service
Function depends on other objects, other components
KiSS Concept
MyClass
myMethod()
{
…other Method
…
}
Database
File system
Web service
MyClassTest
testMyMethod()
{
…
…
}
Test double
How to isolate environment ?
Test double
KiSS Concept
MyClass
myMethod()
{
…other Method
…
}
Database Mock
File system Mock
Web service Mock
MyClassTest
testMyMethod()
{
…
…
}
Set mocks
Solution: use copy object Test double
return fixed value
Stub and Mock
 Set method return
values
 Test state
 Check method calls
 Check arguments used
 Test interactions
KiSS Concept
Stub Mock
Example stub object
KiSS Concept
Example mock object
KiSS Concept
Database testing
 Set up database connection.
 Set up initial dataset.
 Provide methods to get dataset from database.
 Provide methods to compare two datasets.
 Type of dataset
 Flat XML
 XML
 CSV
 MySQL dump
KiSS Concept
Organizing
 Allow to organize tests to test suite to run one time
 Use file system:
 All test files end with „Test‟.
 Organize directory structure.
 Use configuration file: phpunit.xml
KiSS Concept
Use file system
KiSS Concept
Use phpunit.xml
KiSS Concept
Code Coverage
KiSS Concept
 Help to see what are executed when the tests are run.
 Help to find code that is not yet tested.
 Help to measure testing completeness.
Code Coverage
KiSS Concept
Code Coverage
KiSS Concept
Red: code is not executedGreen: code is executed
Test cases cover this line code
Grey: dead code
Note: dead code is source code which is executed but whose result is never used.
Logging
Test results HTML
 <log type="testdox-html" target="./log/testdox.html" />
KiSS Concept
Logging
Test result XML
 <log type="junit" target="./log/logfile.xml" />
KiSS Concept
KiSS Concept
References
 PHPUnit manual
 Training course PHPUnit – Nick Belhome,2010
 Unit Testing with PHPUnit – Michelangelo van Dam
 Ini2it Unit Testing after ZF 1.8 - Michelangelo van Dam
 PHPUnit From Zero To Hero – Jeremy Cook
 PHPUnit & Continous Intergration – AlexMace
 Advanced PHPUnit Testing – Mike Lively
 Practical PHP Testing Patterns
 http://framework.zend.com/manual/1.12/ru/zend.test.phpunit.db.html
KiSS Concept
QUESTION & ANSWEAR
KiSS Concept
LOGO
“ Add your company slogan ”
Keep it Simple Stupid

Contenu connexe

Tendances

Maven Tutorial for Beginners | Edureka
Maven Tutorial for Beginners | EdurekaMaven Tutorial for Beginners | Edureka
Maven Tutorial for Beginners | EdurekaEdureka!
 
Modernizing applications with Amazon EKS - MAD304 - Santa Clara AWS Summit.pdf
Modernizing applications with Amazon EKS - MAD304 - Santa Clara AWS Summit.pdfModernizing applications with Amazon EKS - MAD304 - Santa Clara AWS Summit.pdf
Modernizing applications with Amazon EKS - MAD304 - Santa Clara AWS Summit.pdfAmazon Web Services
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBaskar K
 
PHP 8.1 - What's new and changed
PHP 8.1 - What's new and changedPHP 8.1 - What's new and changed
PHP 8.1 - What's new and changedAyesh Karunaratne
 
How to test infrastructure code: automated testing for Terraform, Kubernetes,...
How to test infrastructure code: automated testing for Terraform, Kubernetes,...How to test infrastructure code: automated testing for Terraform, Kubernetes,...
How to test infrastructure code: automated testing for Terraform, Kubernetes,...Yevgeniy Brikman
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
KGC2010 - 낡은 코드에 단위테스트 넣기
KGC2010 - 낡은 코드에 단위테스트 넣기KGC2010 - 낡은 코드에 단위테스트 넣기
KGC2010 - 낡은 코드에 단위테스트 넣기Ryan Park
 
Securing Infrastructure with OpenScap The Automation Way !!
Securing Infrastructure with OpenScap The Automation Way !!Securing Infrastructure with OpenScap The Automation Way !!
Securing Infrastructure with OpenScap The Automation Way !!Jaskaran Narula
 
Gitlab CI : Integration et Déploiement Continue
Gitlab CI : Integration et Déploiement ContinueGitlab CI : Integration et Déploiement Continue
Gitlab CI : Integration et Déploiement ContinueVincent Composieux
 
Docker networking Tutorial 101
Docker networking Tutorial 101Docker networking Tutorial 101
Docker networking Tutorial 101LorisPack Project
 
Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++Lars Thorup
 

Tendances (20)

Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Maven Tutorial for Beginners | Edureka
Maven Tutorial for Beginners | EdurekaMaven Tutorial for Beginners | Edureka
Maven Tutorial for Beginners | Edureka
 
Phpunit testing
Phpunit testingPhpunit testing
Phpunit testing
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Modernizing applications with Amazon EKS - MAD304 - Santa Clara AWS Summit.pdf
Modernizing applications with Amazon EKS - MAD304 - Santa Clara AWS Summit.pdfModernizing applications with Amazon EKS - MAD304 - Santa Clara AWS Summit.pdf
Modernizing applications with Amazon EKS - MAD304 - Santa Clara AWS Summit.pdf
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
 
Maven ppt
Maven pptMaven ppt
Maven ppt
 
PHP 8.1 - What's new and changed
PHP 8.1 - What's new and changedPHP 8.1 - What's new and changed
PHP 8.1 - What's new and changed
 
How to test infrastructure code: automated testing for Terraform, Kubernetes,...
How to test infrastructure code: automated testing for Terraform, Kubernetes,...How to test infrastructure code: automated testing for Terraform, Kubernetes,...
How to test infrastructure code: automated testing for Terraform, Kubernetes,...
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
KGC2010 - 낡은 코드에 단위테스트 넣기
KGC2010 - 낡은 코드에 단위테스트 넣기KGC2010 - 낡은 코드에 단위테스트 넣기
KGC2010 - 낡은 코드에 단위테스트 넣기
 
Securing Infrastructure with OpenScap The Automation Way !!
Securing Infrastructure with OpenScap The Automation Way !!Securing Infrastructure with OpenScap The Automation Way !!
Securing Infrastructure with OpenScap The Automation Way !!
 
Selenium ppt
Selenium pptSelenium ppt
Selenium ppt
 
Bizweb Microservices Architecture
Bizweb Microservices ArchitectureBizweb Microservices Architecture
Bizweb Microservices Architecture
 
Gitlab CI : Integration et Déploiement Continue
Gitlab CI : Integration et Déploiement ContinueGitlab CI : Integration et Déploiement Continue
Gitlab CI : Integration et Déploiement Continue
 
Docker networking Tutorial 101
Docker networking Tutorial 101Docker networking Tutorial 101
Docker networking Tutorial 101
 
Git hooks
Git hooksGit hooks
Git hooks
 
Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
 

Similaire à Keep It Simple Stupid: Unit Testing Basics

Automation Framework 042009 V2
Automation Framework   042009  V2Automation Framework   042009  V2
Automation Framework 042009 V2Devukjs
 
Continuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CIContinuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CIwajrcs
 
Strategy-driven Test Generation with Open Source Frameworks
Strategy-driven Test Generation with Open Source FrameworksStrategy-driven Test Generation with Open Source Frameworks
Strategy-driven Test Generation with Open Source FrameworksDimitry Polivaev
 
Testing in Craft CMS
Testing in Craft CMSTesting in Craft CMS
Testing in Craft CMSJustinHolt20
 
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql ServerDavid P. Moore
 
Automation Framework 042009 V2
Automation Framework   042009  V2Automation Framework   042009  V2
Automation Framework 042009 V2guestb66d91
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsOrtus Solutions, Corp
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)Rob Hale
 
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)
 
Automated Testing with Databases
Automated Testing with DatabasesAutomated Testing with Databases
Automated Testing with Databaseselliando dias
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional ProgrammerDave Cross
 
The Art Of Debugging
The Art Of DebuggingThe Art Of Debugging
The Art Of Debuggingsvilen.ivanov
 
Introduction to Behavior Driven Development
Introduction to Behavior Driven Development Introduction to Behavior Driven Development
Introduction to Behavior Driven Development Robin O'Brien
 
Coldbox developer training – session 4
Coldbox developer training – session 4Coldbox developer training – session 4
Coldbox developer training – session 4Billie Berzinskas
 
SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)Amr E. Mohamed
 
Test Driven Development Introduction
Test Driven Development IntroductionTest Driven Development Introduction
Test Driven Development IntroductionNguyen Hai
 

Similaire à Keep It Simple Stupid: Unit Testing Basics (20)

Automation Framework 042009 V2
Automation Framework   042009  V2Automation Framework   042009  V2
Automation Framework 042009 V2
 
Continuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CIContinuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CI
 
Strategy-driven Test Generation with Open Source Frameworks
Strategy-driven Test Generation with Open Source FrameworksStrategy-driven Test Generation with Open Source Frameworks
Strategy-driven Test Generation with Open Source Frameworks
 
Test box bdd
Test box bddTest box bdd
Test box bdd
 
Testing in Craft CMS
Testing in Craft CMSTesting in Craft CMS
Testing in Craft CMS
 
Python and test
Python and testPython and test
Python and test
 
Gallio Crafting A Toolchain
Gallio Crafting A ToolchainGallio Crafting A Toolchain
Gallio Crafting A Toolchain
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql Server
 
Automation Framework 042009 V2
Automation Framework   042009  V2Automation Framework   042009  V2
Automation Framework 042009 V2
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
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
 
Automated Testing with Databases
Automated Testing with DatabasesAutomated Testing with Databases
Automated Testing with Databases
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional Programmer
 
The Art Of Debugging
The Art Of DebuggingThe Art Of Debugging
The Art Of Debugging
 
Introduction to Behavior Driven Development
Introduction to Behavior Driven Development Introduction to Behavior Driven Development
Introduction to Behavior Driven Development
 
Coldbox developer training – session 4
Coldbox developer training – session 4Coldbox developer training – session 4
Coldbox developer training – session 4
 
SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)
 
Test Driven Development Introduction
Test Driven Development IntroductionTest Driven Development Introduction
Test Driven Development Introduction
 

Dernier

Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...BookNet Canada
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
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
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...Karmanjay Verma
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Mark Simos
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Jeffrey Haguewood
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 

Dernier (20)

Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
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
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 

Keep It Simple Stupid: Unit Testing Basics

Notes de l'éditeur

  1. - Kiểm thử phần mềm là quá trình thực thi một hệ thống phần mềm để xác định xem phần mềm đó có đúng với đặc tả không và thực hiện trong môi trường như mong đợi hay không. - Mục đích của kiểm thử phần mềm:Tìmralỗichưađượcpháthiệnmộtcáchsớmnhất, hiệuquảnhấtĐảmbảolỗiđãđượcsửa- Kiểmthửphầnmềmkhôngkiểmtra code, khôngkiểmtra database, model, không debug, không fix bug
  2. Unit testing : làkiểu test kiểmtra code xemliệuchứcnăngnóđangthựchiệncóđúngcách hay khôngtheonhưyêucầu.Integration testing : làkiểu test kiểmtraliệutấtcảcác module làđượckếthợphoặcchưakếthợplạicùngvớinhauthựchiệncôngviệccóđạtđượckếtquảnhưtàiliệuyêucầuđãđượcxácđịnh (do mỗilậptrìnhviênthựchiệntrêncác module khácnhau. Khihọhoànthànhđoạn code củahọ, nhómquảnlýcấuhìnhrápchúnglạivớinhauvàchuẩnbịbiêndịch. Các tester cầnchắcrằngcác module nàybâygiờđãđượckếthợpvàlàmviệctheonhưyêucầu - tứclàphải test theonhưyêucầu).- Functional testing : làkiểu test liệumỗivàmọichứcnăngcủaứngdụngđóđanglàmviệccónhưyêucầucủatàiliệu. Nólàkiểu test chínhmà 80% côngviệc test đượcthựchiện. Trongkiểu test nàythìcáctestcaseđượcthựchiện (hoặcthihành).- System testing:Khi tester hoànthànhcôngviệc test (các tester test ứngdụngtrongcácmôitrường test, nghĩalàhọ test vớidữliệu test, không test trêndữliệuthật), ứngdụng (phầnmềm) phảiđược test trênmôitrườngthật. Nónghĩalàgì, tứclàkểtừkhicác tester test nótrongmôitrường test vớidữliệu test, chúng ta phảichắcchắnrằngứngdụnglàmviệctốttrongmôitrườngthậtvớidữliệuthật. Trongmôitrường test, mộtvàiđiềukhôngthể test hoặcthaotácgiả. Tấtcảsẽkhácnhauvàcơsởdữliệukhácnhau, mộtsốthaotáccóthểkhônglàmviệcnhưmongđợikhiứngdụngđượcchuyểntừmôitrường test sang môitrườngsảnphẩm (test enviroment to production environment).- Load testing ? Làkiểu test kiểmtrathờigianđáplạingườidùngvớiứngsốlượngngườidùngbấtkỳtrongmộtngữcảnhnàođócủacùngmộtứngdụngtạicùngmộtthờiđiểm.- Stress testing làgì? Làkiểu test kiểmtrathờigianđáplạingườidùngvớiứngsốlượngngườidùngbấtkỳtrongnhiềungữcảnhkhácnhaucủacùngmộtứngdụngtạicùngmộtthờiđiểm.- Performance testing làgì? Trongloại test này, ứngdụngđược test dựavàosứcnặngnhưsựphứctạpcủagiátrị, độdàicủađầuvào, độdàicủacáccâutruyvấn...Loại test nàykiểmtrabớtphầntải (stress/load) củaứngdụngcóthểđượcchắcchắnhơn.- User acceptance testing làgì? Trongkiểu test này, phầnmềmsẽđượcthựchiệnkiểmtratừngườidùngđểtìmranếuphầnmềmphùhợpvớisựmongđợicủangườidùngvàthựchiệnđúngnhưmongđợi. Tronggiaiđoạn test này, tester cóthểcũngthựchiệnhoặckháchhàngcócác tester củariênghọđểthựchiện.- Regression testing làgì? Khimộtchứcnăngmớiđượcthêmvàophầnmềm, chúng ta cầnchắcchắnrằngphầnchứcnăngmớiđượcthêmvàokhôngpháhỏngcácphầnkháccủaứngdụng. Hoặckhilỗiđãđượcchỉnhsửa, chúng ta cầnchắcchắnrằnglỗichỉnhsửakhôngpháhỏngcácphầnkháctrongứngdụng. Để test điềunàychúng ta thựchiệnkiểu test lặpđilặplạigọilà test hồiquy.
  3. Functional Testing: kiểmthửchứcnăng hay còngọikiểmthửhộpđen (blackbox)Black box làloại test mà tester đưagiátrịđầuvàovàkiểmtragiátrịđầurakhôngquantâmbêntronghoạtđộngnhưthếnào.Chủyếukiểmtragiaodiện, chứcnăngthiếu hay không, vv-Dựavàođặctảyêucầumàđưaracác test case: vìlàloại test chỉquan tam hệthốngphầnmềmcóhoạtđộngđúngnhưtrongyêucầu hay không, khôngquantâmbêntronglàmnhưthếnào-Chỉchútrọnghành vi bênngoàicủaphầnmềm.Structural Testing: kiểmthửcấutrúc hay còngọikiểmthửhộptrắng(white box)White box làloại test mà developer dựavàomã code củamìnhmàđưaracác test case.Test dựavàođiềukhiển logic (if else switch), điềukhiểnvònglặp (for while), cấutrúcdữliệubêntrongDựavàoviệcthựcthicủaphầnmềmmàđưaracác test caseChútrọngcấutrúcbêntrongcủaphầnmềm
  4. Giátrịđầuvào: 2 đồngxu, gõcâyđũa ma thuậtGiátrịđầura: con thỏBlack box: khángiả chi nhìnthấyảothuậtgiabỏ 2 đồngxu, vàgõcâyđũavàocáinón, và con thỏxuấthiệnWhite box: khinhậnđược 2 đồngxuvàthấycáinónbịgõ, ngườibêntrongđưa con thỏchoảothuậtgia
  5. Tiếntrìnhkiểmthử. Kiểmthửthườngbaogồmcácbước - Thiếtkếcáccakiểmthử - Bướctạodữliệuthử + Kiêmthửvớitấtcảcácdữliệuvàolàcầnthiết. + Chọntậpcácdữliệuthửđạidiệntừmiềndữliệuvào - Bướcthựcthichươngtrìnhtrêndữliệuthử + Cungcấpdữliệuthử + Thựcthi + Ghinhậnkếtquả - Bướcquansátkếtquảkiểmthử + Thựchiệntrongkhihoặcsaukhithựcthi + So sánhkếtquảnhậnđượcvàkếtquảmongđợi
  6. Unit testinglàphươngpháp test đơnvịcủa source code đểxácđịnh code đểcóhoạtđộngđúngnhưmongđợikhông.Một unit cóthểlàmột class hoặchàmcủachươngtrình
  7. Có 2 giaiđoạnchính:Trướckhi code hay còngọilàphươngpháp TDD(Test Driven Development: từviệc test dẫnđếnviết code để past test đó) - Tậptrungvàoyêucầucủachươngtrình - Nghĩđếnviệcviết code sẽđượcsửdụng - Ngưng code khiđạtđượcyêucầucủachươngtrình - KhókhănlúcbắtđầuSaukhi code xonghoặctronglúc code - Test chủyếuvàonhữngphầnđã code - Nghĩvềthuậttoánvà logic - Thayđổi code nhiềuhơn - Dễlúcbắtđầu
  8. - Code test phảiđộclập, cáchbiệtvới code được test - Code test phảingắngọn (khoảng 10 dòng), đơngiản, nhanhvàdễđọc. - Khôngsửdụngcáccấutrúcđiềukhiểnvàvònglặptronghàm test. Mỗicâulệnh if else nêntáchthànhnhiềuhàm test khácnhau, mỗihàmlà 1 trườnghợp test - Mộthàm test chỉnên test cho 1 trạngthái. VídụtestFormAcceptValidData
  9. PHPUnit là 1 framework viếtbằng PHP bởi Sebastian Bergmann.ThuộchọxUnit framework. xUnit frameworks: these unit testing frameworks are called the xUnit frameworks because their names usually start with the first letters of the language for which they were built.You might have CppUnit for C++, Junit for Java, Nunit for .NET.ĐượctíchhợpvàhỗtrợbởiZend Studio, Zend framework hay Symfony framework, doctrine
  10. Chạybằng command line: phpunitđườngdẫnđến file testChạybằng IDE: - Zend: trướchết import library vào project: Project -&gt; Properties -&gt; PHP Include Path -&gt; tab Libraries -&gt; click nút Add Library -&gt; chọn PHPUnit 3.x -&gt; OK. Đểchạynhấpchuộtphảilên file -&gt; Run As -&gt;PHPUnit, hiệnkhungkếtquảphpunit - NetBean: kiểmtraphpunitđãthêmvàochưa: Tool -&gt; Option -&gt; tab PHP -&gt; tab Unit Testing -&gt; kiểmtrađườngdẫncủaphpunit “/usr/bin/phpunit”Đểchạy file -&gt; Run -&gt; Test file -&gt; chỉthưmụcchứa file test -&gt; OKĐểchạy test suite: right click tên project -&gt; Properties -&gt; PHPUnit , chọn file bootstrap nếucó, chọn file configuration xml nếucó, chọn check box chạytấtcả file cótênlà Test -&gt; OKĐểhiện coverage report: right click tên project -&gt; Code Coverage -&gt; show reportChạybằng Continuous Integration (CI): tíchhợpliêntụcContinuous Intergration hay CI làmộtmôitrườnghỗtrợpháttriểnphầnmềmcóchứcnănggiúpcácthànhviêntrong team tíchhợp (integrate) côngviệccủahọmộtcáchthườngxuyên, liêntục. Mỗiđiểmtíchhợpsẽđượckiểmtra (verified) bởimột qui trình build và test tựđộngđểđảmbảopháthiệnsớmnhấtnhữnglỗiphátsinhtrongquátrìnhtíchhợpđó.Giảiphápnàythựcsựgiúpchocácnhàpháttriểnphầnmềmgiảmbớtcácvấnđềphátsinhtrongquátrìnhtíchhợpvàchophépcôngviệcpháttriểnphầntrởnênmềmnhanhchóngvàgắnkếthơn. CI baohàmmộtloạtnhữngquátrìnhđượcgắnkếtvớinhaunhư: automated build, coding standard check, static analysic, unit test, depoyingvàintergation test...
  11. Cácchứcnăngchínhcủaphpunit - Test dependency - Data provider - Exception - Fixture - Double - Code coverage
  12. Vớiviệc test thôngthường, để test 1 hàm, chúng ta truyềnvàothamsốsauđólấykếtquảtrảvề so sánhvớikếtquảchúng ta mongmuốn, sauđóxuất reportTest nhiềugiátrịđầuvào, viết code lậplạinhiềulần, mấtthờigianvìvậysửdụng data provider
  13. Data provider cungcấpdữliệuđể test. Dữliệunàycóthểlàgiátrịthamsốtruyềnvàohàmhoặcgiátrịmongmuốntrảvề, v.v
  14. Yêucầuđốivớiviệc test làcácthànhphầnphảiđược test riêngrẽvớinhauđểkếtquảcủathànhphầnkhôngbịảnhhưởngbởithànhphầnkhácvídụkhichúng ta gọiđến service đểlấykếtquảvàsauđólấykếtquảđểtínhtoántiếpchocôngviệccủachúng ta thìbắtbuộckếtquảtrảvềluônluônphảiđúngđểbiếtchắcnếu test củachúng ta bị fail là do code củachúng ta chứkhôngphải do service làmsai. Đểlàmđiềuđóđôikhichúng ta phảigiảlậpnhữngthànhphầnđóđểđảmbảo test đượccôlậpvàchínhxác. Việcgiảlập 1 đốitượngđượcgọilà test doubles.Ý tưởngcủa test double chỉgiảlập 1 phầncủa object, thườnglà 1 method nàođócần test chứkhônggiảlậptoànbộ object.Test double khôngphảithiếtkếmộtchotấtcảnêntùyvàotrườnghợpmàsửdụngcácloại double khácnhau. Test double cómấyloạisau:Dummy: là 1 object màđơngiảnđượcthông qua đểthỏamãncácphươngthứccầnkiểmtra. Nólà test double đơngiảnnhấtmàbạncóthểxâydựngStub cungcấpnhữngkếtquảđóngkhiđượcgọiSpy ghilạinhữnglầngọivàthamsốđểbạncóthểkiểmtrachúngsaunàybêntronghàm testMock giốngnhư Spy nhưngkiểmtralầngọingaylậptứcdựatheonhữnggìmàbạnđãđịnhnghĩatrướcđó. Nócũngcóthểcungcấpkếtquảđóng.Fake làsựthựcthimộtcáchđơngiảnnhấtcóthểcủamột object thậtchẳnghạnnhư DAO.Trongđóchúng ta chỉchú ý đặcbiệtđến Stub và Mock vìlà 2 đốitượngthườngdùng.Stub object làbảnsaocủamột object khácđượcchúng ta địnhnghĩatrướcvàtrảvề 1 kếtquảđóngcủamộtphươngthứctrong object đócũngđượcđịnhnghĩatrướcMock : điểmkhácnhaucănbảngiữa Stub và Mock là Mock khôngchỉgiảlập object màcònchokiểmtranhữnggìbạntruyềnchonóvàcáchbạngọinó.