An Introduction to Unit Testing: Reducing C/C++ bugs with Boost

5 downloads 123 Views 709KB Size Report
An Introduction to Unit Testing: Reducing C/C++ ... 2. Overview. ○ Software Testing. ○ Unit Testing. ○ Boost / Test ... Boost/Test – part of the Boost C++ library.
An Introduction to Unit Testing: Reducing C/C++ bugs with Boost

Andrew McDonnell Software Engineer & Tech Problem Solver http://oldcomputerjunk.net [email protected] Twitter: @andymc73

1

Overview ●

Software Testing



Unit Testing



Boost / Test

2

Why Test? ●

All software has bugs



To avoid embarrassment



To save $$$



Save your sanity!

3

Attribution: http://www.flickr.com/photos/evelynishere/2841132210/ (CC-BY 2.0)

4

In the beginning... (or not) ●

Compile and Run?



Throw it to the users –

(the Apple Maps approach [1])

5

[1] http://www.tomshardware.com/picturestory/607-ios-maps-fail.html Accessed: 22-Jan-2013

How should we Test? ●

Integration Testing



Acceptance Testing / Feature walk throughs



Managed alpha/beta testing



Unit Testing

6

Unit Testing Saves Hacking Time ●

Exercises low level software elements –

Classes, functions or API elements



maintained with, and helps structure, code



Is repeatable

7

What about Open Coding...? ●

Project hackers



Your API users



“ If you don’t like unit testing your software, most likely your users won’t like to test it either. ”

--anonymous 8

Unit Test all the Things! (or not)

Attribution: http://xkcd.com/809 (CC-BY-NC 2.5) 9

Optimal Conditions ●

We want best bang for buck



Wise selection of unit Test Coverage –

Comprehensive = high maintenance



Focus on Risk areas ● ●

Complex algorithms Core modules

10

Suitable Code ●

Algorithmic code



Framework code



Not as suitable / easy for UI elements



Wont fix requirement defects

11

Lessons Learned ●

Boost can save you a world of pain ...

Attribution:http://free-cartoons.sangrea.net/philosophy-cartoons.html (CCA-ND 2.5)

12

Boost/Test ●

Boost/Test – part of the Boost C++ library



Provides framework for expediting Unit Test



Open source license (Boost License) http://www.boost.org/doc/libs/1_42_0/libs/test http://www.boost.org

13

Boost/Test with C ●

Boost/Test is C++ –

But you can exercise C code



Typically just wrap the C #include with ●

#ifdef __cplusplus (etc)



There may be caveats, see online docs

14

Installation ●

Installing on Linux (Debian 6, YMMV)

apt­get install libboost1.42­doc libboost­test1.42­dev



Cross platform – also Windows, etc.

15

Hello World is Easy... ●

Example Code:

#include “my_library.h” #define BOOST_TEST_MODULE HELLO_TEST #include  BOOST_AUTO_TEST_SUITE( hello_suite ) BOOST_AUTO_TEST_CASE( hello_case1 ) {   MyClass testClass(1,2);   BOOST_REQUIRE( sizeof(int) == 4 );  // specific platform   int rc = testClass.doSomething(4,5);   BOOST_CHECK_MSG( rc > 6, “Bad Value Return” ); } BOOST_AUTO_TEST_SUITE_END()

16

So is Build... ●

Boost can be fully inline, simplfying linking: #include  g++ unittest.cxx ­o unittest



This does compile much slower



So you can also link with a library if needed ●



Beware of #ifdefs

See also http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/faq.htm

17

Header only is convient...

Attribution: http://xkcd.com/303/ (CC-BY-NC 2.5) 18

Key Features of Boost ●

Suites



Fixtures



Result checking



Output Control



Optional main()

19

Test Suite ●

A means for grouping of related tests –

Provides a means of organisation



And of identification

BOOST_AUTO_TEST_SUITE( hello_suite )

20

Fixtures ●

Used to prepare generally for tests –



e.g. (re)initialise a library

Execute before Start of all testing –

And/or Before each case

21

Per Test Fixtures - example struct PerTestFixture {   PerTestFixture() { x = reset_stuff(); } // set up   ~PerTestFixture() { destroy(x); } // tear down   some_handle_t x; } BOOST_FIXTURE_TEST_CASE( case_blah, PerTestFixture) {   BOOST_CHECK(do_something()); } BOOST_FIXTURE_TEST_CASE( case_also PerTestFixture) {   BOOST_CHECK(do_something_else()); }

22

The whole point - result checking ●

Running a test involves: ● ● ●



calling code testing the output repeat the above

Use Boost macros to express expected output

23

Boost has many result macros ●



Examples: –

BOOST_CHECK(thing_is_true)



BOOST_CHECK_GT(some_number)



BOOST_EQUAL(a,b)

the doco is a bit confusing ●

Look under “Testing Tools / Reference”

24

Control of checking ●

Three Levels –

CHECK

determines PASS or FAIL



WARN

logs a warning



REQUIRE

abort all tests on fail

25

Choices: lots of output,or “Pass” ● ●



Control of message detail by environment See just pass/fail, warnings, tests conducted, or detailed logs It is possible to automate and collect logs from a comprehensive suite –

And run nightly, etc.

26

Where is my main() ? ●

Is hidden by test executive macros –

But you can use your own if needed

27

Complementary to Unit Testing ●

Unit Testing wont find all bugs



Source Code Inspection



Automated linting tools, static analysis

28

In Summary: ●

All software has bugs;



You dont want to be up at 3am, so:



(If you code in C++ (or C),)



Use Boost/Test to Kill bugs first



(Or any other Unit Test Framework!)



(But Boost is Easy) 29

Credits & License ●





Content by Andrew McDonnell @andymc73 [email protected] http://oldcomputerjunk.net http://au.linkedin.com/in/amcdonnell License: Creative Commons CC-BY-SA Images / cartoons attributed on slide

OpenOffice.org template by Raphaël Hertzog http://raphaelhertzog.com/go/ooo-template License: GPL-2+

30

Test Input ●

Depends on scenario



Generate comparison data by code in test



Canned data



Data generated by previous testing



Alternative APIs



Past good versions 31

All Software Has Bugs. (heading attribution: about three talks I attended at last years LCA !)

Attribution: 1986 BYTE Magazine Vol. 10 No. 11, p208 32

Unit Testing in Other Languages ●

Java : TestNG, JUnit



Python : PyUnit



PHP : PHPUnit



Perl : Test::More



Haskell : HUnit

33

Testing doesnt fix everything ●

http://knowyourmeme.com/photos/475752tree-swing-cartoon-parodies Requirements issues ●

cheap to fix early, expensive later

34

Moving on to Boost/Test ...

-

anonymous

35

Obligatory Wikipedia Quote ●

According to the wisdom of the crowds [1]: “unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine if they are fit for use” [1] http://en.wikipedia.org/wiki/Unit_testing [Accessed 22-Jan-2013] 36