SlideShare une entreprise Scribd logo
1  sur  11
Dev Unit Testing:
CppUnit Workshop


Mudabbir Warsi (SHAMU04)
December 29,2010
Agenda

• Introduction to Unit Testing
• Introduction to CppUnit
• Important aspects of CppUnit
  • Writing Test Cases
  • Launching Tests
  • Collecting and Reporting Test Results
• CppUnit Workshop
  • Demo
  • How to write unit tests for existing code?
  • Integration with the Build process
• Introduction to Stubs and Mock objects
• Q&A
   2   December 29, 2010 Copyright ©2010 CA. All rights reserved.
Introduction

• Traditional testing methods
    • Stepping through a debugger
    • Littering code with stream out put calls

• What is a “Unit Test”?
    “A Unit Test is a piece of code written by a developer that exercises a very small, specific area of
    functionality of the code being tested”.
•    Why Unit Testing?
    • Speed up the development – Write tests once, use tests to find errors many times
    • Less time spent on debugging – Incorrect changes discovered immediately
    • Tests as documentation. Provides a working specification of the functional code
    • Gain Confidence in your code
•    Excuses for NOT (Unit) testing?
    • It takes too much time to write the tests
    • I am being paid to write code, not to write tests
    • I feel guilty about putting QA staff out of work
    3    December 29, 2010 Copyright ©2010 CA. All rights reserved.
Introduction to CppUnit

• Trivia
    •   Kent Beck published the first unit test framework for SmallTalk in 1999
    •   CppUnit is a member of xUnit family (SUnit, JUnit, NUnit, pyUnit, vbUnit etc)
    •   CppUnit started as a port of JUnit to C++ by Michael Feathers
    •   Motto of CppUnit: “Testing: Early, Often and Automated”


• Important Classes of xUnit Framework
    •   TestCase                  : Represents a single test case
    •   TestSuite                 : A collection of Test Cases
    •   TestRunner                : A Launcher of Test Suites
    •   TestResult                : Collects errors/failures that occur during a test

    •   TestListener              : Listener of events that occur during a test




   4    December 29, 2010 Copyright ©2010 CA. All rights reserved.
Writing Test Cases


•   Class “Test” represents one individual test. All test cases
    should be a sub-class of Test. Its an Abstract base class.
                                                                      Test
•   Class “TestFixture” provides a common                             run(TestResult*):void
    environment OR common resources for a set of                                                  TestFixture
    test cases.
                                                                             TestLeaf         Setup()
                                                                                              TearDown()
•   Class “TestLeaf” is a base class for single test case. A
    test that is not composed of other test cases.                           TestCase

                                                                      run(TestResult*):void
                                                                      runTest() : void

•   Class “TestCase” is the parent of all Unit Tests. All
    unit tests should inherit from this class and should                  MyTestCase

    override the runTest() method                                     runTest() : void




     5   December 29, 2010 Copyright ©2010 CA. All rights reserved.
Writing Test Cases….Contd


•   A Test Suite is a container that collects several tests and runs them as a single unit.


•   Class “TestComposite” represents a                                                                  Test
                                                                                       1..*
    base class for collection of test cases.
                                                                                                 run(TestResult*):void
    Subclass this to implement a custom
    Test Suite.
                                                                                                      TestComposite

•   Class “TestSuite” represents a                                                               run(TestResult*) : void

    collection of Test Cases that are run as
    a single unit.
                                                                                TestSuite                                       MyTestSuite

                                                                           addTest(Test*) : void                           addTest(Test*) : void
                                                                       1   doGetChildTestAt(int) : Test*                   doGetChildTestAt(int) : Test*

                                                                           vector<Test*> suite                             deque<Test*> suite




     6    December 29, 2010 Copyright ©2010 CA. All rights reserved.
Launching Tests with TestRunner


• Class “TestRunner” executes the test cases and provides statistical information
  regarding the outcome of the test execution.
• CppUnit provides different test runners.
       •   TestRunner                 : Generic test runner
       •   TextTestRunner             : A text mode test runner
       •   MFCTestRunner : GUI based test runner
       •   QTTestRunner               : GUI based test runner for QT framework



• Unlike other elements of the framework, CppUnit does not provide any interface
  for creating custom Test Runners.

• Important method of TestRunner Class
    TestRunner::run(TestResult&); // Runs the test using the specified controller

   7       December 29, 2010 Copyright ©2010 CA. All rights reserved.
Collecting and Reporting Test Results


• Collecting Results with TestResult
   A TestResult collects the results of executing a collection of test cases. It counts the tests run and
   collects the failures and errors so that the framework can report them.


• Observing Results with TestListeners
   TestListeners provides the ability for the Framework users to be notified about the different events
   that occur while execution of a test case.
   Eg: Start of a test case, end of a test case, start of test suite, End of a test suite

• Printing Results with Outputters
   Outputters print the summary of the Test Results in various formats (XML Format, Text Format etc).
   It can be extended to print result summary in any custom format.
   Eg: TextOutputter, XmlOutputter, CompilerOutputter




    8    December 29, 2010 Copyright ©2010 CA. All rights reserved.
CppUnit Workshop


• DEMO
• How to write unit tests for existing code?
• Integration with the Build process




    9   December 29, 2010 Copyright ©2010 CA. All rights reserved.
Introduction to Stubs and MOCK objects

•   Stub is a piece of code that replaces the real code, in order to isolate the calling code from the real
    implementation.
     •    Advantages of STUBS
           • Replace complex behavior with a simple one
           • Allows for coarse-grained Isolation
     •    Disadvantages of STUBS
           • Complex to write
           • Difficult to debug and maintain

•   A Mock object is an object created to stand in for an object that the code being tested will be
    collaborating with.
     •    Advantages of MOCK objects
           •     Easier to write and maintain.
           •     Allows for fine-grained Isolation

     •    Disadvantages of MOCK objects
           • There may be many objects to Mock
           • Forces to re write some of the code under test.
     10   December 29, 2010 Copyright ©2010 CA. All rights reserved.
Thank You !!

Contenu connexe

Tendances (20)

JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
 
Python datetime
Python datetimePython datetime
Python datetime
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Unit testing
Unit testingUnit testing
Unit testing
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
 
JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
 
05 junit
05 junit05 junit
05 junit
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing Framework
 
[若渴計畫] Challenges and Solutions of Window Remote Shellcode
[若渴計畫] Challenges and Solutions of Window Remote Shellcode[若渴計畫] Challenges and Solutions of Window Remote Shellcode
[若渴計畫] Challenges and Solutions of Window Remote Shellcode
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37
 
TestNG vs Junit
TestNG vs JunitTestNG vs Junit
TestNG vs Junit
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
 
Nunit
NunitNunit
Nunit
 
Programming in Python
Programming in Python Programming in Python
Programming in Python
 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Networking in python by Rj
 
Testing Metrics
Testing MetricsTesting Metrics
Testing Metrics
 
Threads in python
Threads in pythonThreads in python
Threads in python
 
Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)
 
Eclipse - Installation and quick start guide
Eclipse - Installation and quick start guideEclipse - Installation and quick start guide
Eclipse - Installation and quick start guide
 
Control statement-Selective
Control statement-SelectiveControl statement-Selective
Control statement-Selective
 

En vedette

Nunit C# source code defects report by Parasoft dotTEST
Nunit  C# source code  defects report by Parasoft dotTEST Nunit  C# source code  defects report by Parasoft dotTEST
Nunit C# source code defects report by Parasoft dotTEST Engineering Software Lab
 
Parasoft Concerto A complete ALM platform that ensures quality software can b...
Parasoft Concerto A complete ALM platform that ensures quality software can b...Parasoft Concerto A complete ALM platform that ensures quality software can b...
Parasoft Concerto A complete ALM platform that ensures quality software can b...Engineering Software Lab
 
Parasoft .TEST, Write better C# Code Using Data Flow Analysis
Parasoft .TEST, Write better C# Code Using  Data Flow Analysis Parasoft .TEST, Write better C# Code Using  Data Flow Analysis
Parasoft .TEST, Write better C# Code Using Data Flow Analysis Engineering Software Lab
 
Perforce עשרת היתרונות המובילים של מערכת ניהול התצורה
Perforce עשרת היתרונות המובילים של מערכת ניהול התצורהPerforce עשרת היתרונות המובילים של מערכת ניהול התצורה
Perforce עשרת היתרונות המובילים של מערכת ניהול התצורהEngineering Software Lab
 
Amran Tuberi - the damage of cycling to the desert ecosystem
Amran Tuberi - the damage of cycling to the desert ecosystemAmran Tuberi - the damage of cycling to the desert ecosystem
Amran Tuberi - the damage of cycling to the desert ecosystemEngineering Software Lab
 
WPF/ XamDataGrid Performance, Infragistics Seminar, Israel , November 2011
WPF/ XamDataGrid Performance, Infragistics Seminar, Israel , November 2011WPF/ XamDataGrid Performance, Infragistics Seminar, Israel , November 2011
WPF/ XamDataGrid Performance, Infragistics Seminar, Israel , November 2011Engineering Software Lab
 
Code coverage in theory and in practice form the do178 b perspective
Code coverage in theory and in practice form the do178 b perspectiveCode coverage in theory and in practice form the do178 b perspective
Code coverage in theory and in practice form the do178 b perspectiveEngineering Software Lab
 
Unit testing on embedded target with C++Test
Unit testing on embedded  target with C++TestUnit testing on embedded  target with C++Test
Unit testing on embedded target with C++TestEngineering Software Lab
 
Code Coverage in Theory and in practice form the DO178B perspective
Code Coverage in Theory and in practice form the DO178B perspective   Code Coverage in Theory and in practice form the DO178B perspective
Code Coverage in Theory and in practice form the DO178B perspective Engineering Software Lab
 
Digital Business: Communication and Collaboration Introduction
Digital Business: Communication and Collaboration IntroductionDigital Business: Communication and Collaboration Introduction
Digital Business: Communication and Collaboration IntroductionDigital Business
 

En vedette (20)

Parasoft fda software compliance part2
Parasoft fda software compliance   part2Parasoft fda software compliance   part2
Parasoft fda software compliance part2
 
Nunit C# source code defects report by Parasoft dotTEST
Nunit  C# source code  defects report by Parasoft dotTEST Nunit  C# source code  defects report by Parasoft dotTEST
Nunit C# source code defects report by Parasoft dotTEST
 
Introduction to Parasoft C++TEST
Introduction to Parasoft C++TEST Introduction to Parasoft C++TEST
Introduction to Parasoft C++TEST
 
Parasoft fda software compliance part1
Parasoft fda software compliance   part1Parasoft fda software compliance   part1
Parasoft fda software compliance part1
 
Parasoft Concerto A complete ALM platform that ensures quality software can b...
Parasoft Concerto A complete ALM platform that ensures quality software can b...Parasoft Concerto A complete ALM platform that ensures quality software can b...
Parasoft Concerto A complete ALM platform that ensures quality software can b...
 
Parasoft .TEST, Write better C# Code Using Data Flow Analysis
Parasoft .TEST, Write better C# Code Using  Data Flow Analysis Parasoft .TEST, Write better C# Code Using  Data Flow Analysis
Parasoft .TEST, Write better C# Code Using Data Flow Analysis
 
Perforce עשרת היתרונות המובילים של מערכת ניהול התצורה
Perforce עשרת היתרונות המובילים של מערכת ניהול התצורהPerforce עשרת היתרונות המובילים של מערכת ניהול התצורה
Perforce עשרת היתרונות המובילים של מערכת ניהול התצורה
 
A Scalable Software Build Accelerator
A Scalable Software Build AcceleratorA Scalable Software Build Accelerator
A Scalable Software Build Accelerator
 
Amran Tuberi - the damage of cycling to the desert ecosystem
Amran Tuberi - the damage of cycling to the desert ecosystemAmran Tuberi - the damage of cycling to the desert ecosystem
Amran Tuberi - the damage of cycling to the desert ecosystem
 
המסדרת הפכה למגוהצת
המסדרת הפכה למגוהצתהמסדרת הפכה למגוהצת
המסדרת הפכה למגוהצת
 
WPF/ XamDataGrid Performance, Infragistics Seminar, Israel , November 2011
WPF/ XamDataGrid Performance, Infragistics Seminar, Israel , November 2011WPF/ XamDataGrid Performance, Infragistics Seminar, Israel , November 2011
WPF/ XamDataGrid Performance, Infragistics Seminar, Israel , November 2011
 
Palamida Open Source Compliance Solution
Palamida Open Source Compliance Solution Palamida Open Source Compliance Solution
Palamida Open Source Compliance Solution
 
Code coverage in theory and in practice form the do178 b perspective
Code coverage in theory and in practice form the do178 b perspectiveCode coverage in theory and in practice form the do178 b perspective
Code coverage in theory and in practice form the do178 b perspective
 
FDA software compliance 2016
FDA software compliance 2016FDA software compliance 2016
FDA software compliance 2016
 
Unit testing on embedded target with C++Test
Unit testing on embedded  target with C++TestUnit testing on embedded  target with C++Test
Unit testing on embedded target with C++Test
 
Embedded System Test Automation
Embedded System Test AutomationEmbedded System Test Automation
Embedded System Test Automation
 
Code Coverage in Theory and in practice form the DO178B perspective
Code Coverage in Theory and in practice form the DO178B perspective   Code Coverage in Theory and in practice form the DO178B perspective
Code Coverage in Theory and in practice form the DO178B perspective
 
Sykemittari
SykemittariSykemittari
Sykemittari
 
Digital Business: Communication and Collaboration Introduction
Digital Business: Communication and Collaboration IntroductionDigital Business: Communication and Collaboration Introduction
Digital Business: Communication and Collaboration Introduction
 
Что растёт на подоконнике
Что растёт на подоконникеЧто растёт на подоконнике
Что растёт на подоконнике
 

Similaire à Cpp unit

Similaire à Cpp unit (20)

Qt test framework
Qt test frameworkQt test framework
Qt test framework
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Junit
JunitJunit
Junit
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Test ng
Test ngTest ng
Test ng
 
Junit
JunitJunit
Junit
 
Junit
JunitJunit
Junit
 
Plone Testing Tools And Techniques
Plone Testing Tools And TechniquesPlone Testing Tools And Techniques
Plone Testing Tools And Techniques
 
J unit스터디슬라이드
J unit스터디슬라이드J unit스터디슬라이드
J unit스터디슬라이드
 
Test ng tutorial
Test ng tutorialTest ng tutorial
Test ng tutorial
 
Test automation principles, terminologies and implementations
Test automation principles, terminologies and implementationsTest automation principles, terminologies and implementations
Test automation principles, terminologies and implementations
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Unit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran JanardhanaUnit Testing with JUnit4 by Ravikiran Janardhana
Unit Testing with JUnit4 by Ravikiran Janardhana
 
Gallio Crafting A Toolchain
Gallio Crafting A ToolchainGallio Crafting A Toolchain
Gallio Crafting A Toolchain
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
 
L08 Unit Testing
L08 Unit TestingL08 Unit Testing
L08 Unit Testing
 

Dernier

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Dernier (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

Cpp unit

  • 1. Dev Unit Testing: CppUnit Workshop Mudabbir Warsi (SHAMU04) December 29,2010
  • 2. Agenda • Introduction to Unit Testing • Introduction to CppUnit • Important aspects of CppUnit • Writing Test Cases • Launching Tests • Collecting and Reporting Test Results • CppUnit Workshop • Demo • How to write unit tests for existing code? • Integration with the Build process • Introduction to Stubs and Mock objects • Q&A 2 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  • 3. Introduction • Traditional testing methods • Stepping through a debugger • Littering code with stream out put calls • What is a “Unit Test”? “A Unit Test is a piece of code written by a developer that exercises a very small, specific area of functionality of the code being tested”. • Why Unit Testing? • Speed up the development – Write tests once, use tests to find errors many times • Less time spent on debugging – Incorrect changes discovered immediately • Tests as documentation. Provides a working specification of the functional code • Gain Confidence in your code • Excuses for NOT (Unit) testing? • It takes too much time to write the tests • I am being paid to write code, not to write tests • I feel guilty about putting QA staff out of work 3 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  • 4. Introduction to CppUnit • Trivia • Kent Beck published the first unit test framework for SmallTalk in 1999 • CppUnit is a member of xUnit family (SUnit, JUnit, NUnit, pyUnit, vbUnit etc) • CppUnit started as a port of JUnit to C++ by Michael Feathers • Motto of CppUnit: “Testing: Early, Often and Automated” • Important Classes of xUnit Framework • TestCase : Represents a single test case • TestSuite : A collection of Test Cases • TestRunner : A Launcher of Test Suites • TestResult : Collects errors/failures that occur during a test • TestListener : Listener of events that occur during a test 4 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  • 5. Writing Test Cases • Class “Test” represents one individual test. All test cases should be a sub-class of Test. Its an Abstract base class. Test • Class “TestFixture” provides a common run(TestResult*):void environment OR common resources for a set of TestFixture test cases. TestLeaf Setup() TearDown() • Class “TestLeaf” is a base class for single test case. A test that is not composed of other test cases. TestCase run(TestResult*):void runTest() : void • Class “TestCase” is the parent of all Unit Tests. All unit tests should inherit from this class and should MyTestCase override the runTest() method runTest() : void 5 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  • 6. Writing Test Cases….Contd • A Test Suite is a container that collects several tests and runs them as a single unit. • Class “TestComposite” represents a Test 1..* base class for collection of test cases. run(TestResult*):void Subclass this to implement a custom Test Suite. TestComposite • Class “TestSuite” represents a run(TestResult*) : void collection of Test Cases that are run as a single unit. TestSuite MyTestSuite addTest(Test*) : void addTest(Test*) : void 1 doGetChildTestAt(int) : Test* doGetChildTestAt(int) : Test* vector<Test*> suite deque<Test*> suite 6 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  • 7. Launching Tests with TestRunner • Class “TestRunner” executes the test cases and provides statistical information regarding the outcome of the test execution. • CppUnit provides different test runners. • TestRunner : Generic test runner • TextTestRunner : A text mode test runner • MFCTestRunner : GUI based test runner • QTTestRunner : GUI based test runner for QT framework • Unlike other elements of the framework, CppUnit does not provide any interface for creating custom Test Runners. • Important method of TestRunner Class TestRunner::run(TestResult&); // Runs the test using the specified controller 7 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  • 8. Collecting and Reporting Test Results • Collecting Results with TestResult A TestResult collects the results of executing a collection of test cases. It counts the tests run and collects the failures and errors so that the framework can report them. • Observing Results with TestListeners TestListeners provides the ability for the Framework users to be notified about the different events that occur while execution of a test case. Eg: Start of a test case, end of a test case, start of test suite, End of a test suite • Printing Results with Outputters Outputters print the summary of the Test Results in various formats (XML Format, Text Format etc). It can be extended to print result summary in any custom format. Eg: TextOutputter, XmlOutputter, CompilerOutputter 8 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  • 9. CppUnit Workshop • DEMO • How to write unit tests for existing code? • Integration with the Build process 9 December 29, 2010 Copyright ©2010 CA. All rights reserved.
  • 10. Introduction to Stubs and MOCK objects • Stub is a piece of code that replaces the real code, in order to isolate the calling code from the real implementation. • Advantages of STUBS • Replace complex behavior with a simple one • Allows for coarse-grained Isolation • Disadvantages of STUBS • Complex to write • Difficult to debug and maintain • A Mock object is an object created to stand in for an object that the code being tested will be collaborating with. • Advantages of MOCK objects • Easier to write and maintain. • Allows for fine-grained Isolation • Disadvantages of MOCK objects • There may be many objects to Mock • Forces to re write some of the code under test. 10 December 29, 2010 Copyright ©2010 CA. All rights reserved.