SlideShare uma empresa Scribd logo
1 de 7
C++ Unit Test with GoogleTest
Raihan Masud,
Senior Software Engineer,
Micro Systems Engineering
What
C++ code test with unit testing framework
Why
GUI/Integration test can’t do code/unit testing
Code Coverage
How
By using unit testing framework in C++
Googletest - Google C++ testing framework
Chrome browser, OS and LLVM Compliers
CPP Unit
C++ port of Junit, has basic unit testing, only one level of assertion
Boost Test
Huge, preferable if you are already using boost libraries, no mocking
GoogleTest
Assertion rich, cross platform, mocking, customizable logging/reports
C++ Unit Test Frameworks
Basic Unit testing – assert, double, string test
Fatal/Non Fatal Assertion, Exception Assertion
Test/ Fixture Setup/Teardown
Test Result Reporting – xml
Auto Identifying tests by Google Test Framework – setup and running is easy
Use Shared Resources in test
Death Test -> Program Termination, segmentation fault, etc
Customize tests/test run with test flags - randomize test run, repeat same tests
Enable/Disable Test
Type assertion - run same test for multiple types
Use Reflection -> better test description, Test Name/Info
Extending, customizing test runs on Event Listeners
Parameterized/data-driven test - run same test for multiple test inputs/data
Distribute test over multiple machines
GoogleTest Features
Writing Tests using GoogleTest
Download and Compile GoogleTest Framework Library
https://code.google.com/p/googletest/
Create Tests using Visual Studio
Add references to googletest and project under test, and
additional include directories
DEMO USING VISUAL STUDIO
Template Project
TEST
ASSERT_EQ, EXPECT_EQ, EXPECT_STR_EQ, EXPECT_TRUE
TEST_F :public::testing::test
Add AvOpt reference
Exception assertion
static void SetUpTestCase(){}, static void TearDownTestCase(){}
virtual void SetUp(){} TearDown(){}
GUI for GoogleTest
Filters/Customize test run – shuffle tests
Enable/Disable DISABLED_TestName
Test result
VALUE PARAMETERIZED TESTS
• class FooTest : public ::testing::TestWithParam<const char*> {
// You can implement all the usual fixture class members here.
// To access the test parameter, call GetParam() from class
// TestWithParam<T>.
};
// Or, when you want to add parameters to a pre-existing fixture class:
class BaseTest : public ::testing::Test {
...
};
class BarTest : public BaseTest,
public ::testing::WithParamInterface<const char*> {
...
};
VALUE PARAMETERIZED TESTS
• Then, use the TEST_P macro to define as many test patterns using this fixture as
you want. The _P suffix is for "parameterized" or "pattern", whichever you prefer
to think.
• TEST_P(FooTest, DoesBlah) {
// Inside a test, access the test parameter with the GetParam() method
// of the TestWithParam<T> class:
EXPECT_TRUE(foo.Blah(GetParam()));
...
}
TEST_P(FooTest, HasBlahBlah) {
...
}

Mais conteúdo relacionado

Mais procurados

Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Harry Potter
 
Unit Testing RPG with JUnit
Unit Testing RPG with JUnitUnit Testing RPG with JUnit
Unit Testing RPG with JUnit
Greg.Helton
 

Mais procurados (20)

Introduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightIntroduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylight
 
Automated testing in Python and beyond
Automated testing in Python and beyondAutomated testing in Python and beyond
Automated testing in Python and beyond
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With Pytest
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
 
Doing the Impossible
Doing the ImpossibleDoing the Impossible
Doing the Impossible
 
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
 
Test Driven In Groovy
Test Driven In GroovyTest Driven In Groovy
Test Driven In Groovy
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
 
Tdd With Groovy
Tdd With GroovyTdd With Groovy
Tdd With Groovy
 
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
 
Google test training
Google test trainingGoogle test training
Google test training
 
Cursus phpunit
Cursus phpunitCursus phpunit
Cursus phpunit
 
Python and test
Python and testPython and test
Python and test
 
JUnit 5 - The Next Generation
JUnit 5 - The Next GenerationJUnit 5 - The Next Generation
JUnit 5 - The Next Generation
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Unit Testing RPG with JUnit
Unit Testing RPG with JUnitUnit Testing RPG with JUnit
Unit Testing RPG with JUnit
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
PHPUnit - Unit testing
PHPUnit - Unit testingPHPUnit - Unit testing
PHPUnit - Unit testing
 

Semelhante a Presentation_C++UnitTest

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
Tricode (part of Dept)
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
Priya Sharma
 

Semelhante a Presentation_C++UnitTest (20)

V-TEST
V-TESTV-TEST
V-TEST
 
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
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
UPC Testing talk 2
UPC Testing talk 2UPC Testing talk 2
UPC Testing talk 2
 
Test ng for testers
Test ng for testersTest ng for testers
Test ng for testers
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
 
Joomla! Testing - J!DD Germany 2016
Joomla! Testing - J!DD Germany 2016Joomla! Testing - J!DD Germany 2016
Joomla! Testing - J!DD Germany 2016
 
Testes? Mas isso não aumenta o tempo de projecto? Não quero...
Testes? Mas isso não aumenta o tempo de projecto? Não quero...Testes? Mas isso não aumenta o tempo de projecto? Não quero...
Testes? Mas isso não aumenta o tempo de projecto? Não quero...
 
JahiaOne 2015 - How to automatically unit and integration test your Digital F...
JahiaOne 2015 - How to automatically unit and integration test your Digital F...JahiaOne 2015 - How to automatically unit and integration test your Digital F...
JahiaOne 2015 - How to automatically unit and integration test your Digital F...
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
 
L08 Unit Testing
L08 Unit TestingL08 Unit Testing
L08 Unit Testing
 
Mxunit
MxunitMxunit
Mxunit
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
JUnit Goodness
JUnit GoodnessJUnit Goodness
JUnit Goodness
 
Simple test drupal7_presentation_la_drupal_jul21-2010
Simple test drupal7_presentation_la_drupal_jul21-2010Simple test drupal7_presentation_la_drupal_jul21-2010
Simple test drupal7_presentation_la_drupal_jul21-2010
 
Junit_.pptx
Junit_.pptxJunit_.pptx
Junit_.pptx
 

Presentation_C++UnitTest

  • 1. C++ Unit Test with GoogleTest Raihan Masud, Senior Software Engineer, Micro Systems Engineering What C++ code test with unit testing framework Why GUI/Integration test can’t do code/unit testing Code Coverage How By using unit testing framework in C++ Googletest - Google C++ testing framework Chrome browser, OS and LLVM Compliers
  • 2. CPP Unit C++ port of Junit, has basic unit testing, only one level of assertion Boost Test Huge, preferable if you are already using boost libraries, no mocking GoogleTest Assertion rich, cross platform, mocking, customizable logging/reports C++ Unit Test Frameworks
  • 3. Basic Unit testing – assert, double, string test Fatal/Non Fatal Assertion, Exception Assertion Test/ Fixture Setup/Teardown Test Result Reporting – xml Auto Identifying tests by Google Test Framework – setup and running is easy Use Shared Resources in test Death Test -> Program Termination, segmentation fault, etc Customize tests/test run with test flags - randomize test run, repeat same tests Enable/Disable Test Type assertion - run same test for multiple types Use Reflection -> better test description, Test Name/Info Extending, customizing test runs on Event Listeners Parameterized/data-driven test - run same test for multiple test inputs/data Distribute test over multiple machines GoogleTest Features
  • 4. Writing Tests using GoogleTest Download and Compile GoogleTest Framework Library https://code.google.com/p/googletest/ Create Tests using Visual Studio Add references to googletest and project under test, and additional include directories
  • 5. DEMO USING VISUAL STUDIO Template Project TEST ASSERT_EQ, EXPECT_EQ, EXPECT_STR_EQ, EXPECT_TRUE TEST_F :public::testing::test Add AvOpt reference Exception assertion static void SetUpTestCase(){}, static void TearDownTestCase(){} virtual void SetUp(){} TearDown(){} GUI for GoogleTest Filters/Customize test run – shuffle tests Enable/Disable DISABLED_TestName Test result
  • 6. VALUE PARAMETERIZED TESTS • class FooTest : public ::testing::TestWithParam<const char*> { // You can implement all the usual fixture class members here. // To access the test parameter, call GetParam() from class // TestWithParam<T>. }; // Or, when you want to add parameters to a pre-existing fixture class: class BaseTest : public ::testing::Test { ... }; class BarTest : public BaseTest, public ::testing::WithParamInterface<const char*> { ... };
  • 7. VALUE PARAMETERIZED TESTS • Then, use the TEST_P macro to define as many test patterns using this fixture as you want. The _P suffix is for "parameterized" or "pattern", whichever you prefer to think. • TEST_P(FooTest, DoesBlah) { // Inside a test, access the test parameter with the GetParam() method // of the TestWithParam<T> class: EXPECT_TRUE(foo.Blah(GetParam())); ... } TEST_P(FooTest, HasBlahBlah) { ... }