Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

C++ Unit testing - the good, the bad & the ugly

624 views

Published on

C++ is a powerful language when used by a seasoned developer. It is being used everywhere from operating systems and compilers to cross platform mobile apps development.
But this power comes with a cost - C++ programming is not simple nor easy – with memory leaks, stack overflows and strange exceptions, a software developer needs all of the power he can use.

By harnessing the power of unit tests a software developer can find defects quickly, avoid regression and speed up development time.

In this talk I’ll show how to use the right unit testing tools and practices available that will help you get your C++ code under control and even test legacy code which no one ever want to touch.

Published in: Software
  • Be the first to comment

C++ Unit testing - the good, the bad & the ugly

  1. 1. C++ Unit testing The good, the bad and the ugly Dror Helper | @dhelper | http://helpercode.com
  2. 2. Consultant @CodeValue Developing software since 2002 Clean Coder & Test Driven Developer Pluralsight author https://www.pluralsight.com/authors/dror-helper B: http://helpercode.com T: @dhelper About.ME
  3. 3. An ode to C++ Multi-paradigm Performance & Speed Truly multi-platform Plays well with others Extendible and powerful Low level programming My compiler compiled your compiler’s compiler
  4. 4. “C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.” Bjarne Stroustrup
  5. 5. Avoid stupid bugs Early feedback Avoid Regression Write Sustainable, manageable code Route to modular OO design In code documentation Have you tried unit testing your code?
  6. 6. Definition: Unit Test (by me) A method (code) that tests specific functionality, Has clear pass/fail criteria and runs in isolation
  7. 7. This is a unit test (GTest) TEST(MovieTests, WhatWouldTucoDo) { Charcter tuco; tuco.SetHaveToShoot(true); ASSERT_FALSE(tuco.GetTalk()); }
  8. 8. What you’ll need Developer’s Machine • Unit testing framework • Mocking framework • Code coverage tool • Other (IDE, Refactoring tools) Continuous Integration • CI Server • Build Agent(s)
  9. 9. Unit testing framework capabilities Tests & Fixtures Assertio ns Test Runners
  10. 10. C++ Unit Testing Frameworks The Good • Many choices • Many Styles The Bad • Documentation may vary • Not all maintained The Ugly • Deployment • No Reflection…
  11. 11. * at least until C++/2020
  12. 12. C++ Unit Testing Frameworks GTest MSTest Catch CppUnit Boost.Test DocTest lest xUnit++ Mettle A Short, incomplete list…
  13. 13. Multiple asserts • Lose information • Complex tests One assert per test • Writing many tests for the same scenario • See Above The multiple assertion dilemma
  14. 14. Innovation in C++ Enabling readable and maintainable tests • Failures without exceptions • Better failure messages • Single, powerful assert • Rethinking test fixture - Sections
  15. 15. 16 What is a “Mock”? “mock objects are simulated objects that mimic the behavior of real objects in controlled ways” [From Wikipedia] I prefer to call them “Fakes”
  16. 16. Mocking frameworks responsibilities 1. Create Fake/Mock objects 2. Set behavior on fakes 3. Verify that methods were called
  17. 17. C++ Mocking Frameworks The Good • C++ has mocking frameworks The Bad • I meant a framework (at least until recently) • Beware of dead projects The Ugly • Most one step better than hand rolled mocks • Most are not AAA compliant
  18. 18. C++ Mocking Frameworks GMock Trompeloeil HippoMocks FakeIt *Based on my own experience
  19. 19. Don’t use obsolete tools! 1. Check when was the last commit! 2. How extensive is the documentation C++ is changing and so should your testing framework
  20. 20. C++ Development in Windows • Multi-platform – develop on windows, compile to run everywhere • Remote debug/run on Linux (VS2017) • Use test runner (or develop your own) • Code coverage support
  21. 21. How to choose your unit testing tools Multi-platform vs. mono-platform Ease of use What happens when test fails Integration points Runner Support Ease of deployment Personal taste
  22. 22. IN THIS WORLD THERE’S TWO TYPES OF PEOPLE MY FRIEND. THOSE WITH UNIT TESTS AND THOSE WHO BLAME. WRITE UNIT TESTS!
  23. 23. Thank you Dror Helper | @dhelper | http://helpercode.com

×