SlideShare a Scribd company logo
Introduction to
Unit Testing
Author : Pooya Sagharchi Ha
January 27, 2015
Agenda
● What is Unit Testing ?
● Why should we Unit Test?
● Isolate units for testing
● UnitTest++ framework
● HippoMocks framework
Unit Testing
● The smallest testable parts of an application, are called
units
● Unit Test is a method in which a programmer tests
individual units
● execute a program using artificial data
Unit Testing
input
Main Class Testing Class
output
True or False
Isolate Unit Testing
● it goal is to isolate each part of the program and show that the individual parts
are correct
Class 1
Class 2
Class 3
Class 4
Class 3
Testing Class
input
output
True or
Why Unit Testing?
● helps to find the problems early in the software
development
● Can be used to validate that the code functionality still
works after code change
UnitTest++
● lightweight
● Simplicity
● Portability ( platform Linux, Mac and windows )
● Speed
Getting started:
1. Create a header file on UnitTests folder
2. Include UnitTest++.h and TestReporterStdout.h
3. Write your test in TEST body
4. Compile it
UnitTest++
UnitTest++
For example :
#include "UnitTest++.h"
#include "TestReportsStdout.h"
TEST(ChechSumTest)
{
int sum =0;
for(int i=0; i < 5; ++i)
sum += i;
CHECK_EQUAL(10, sum);
}
UnitTest++
● Test macro
Define : simply put the following code in a .cpp file of your choice.
Syntax :
TEST(YourTestName)
{
}
UnitTest++
● Suit macro
Define : A suite serves as a namespace for test names, so that the same test name can be used in two
different contexts.
Syntax :
SU ITE(YourSuiteName)
{
TEST(YourTestName)
{
}
TEST(YourOtherTestName)
{
}
}
UnitTest++
● Check macro
Define : It will fail if the boolean expression evaluates to false and conversely.
Syntax :
TEST(YourTestName)
{
Check(value);
}
UnitTest++
● Check Equal macro
Define : It will compare expected argument and actual argument.
Syntax :
TEST(YourTestName)
{
Check_EQUAL(expected, actual);
}
UnitTest++
● Check Close macro
Define : It is used for floating-point comparison.
Syntax :
TEST(YourTestName)
{
Check_CLOSE(expected, actual, tolerance);
}
UnitTest++
● Array macro
There is a set of check macros for array comparison as well.
Syntax :
TEST(YourTestName)
{
Check_Array_EQUAL(expected, actual, count);
Check_Array_CLOSE(expected, actual, tolerance);
Check_Array2D_EQUAL(expected, actual, row, columns, tolerance);
}
UnitTest++
● Exception check macros
Define : which asserts that its enclosed expression throws the specified type.
Syntax :
TEST(YourTestName)
{
Check_THROW(expression, ExpectedExpectionType);
}
HippoMocks
Getting started:
1. Create a header file on UnitTests folder
2. Include UnitTest++.h and TestReporterStdout.h and
hippmocks.h
3. Write your test in TEST body
4. Compile it
HippoMocks
● MockRepository class
Define : Create a MockRepository on the stack.
Syntax :
TEST(YourTestName)
{
MockRepository mocks;
}
HippoMocks
● Interface Mock
Define : Used to create an instance of a class that implements a given interface.
Syntax :
TEST(YourTestName)
{
MockRepository mocks;
ClassName *classnameMock = mocks.Mock<ClassName>();
}
HippoMocks
● Expected Call
Define : Indicates that the given method on the mock will be called. If the method is not called
before the Repository destructs, a runtime error is generated.
Syntax :
TEST(YourTestName)
{
MockRepository mocks;
ClassName *classnameMock = mocks.Mock<ClassName>();
mock.ExpectCall(obj, func);
}
HippoMocks
● Expected Call
Define : AutoExpect can be called prior to calls to ExpectCall but the order of your program
increase.
Syntax :
TEST(YourTestName)
{
MockRepository mocks;
ClassName *classnameMock = mocks.Mock<ClassName>();
mocks.autoExpect = false or true;
}
HippoMocks
● On Call
Define : Similar to ExpectCall, except that it's okay if the method doesn't get called.
Syntax :
TEST(YourTestName)
{
MockRepository mocks;
ClassName *classnameMock = mocks.Mock<ClassName>();
mock.OnCall(obj, func);
}
HippoMocks
● Never Call
Define : Similar to ExpectCall, except that it sets up expectation that the
method will never be called.
Syntax :
TEST(YourTestName)
{
MockRepository mocks;
ClassName *classnameMock = mocks.Mock<ClassName>();
mock.NeverCall(obj, func);
}
HippoMocks
● TCall class
Define : This class is not instantiated directly. It's returned by
MockRepository::ExpectCall The TCall class can be used to further refine the
expectations of the method call.
HippoMocks
● With
Define : When used, indicates what parameters expected to be passed in.
Syntax :
TEST(YourTestName)
{
MockRepository mocks;
ClassName *classnameMock = mocks.Mock<ClassName>();
mock.ExpectCall(obj, func).With(value);
}
HippoMocks
● Return
Define : When used, indicates what value the method will return.
Syntax :
TEST(YourTestName)
{
MockRepository mocks;
ClassName *classnameMock = mocks.Mock<ClassName>();
mock.ExpectCall(obj, func).Return(obj);
}
HippoMocks
● Throw
Define : When used, indicates that the method must throw an exception.
Syntax :
TEST(YourTestName)
{
MockRepository mocks;
ClassName *classnameMock = mocks.Mock<ClassName>();
mock.ExpectCall(obj, func).Throw(exception);
}
HippoMocks
● Do
Define : When used, indicates what the method will do by passing in a function pointer.
Syntax :
TEST(YourTestName)
{
MockRepository mocks;
ClassName *classnameMock = mocks.Mock<ClassName>();
mock.ExpectCall(obj, func).Do(function);
}

More Related Content

What's hot

Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
Nayanda Haberty
 
Types of testing
Types of testingTypes of testing
Types of testing
Sonam Agarwal
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
Devvrat Shukla
 
Unit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and KarmaUnit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
Vulnerability assessment and penetration testing
Vulnerability assessment and penetration testingVulnerability assessment and penetration testing
Vulnerability assessment and penetration testing
Abu Sadat Mohammed Yasin
 
Testing with Spring: An Introduction
Testing with Spring: An IntroductionTesting with Spring: An Introduction
Testing with Spring: An Introduction
Sam Brannen
 
Taller TestingUy 2019 - Test automatizado con Katalon Studio
Taller TestingUy 2019 - Test automatizado con Katalon StudioTaller TestingUy 2019 - Test automatizado con Katalon Studio
Taller TestingUy 2019 - Test automatizado con Katalon Studio
TestingUy
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
Nuha Noor
 
Software testing
Software testingSoftware testing
Software testing
mkn3009
 
Types of Software Testing | Edureka
Types of Software Testing | EdurekaTypes of Software Testing | Edureka
Types of Software Testing | Edureka
Edureka!
 
Software testing ppt
Software testing pptSoftware testing ppt
Software testing ppt
Savyasachi14
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
Prashant Cholachagudd
 
Selenium interview questions and answers
Selenium interview questions and answersSelenium interview questions and answers
Selenium interview questions and answers
kavinilavuG
 
TestNG
TestNGTestNG
Public and private APIs: differences and challenges
Public and private APIs: differences and challengesPublic and private APIs: differences and challenges
Public and private APIs: differences and challenges
Restlet
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
Onkar Deshpande
 
CTFL Module 04
CTFL Module 04CTFL Module 04
CTFL Module 04
Davis Thomas
 
Basic software-testing-concepts
Basic software-testing-conceptsBasic software-testing-concepts
Basic software-testing-concepts
medsherb
 
Firewall best-practices-firewall-analyzer
Firewall best-practices-firewall-analyzerFirewall best-practices-firewall-analyzer
Firewall best-practices-firewall-analyzer
iDric Soluciones de TI y Seguridad Informática
 
Software automation
Software automationSoftware automation
Software automation
gokilabrindha
 

What's hot (20)

Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Types of testing
Types of testingTypes of testing
Types of testing
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Unit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and KarmaUnit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and Karma
 
Vulnerability assessment and penetration testing
Vulnerability assessment and penetration testingVulnerability assessment and penetration testing
Vulnerability assessment and penetration testing
 
Testing with Spring: An Introduction
Testing with Spring: An IntroductionTesting with Spring: An Introduction
Testing with Spring: An Introduction
 
Taller TestingUy 2019 - Test automatizado con Katalon Studio
Taller TestingUy 2019 - Test automatizado con Katalon StudioTaller TestingUy 2019 - Test automatizado con Katalon Studio
Taller TestingUy 2019 - Test automatizado con Katalon Studio
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Software testing
Software testingSoftware testing
Software testing
 
Types of Software Testing | Edureka
Types of Software Testing | EdurekaTypes of Software Testing | Edureka
Types of Software Testing | Edureka
 
Software testing ppt
Software testing pptSoftware testing ppt
Software testing ppt
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
 
Selenium interview questions and answers
Selenium interview questions and answersSelenium interview questions and answers
Selenium interview questions and answers
 
TestNG
TestNGTestNG
TestNG
 
Public and private APIs: differences and challenges
Public and private APIs: differences and challengesPublic and private APIs: differences and challenges
Public and private APIs: differences and challenges
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
 
CTFL Module 04
CTFL Module 04CTFL Module 04
CTFL Module 04
 
Basic software-testing-concepts
Basic software-testing-conceptsBasic software-testing-concepts
Basic software-testing-concepts
 
Firewall best-practices-firewall-analyzer
Firewall best-practices-firewall-analyzerFirewall best-practices-firewall-analyzer
Firewall best-practices-firewall-analyzer
 
Software automation
Software automationSoftware automation
Software automation
 

Similar to Unit testing

Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
Pokpitch Patcharadamrongkul
 
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
 
Effective Unit Test Style Guide
Effective Unit Test Style GuideEffective Unit Test Style Guide
Effective Unit Test Style Guide
Jacky Lai
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
Aktuğ Urun
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
Priya Sharma
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
priya_trivedi
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
SumitKumar918321
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
Марія Русин
 
J Unit
J UnitJ Unit
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
Levon Apreyan
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Anand Kumar Rajana
 
Junit
JunitJunit
3 j unit
3 j unit3 j unit
3 j unit
kishoregali
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
Sanjib Dhar
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
Ahmed M. Gomaa
 
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
Amila Paranawithana
 
Junit
JunitJunit
Testing And Mxunit In ColdFusion
Testing And Mxunit In ColdFusionTesting And Mxunit In ColdFusion
Testing And Mxunit In ColdFusion
Denard Springle IV
 
Pragmatic unittestingwithj unit
Pragmatic unittestingwithj unitPragmatic unittestingwithj unit
Pragmatic unittestingwithj unit
liminescence
 
Junit 4.0
Junit 4.0Junit 4.0

Similar to Unit testing (20)

Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
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)
 
Effective Unit Test Style Guide
Effective Unit Test Style GuideEffective Unit Test Style Guide
Effective Unit Test Style Guide
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
J Unit
J UnitJ Unit
J Unit
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Junit
JunitJunit
Junit
 
3 j unit
3 j unit3 j unit
3 j unit
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
 
Junit
JunitJunit
Junit
 
Testing And Mxunit In ColdFusion
Testing And Mxunit In ColdFusionTesting And Mxunit In ColdFusion
Testing And Mxunit In ColdFusion
 
Pragmatic unittestingwithj unit
Pragmatic unittestingwithj unitPragmatic unittestingwithj unit
Pragmatic unittestingwithj unit
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 

Recently uploaded

Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
IsmaelVazquez38
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
Celine George
 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
TechSoup
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
سمير بسيوني
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
deepaannamalai16
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
RamseyBerglund
 
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
Nguyen Thanh Tu Collection
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
Celine George
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
nitinpv4ai
 
How to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in useHow to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in use
Celine George
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
EduSkills OECD
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
nitinpv4ai
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
Mohammad Al-Dhahabi
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
TechSoup
 

Recently uploaded (20)

Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
 
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
 
How to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in useHow to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in use
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
 

Unit testing

  • 1. Introduction to Unit Testing Author : Pooya Sagharchi Ha January 27, 2015
  • 2. Agenda ● What is Unit Testing ? ● Why should we Unit Test? ● Isolate units for testing ● UnitTest++ framework ● HippoMocks framework
  • 3. Unit Testing ● The smallest testable parts of an application, are called units ● Unit Test is a method in which a programmer tests individual units ● execute a program using artificial data
  • 4. Unit Testing input Main Class Testing Class output True or False
  • 5. Isolate Unit Testing ● it goal is to isolate each part of the program and show that the individual parts are correct Class 1 Class 2 Class 3 Class 4 Class 3 Testing Class input output True or
  • 6. Why Unit Testing? ● helps to find the problems early in the software development ● Can be used to validate that the code functionality still works after code change
  • 7. UnitTest++ ● lightweight ● Simplicity ● Portability ( platform Linux, Mac and windows ) ● Speed
  • 8. Getting started: 1. Create a header file on UnitTests folder 2. Include UnitTest++.h and TestReporterStdout.h 3. Write your test in TEST body 4. Compile it UnitTest++
  • 9. UnitTest++ For example : #include "UnitTest++.h" #include "TestReportsStdout.h" TEST(ChechSumTest) { int sum =0; for(int i=0; i < 5; ++i) sum += i; CHECK_EQUAL(10, sum); }
  • 10. UnitTest++ ● Test macro Define : simply put the following code in a .cpp file of your choice. Syntax : TEST(YourTestName) { }
  • 11. UnitTest++ ● Suit macro Define : A suite serves as a namespace for test names, so that the same test name can be used in two different contexts. Syntax : SU ITE(YourSuiteName) { TEST(YourTestName) { } TEST(YourOtherTestName) { } }
  • 12. UnitTest++ ● Check macro Define : It will fail if the boolean expression evaluates to false and conversely. Syntax : TEST(YourTestName) { Check(value); }
  • 13. UnitTest++ ● Check Equal macro Define : It will compare expected argument and actual argument. Syntax : TEST(YourTestName) { Check_EQUAL(expected, actual); }
  • 14. UnitTest++ ● Check Close macro Define : It is used for floating-point comparison. Syntax : TEST(YourTestName) { Check_CLOSE(expected, actual, tolerance); }
  • 15. UnitTest++ ● Array macro There is a set of check macros for array comparison as well. Syntax : TEST(YourTestName) { Check_Array_EQUAL(expected, actual, count); Check_Array_CLOSE(expected, actual, tolerance); Check_Array2D_EQUAL(expected, actual, row, columns, tolerance); }
  • 16. UnitTest++ ● Exception check macros Define : which asserts that its enclosed expression throws the specified type. Syntax : TEST(YourTestName) { Check_THROW(expression, ExpectedExpectionType); }
  • 17. HippoMocks Getting started: 1. Create a header file on UnitTests folder 2. Include UnitTest++.h and TestReporterStdout.h and hippmocks.h 3. Write your test in TEST body 4. Compile it
  • 18. HippoMocks ● MockRepository class Define : Create a MockRepository on the stack. Syntax : TEST(YourTestName) { MockRepository mocks; }
  • 19. HippoMocks ● Interface Mock Define : Used to create an instance of a class that implements a given interface. Syntax : TEST(YourTestName) { MockRepository mocks; ClassName *classnameMock = mocks.Mock<ClassName>(); }
  • 20. HippoMocks ● Expected Call Define : Indicates that the given method on the mock will be called. If the method is not called before the Repository destructs, a runtime error is generated. Syntax : TEST(YourTestName) { MockRepository mocks; ClassName *classnameMock = mocks.Mock<ClassName>(); mock.ExpectCall(obj, func); }
  • 21. HippoMocks ● Expected Call Define : AutoExpect can be called prior to calls to ExpectCall but the order of your program increase. Syntax : TEST(YourTestName) { MockRepository mocks; ClassName *classnameMock = mocks.Mock<ClassName>(); mocks.autoExpect = false or true; }
  • 22. HippoMocks ● On Call Define : Similar to ExpectCall, except that it's okay if the method doesn't get called. Syntax : TEST(YourTestName) { MockRepository mocks; ClassName *classnameMock = mocks.Mock<ClassName>(); mock.OnCall(obj, func); }
  • 23. HippoMocks ● Never Call Define : Similar to ExpectCall, except that it sets up expectation that the method will never be called. Syntax : TEST(YourTestName) { MockRepository mocks; ClassName *classnameMock = mocks.Mock<ClassName>(); mock.NeverCall(obj, func); }
  • 24. HippoMocks ● TCall class Define : This class is not instantiated directly. It's returned by MockRepository::ExpectCall The TCall class can be used to further refine the expectations of the method call.
  • 25. HippoMocks ● With Define : When used, indicates what parameters expected to be passed in. Syntax : TEST(YourTestName) { MockRepository mocks; ClassName *classnameMock = mocks.Mock<ClassName>(); mock.ExpectCall(obj, func).With(value); }
  • 26. HippoMocks ● Return Define : When used, indicates what value the method will return. Syntax : TEST(YourTestName) { MockRepository mocks; ClassName *classnameMock = mocks.Mock<ClassName>(); mock.ExpectCall(obj, func).Return(obj); }
  • 27. HippoMocks ● Throw Define : When used, indicates that the method must throw an exception. Syntax : TEST(YourTestName) { MockRepository mocks; ClassName *classnameMock = mocks.Mock<ClassName>(); mock.ExpectCall(obj, func).Throw(exception); }
  • 28. HippoMocks ● Do Define : When used, indicates what the method will do by passing in a function pointer. Syntax : TEST(YourTestName) { MockRepository mocks; ClassName *classnameMock = mocks.Mock<ClassName>(); mock.ExpectCall(obj, func).Do(function); }