a (hopefully brief) intro to unit testingwsumner/teaching/373/05-unittesting.pdf3 levels of testing...

76
A (hopefully brief) Intro to Unit Tesng CMPT 373 Soſtware Development Methods Nick Sumner with material from the GoogleTest documentaon

Upload: others

Post on 08-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

A (hopefully brief) Intro toUnit Testing

CMPT 373Software Development Methods

Nick Sumnerwith material from the GoogleTest documentation

Page 2: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

2

Levels of Testing

● Many different levels of testing can be considered:– Unit Tests

– Integration Tests

– System Tests

– Acceptance Tests

– …

Page 3: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

3

Levels of Testing

● Many different levels of testing can be considered:– Unit Tests

– Integration Tests

– System Tests

– Acceptance Tests

– …

● The simplest of these is Unit Testing– Testing the smallest possible fragments of a program

Page 4: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

4

Unit Testing

● Try to ensure that the functionality of each component works in isolation

Page 5: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

5

Unit Testing

● Try to ensure that the functionality of each component works in isolation– Unit Test a car:

Wheels work. Steering wheel works....

Page 6: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

6

Unit Testing

● Try to ensure that the functionality of each component works in isolation– Unit Test a car:

Wheels work. Steering wheel works....– Integration Test a car:

Steering wheel turns the wheels....

Page 7: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

7

Unit Testing

● Try to ensure that the functionality of each component works in isolation– Unit Test a car:

Wheels work. Steering wheel works....– Integration Test a car:

Steering wheel turns the wheels....– System Test a car:

Driving down the highway with the air conditioning on works...

Page 8: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

8

Unit Testing

● Try to ensure that the functionality of each component works in isolation– Unit Test a car:

Wheels work. Steering wheel works....– Integration Test a car:

Steering wheel turns the wheels....– System Test a car:

Driving down the highway with the air conditioning on works....

● Not testing how well things are glued together.

Page 9: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

9

Unit Testing

● Try to ensure that the functionality of each component works in isolation– Unit Test a car:

Wheels work. Steering wheel works....– Integration Test a car:

Steering wheel turns the wheels....– System Test a car:

Driving down the highway with the air conditioning on works....

● Not testing how well things are glued together.

Why? How is this beneficial?

Page 10: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

10

Unit Tests

● A dual view:– They specify the expected behavior of individual components

Page 11: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

11

Unit Tests

● A dual view:– They specify the expected behavior of individual components

– An executable specification

Page 12: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

12

Unit Tests

● A dual view:– They specify the expected behavior of individual components

– An executable specification

● Can even be built first & used to guide development– Usually called Test Driven Development

Page 13: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

13

Unit Tests

● Some guiding principles:– Focus on one component in isolation

– Be simple to set up & run

– Be easy to understand

Page 14: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

14

Unit Tests

● Some guiding principles:– Focus on one component in isolation

– Be simple to set up & run

– Be easy to understand

● Usually managed by some automating framework ....

Page 15: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

15

GoogleTest

● Increasingly used framework for C++– Not dissimilar from JUnit

Page 16: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

16

GoogleTest

● Increasingly used framework for C++– Not dissimilar from JUnit

● Test cases are written as functions:

TEST(TriangleTest, isEquilateral) { Triangle tri{2,2,2}; EXPECT_TRUE(tri.isEquilateral());}

Page 17: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

17

GoogleTest

● Increasingly used framework for C++– Not dissimilar from JUnit

● Test cases are written as functions:

TEST(TriangleTest, isEquilateral) { Triangle tri{2,2,2}; EXPECT_TRUE(tri.isEquilateral());}The TEST macro definesindividual test cases.

Page 18: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

18

GoogleTest

● Increasingly used framework for C++– Not dissimilar from JUnit

● Test cases are written as functions:

TEST(TriangleTest, isEquilateral) { Triangle tri{2,2,2}; EXPECT_TRUE(tri.isEquilateral());}

The first argumentnames related tests.

Page 19: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

19

GoogleTest

● Increasingly used framework for C++– Not dissimilar from JUnit

● Test cases are written as functions:

TEST(TriangleTest, isEquilateral) { Triangle tri{2,2,2}; EXPECT_TRUE(tri.isEquilateral());}

The second argumentnames individual test cases.

Page 20: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

20

GoogleTest

● Increasingly used framework for C++– Not dissimilar from JUnit

● Test cases are written as functions:

TEST(TriangleTest, isEquilateral) { Triangle tri{2,2,2}; EXPECT_TRUE(tri.isEquilateral());}EXPECT and ASSERT macros

provide correctness oracles.

Page 21: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

21

GoogleTest

● Increasingly used framework for C++– Not dissimilar from JUnit

● Test cases are written as functions:

TEST(TriangleTest, isEquilateral) { Triangle tri{2,2,2}; EXPECT_TRUE(tri.isEquilateral());}ASSERT oracles terminate the program when they fail.

EXPECT oracles allow the program to continue running.

Page 22: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

22

GoogleTest

● Increasingly used framework for C++– Not dissimilar from JUnit

● Test cases are written as functions.

● TEST() cases are automatically registered with GoogleTest and are executed by the test driver.

Page 23: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

23

GoogleTest

● Increasingly used framework for C++– Not dissimilar from JUnit

● Test cases are written as functions.

● TEST() cases are automatically registered with GoogleTest and are executed by the test driver.

● Some tests require common setUp & tearDown– Group them into test fixtures

– A fresh fixture is created for each test

Page 24: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

24

GoogleTest - Fixtures

class StackTest : public ::testing::Test { protected: void SetUp() override { s1.push(1); s2.push(2); s2.push(3); }

void TearDown() override { }

Stack<int> s1; Stack<int> s2;}; Derive from the fixture base class

Page 25: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

25

GoogleTest - Fixtures

class StackTest : public ::testing::Test { protected: void SetUp() override { s1.push(1); s2.push(2); s2.push(3); }

void TearDown() override { }

Stack<int> s1; Stack<int> s2;}; SetUp() will be called before

all tests using the fixture

Page 26: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

26

GoogleTest - Fixtures

class StackTest : public ::testing::Test { protected: void SetUp() override { s1.push(1); s2.push(2); s2.push(3); }

void TearDown() override { }

Stack<int> s1; Stack<int> s2;};TearDown() will be called after

all tests using the fixture

Page 27: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

27

GoogleTest - Fixtures

Use the fixture in test cases defined with TEST_F:

TEST_F(StackTest, popOfOneIsEmpty) { s1.pop(); EXPECT_EQ(0, s1.size());}

Page 28: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

28

GoogleTest - Fixtures

Use the fixture in test cases defined with TEST_F:

TEST_F(StackTest, popOfOneIsEmpty) { s1.pop(); EXPECT_EQ(0, s1.size());}

Page 29: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

29

GoogleTest - Fixtures

Use the fixture in test cases defined with TEST_F:

TEST_F(StackTest, popOfOneIsEmpty) { s1.pop(); EXPECT_EQ(0, s1.size());}

{ StackTest t; t.SetUp(); t.popOfOneIsEmpty(); t.TearDown();}

Behaves like

Page 30: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

30

GoogleTest - Fixtures

Use the fixture in test cases defined with TEST_F:

TEST_F(StackTest, popOfOneIsEmpty) { s1.pop(); EXPECT_EQ(0, s1.size());}

A different expectation than before!

Page 31: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

31

GoogleTest - Fixtures

Use the fixture in test cases defined with TEST_F:

TEST_F(StackTest, popOfOneIsEmpty) { s1.pop(); EXPECT_EQ(0, s1.size());}

expectedvalue

Page 32: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

32

GoogleTest - Fixtures

Use the fixture in test cases defined with TEST_F:

TEST_F(StackTest, popOfOneIsEmpty) { s1.pop(); EXPECT_EQ(0, s1.size());}

expectedvalue

observedvalue

Page 33: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

33

GoogleTest

● Many different assertions and expectations available

ASSERT_TRUE(condition);ASSERT_FALSE(condition);ASSERT_EQ(expected,actual);ASSERT_NE(val1,val2);ASSERT_LT(val1,val2);ASSERT_LE(val1,val2);ASSERT_GT(val1,val2);ASSERT_GE(val1,val2);

EXPECT_TRUE(condition);EXPECT_FALSE(condition);EXPECT_EQ(expected,actual);EXPECT_NE(val1,val2);EXPECT_LT(val1,val2);EXPECT_LE(val1,val2);EXPECT_GT(val1,val2);EXPECT_GE(val1,val2);

Page 34: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

34

GoogleTest

● Many different assertions and expectations available

● More information available online– github.com/google/googletest/blob/master/googletest/docs/Primer.md

– github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md

Page 35: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

35

Common Patterns (Ammonn & Offutt)

● Checking State– Final State

● Prepare initial state● Run test● Check final state

Page 36: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

36

Common Patterns (Ammonn & Offutt)

● Checking State– Final State

● Prepare initial state● Run test● Check final state

– Pre and Post conditions● Check initial state as well as final state

Page 37: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

37

Common Patterns (Ammonn & Offutt)

● Checking State– Final State

● Prepare initial state● Run test● Check final state

– Pre and Post conditions● Check initial state as well as final state

– Relative effects● Check final state relative to some initial state

Page 38: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

38

Common Patterns (Ammonn & Offutt)

● Checking State– Final State

● Prepare initial state● Run test● Check final state

– Pre and Post conditions● Check initial state as well as final state

– Relative effects● Check final state relative to some initial state

– Round trips● Check behavior on transform/inverse transform pairs

Page 39: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

39

Common Patterns (Ammonn & Offutt)

● Checking Interactions/Behavior

Page 40: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

40

Common Patterns (Ammonn & Offutt)

● Checking Interactions/Behavior

void walkAroundSquare(Person& person) { person.step(); person.turnRight(); person.step(); person.turnRight(); person.step(); // Skipped: person.turnRight(); person.step();}

Page 41: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

41

void walkAroundSquare(Person& person) { person.step(); person.turnRight(); person.step(); person.turnRight(); person.step(); // Skipped: person.turnRight(); person.step();}

Common Patterns (Ammonn & Offutt)

● Checking Interactions/Behavior

Intended

Page 42: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

42

void walkAroundSquare(Person& person) { person.step(); person.turnRight(); person.step(); person.turnRight(); person.step(); // Skipped: person.turnRight(); person.step();}

Common Patterns (Ammonn & Offutt)

● Checking Interactions/Behavior

Intended

Page 43: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

43

void walkAroundSquare(Person& person) { person.step(); person.turnRight(); person.step(); person.turnRight(); person.step(); // Skipped: person.turnRight(); person.step();}

Common Patterns (Ammonn & Offutt)

● Checking Interactions/Behavior

Intended

Page 44: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

44

void walkAroundSquare(Person& person) { person.step(); person.turnRight(); person.step(); person.turnRight(); person.step(); // Skipped: person.turnRight(); person.step();}

Common Patterns (Ammonn & Offutt)

● Checking Interactions/Behavior

Intended

Page 45: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

45

void walkAroundSquare(Person& person) { person.step(); person.turnRight(); person.step(); person.turnRight(); person.step(); // Skipped: person.turnRight(); person.step();}

Common Patterns (Ammonn & Offutt)

● Checking Interactions/Behavior

Intended

Actual...

Page 46: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

46

void walkAroundSquare(Person& person) { person.step(); person.turnRight(); person.step(); person.turnRight(); person.step(); // Skipped: person.turnRight(); person.step();}

Common Patterns (Ammonn & Offutt)

● Checking Interactions/Behavior

Intended

How can we test walkAroundSquare()?

Actual...

Page 47: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

47

Common Patterns (Ammonn & Offutt)

● Checking Interactions/Behavior– Use mocks

Page 48: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

48

Common Patterns (Ammonn & Offutt)

● Checking Interactions/Behavior– Use mocks

● Testing 'fakes' that verify expected interactions

e.g. a fake Person that looks for correct steps & turns

Page 49: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

49

Common Patterns (Ammonn & Offutt)

● Checking Interactions/Behavior– Use mocks

● Testing 'fakes' that verify expected interactions

e.g. a fake Person that looks for correct steps & turns

class MockPerson : public Person { // Override methods to check for // expected behavior.};

Page 50: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

50

Common Patterns (Ammonn & Offutt)

● Checking Interactions/Behavior– Use mocks

● Testing 'fakes' that verify expected interactions

e.g. a fake Person that looks for correct steps & turns

● http://martinfowler.com/articles/mocksArentStubs.html● http://googletesting.blogspot.ca/2013/03/testing-on-toilet-testing-state-vs

.html

Page 51: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

51

Mocking Framework Example

● Frameworks exist that can automate the boilerplate behind:

Page 52: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

52

Mocking Framework Example

● Frameworks exist that can automate the boilerplate behind:

– Mocking

e.g. GoogleMock, Mockito, etc.

Page 53: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

53

Mocking Framework Example

● Frameworks exist that can automate the boilerplate behind:

– Mocking● e.g. GoogleMock, Mockito, etc.

– Dependency Injection

e.g. Google Guice, Pico Container, etc.

Page 54: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

54

Using GoogleMock

● Steps:

1) Derive a mock class from the class you wish to fake

class MockThing : public Thing { ...};

Page 55: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

55

Using GoogleMock

● Steps:

1) Derive a mock class from the class you wish to fake

2) Replace virtual calls with uses of MOCK_METHODn() or

MOCK_CONST_METHODn().

class MockThing : public Thing { public: ... MOCK_METHOD1(foo, int(int)); MOCK_METHOD1(bar, void(int));};

Page 56: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

56

Using GoogleMock

● Steps:

1) Derive a mock class from the class you wish to fake

2) Replace virtual calls with uses of MOCK_METHODn() or

MOCK_CONST_METHODn().

3) Use the mock class in your tests.

Page 57: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

57

Using GoogleMock

● Steps:

1) Derive a mock class from the class you wish to fake

2) Replace virtual calls with uses of MOCK_METHODn() or

MOCK_CONST_METHODn().

3) Use the mock class in your tests.

4) Specify expectations before use via EXPECT_CALL().● What arguments? How many times? In what order?InSequence dummy;EXPECT_CALL(mockThing, foo(Ge(20))) .Times(2) .WillOnce(Return(100)) .WillOnce(Return(200));EXPECT_CALL(mockThing, bar(Lt(5)));

Page 58: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

58

Using GoogleMock

● Steps:

1) Derive a mock class from the class you wish to fake

2) Replace virtual calls with uses of MOCK_METHODn() or

MOCK_CONST_METHODn().

3) Use the mock class in your tests.

4) Specify expectations before use via EXPECT_CALL().● What arguments? How many times? In what order?

5) Expectations are automatically checked in the destructor of the mock.

Page 59: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

59

Using GoogleMock

● Precisely specifying mock behavior

InSequence dummy;EXPECT_CALL(mockThing, foo(Ge(20))) .Times(2) // Can be omitted here .WillOnce(Return(100)) .WillOnce(Return(200));EXPECT_CALL(mockThing, bar(Lt(5)));

Page 60: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

60

Using GoogleMock

● Precisely specifying mock behavior

InSequence dummy;EXPECT_CALL(mockThing, foo(Ge(20))) .Times(2) // Can be omitted here .WillOnce(Return(100)) .WillOnce(Return(200));EXPECT_CALL(mockThing, bar(Lt(5)));

Page 61: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

61

Using GoogleMock

● Precisely specifying mock behavior

InSequence dummy;EXPECT_CALL(mockThing, foo(Ge(20))) .Times(2) // Can be omitted here .WillOnce(Return(100)) .WillOnce(Return(200));EXPECT_CALL(mockThing, bar(Lt(5)));

Page 62: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

62

Using GoogleMock

● Precisely specifying mock behavior

InSequence dummy;EXPECT_CALL(mockThing, foo(Ge(20))) .Times(2) // Can be omitted here .WillOnce(Return(100)) .WillOnce(Return(200));EXPECT_CALL(mockThing, bar(Lt(5)));

Page 63: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

63

Using GoogleMock

● Precisely specifying mock behavior

InSequence dummy;EXPECT_CALL(mockThing, foo(Ge(20))) .Times(2) // Can be omitted here .WillOnce(Return(100)) .WillOnce(Return(200));EXPECT_CALL(mockThing, bar(Lt(5)));

Page 64: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

64

Using GoogleMock

● Precisely specifying mock behavior

InSequence dummy;EXPECT_CALL(mockThing, foo(Ge(20))) .Times(2) // Can be omitted here .WillOnce(Return(100)) .WillOnce(Return(200));EXPECT_CALL(mockThing, bar(Lt(5)));

Complex behaviors can be checkedusing these basic pieces.

Page 65: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

65

Using GoogleMockTEST(walkingTests, testWalkAroundSquare) {

}

Page 66: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

66

Using GoogleMockTEST(walkingTests, testWalkAroundSquare) { MockPerson mockPerson;

walkAroundSquare(mockPerson);}

Page 67: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

67

Using GoogleMockTEST(walkingTests, testWalkAroundSquare) { MockPerson mockPerson; InSequence dummy;

walkAroundSquare(mockPerson);}

Page 68: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

68

Using GoogleMockTEST(walkingTests, testWalkAroundSquare) { MockPerson mockPerson; InSequence dummy; EXPECT_CALL(mockPerson, step()); EXPECT_CALL(mockPerson, turnRight()); … EXPECT_CALL(mockPerson, turnRight()); EXPECT_CALL(mockPerson, step());

walkAroundSquare(mockPerson);}

Page 69: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

69

Using GoogleMockTEST(walkingTests, testWalkAroundSquare) { MockPerson mockPerson; InSequence dummy; EXPECT_CALL(mockPerson, step()); EXPECT_CALL(mockPerson, turnRight()); … EXPECT_CALL(mockPerson, turnRight()); EXPECT_CALL(mockPerson, step());

walkAroundSquare(mockPerson);}

Note: Mocking couples implementation to tests.In practice it should be used carefully.

Page 70: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

70

Common Guidelines

● Have your unit tests mirror/shadow your source

– Foo.cpp → test/FooTest.cpp

Page 71: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

71

Common Guidelines

● Have your unit tests mirror/shadow your source

– Foo.cpp → test/FooTest.cpp

● Keep each test case focused

Page 72: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

72

Common Guidelines

● Have your unit tests mirror/shadow your source

– Foo.cpp → test/FooTest.cpp

● Keep each test case focused

● Try to test all conditions & lines

– Much more on this in CMPT 473

Page 73: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

73

Summary

● Unit testing provides a way to automate much of the testing process.

Page 74: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

74

Summary

● Unit testing provides a way to automate much of the testing process.

● Testing small components bootstraps confidence in the system on confidence in its constituents.

Page 75: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

75

Summary

● Unit testing provides a way to automate much of the testing process.

● Testing small components bootstraps confidence in the system on confidence in its constituents.

● Tests can verify state or behaviors.

Page 76: A (hopefully brief) Intro to Unit Testingwsumner/teaching/373/05-unittesting.pdf3 Levels of Testing Many different levels of testing can be considered: – Unit Tests – Integration

76

Summary

● Unit testing provides a way to automate much of the testing process.

● Testing small components bootstraps confidence in the system on confidence in its constituents.

● Tests can verify state or behaviors.

And this only scratches the surface.