a (hopefully brief) intro to unit testingwsumner/teaching/373/05-unittesting.pdf3 levels of testing...
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/33.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/34.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/35.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/36.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/37.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/38.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/39.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/40.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/41.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/42.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/43.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/44.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/45.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/46.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/47.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/48.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/49.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/50.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/51.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/52.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/53.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/54.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/55.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/56.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/57.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/58.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/59.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/60.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/61.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/62.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/63.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/64.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/65.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/66.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/67.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/68.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/69.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/70.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/71.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/72.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/73.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/74.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/75.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022052022/6037511ca2cd47721859672c/html5/thumbnails/76.jpg)
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.