Upload
pooyasi
View
18
Download
1
Embed Size (px)
Citation preview
Introduction to Unit Testing
Author : Pooya Sagharchi HaJanuary 27, 2015
Agenda
● What is Unit Testing ?
● Why should we Unit Test?
● Isolate units for testing
● UnitTest++ framework
● HippoMocks framework
Unit Testing● The smallest testable parts of an application, are
called units
● Unit Test is a method in which a programmer
tests individual units
● execute a program using artificial data
Unit Testing
input
Main Class Testing Class
output
True or False
Isolate Unit Testing● it goal is to isolate each part of the program and show that the
individual parts are correct
Class 1
Class 2
Class 3
Class 4
Class 3
Testing Class
input
output
True or False
Why Unit Testing?
● helps to find the problems early in the software
development
● Can be used to validate that the code
functionality still works after code change
UnitTest++
● lightweight
● Simplicity
● Portability ( platform Linux, Mac and windows )
● Speed
Getting started:1. Create a header file on UnitTests folder2. Include UnitTest++.h and TestReporterStdout.h3. Write your test in TEST body4. Compile it
UnitTest++
UnitTest++For example : #include "UnitTest++.h"#include "TestReportsStdout.h"TEST(ChechSumTest){
int sum =0;for(int i=0; i < 5; ++i)
sum += i;CHECK_EQUAL(10, sum);}
UnitTest++● Test macro
Define : simply put the following code in a .cpp file of your choice.
Syntax : TEST(YourTestName) { }
UnitTest++● Suit macro
Define : A suite serves as a namespace for test names, so that the same test name can be used in two different contexts.
Syntax : SU ITE(YourSuiteName) { TEST(YourTestName) { }
TEST(YourOtherTestName) { } }
UnitTest++● Check macro
Define : It will fail if the boolean expression evaluates to false and conversely.
Syntax : TEST(YourTestName) {
Check(value); }
UnitTest++● Check Equal macro
Define : It will compare expected argument and actual argument.
Syntax : TEST(YourTestName) {
Check_EQUAL(expected, actual); }
UnitTest++● Check Close macro
Define : It is used for floating-point comparison.
Syntax : TEST(YourTestName) {
Check_CLOSE(expected, actual, tolerance); }
UnitTest++● Array macroThere is a set of check macros for array comparison as well.
Syntax : TEST(YourTestName) {
Check_Array_EQUAL(expected, actual, count);Check_Array_CLOSE(expected, actual, tolerance);Check_Array2D_EQUAL(expected, actual, row, columns, tolerance);
}
UnitTest++
● Exception check macros
Define : which asserts that its enclosed expression throws the specified type.
Syntax : TEST(YourTestName) {
Check_THROW(expression, ExpectedExpectionType); }
HippoMocksGetting started:1. Create a header file on UnitTests folder2. Include UnitTest++.h and TestReporterStdout.h
and hippmocks.h3. Write your test in TEST body4. Compile it
HippoMocks● MockRepository class
Define : Create a MockRepository on the stack.
Syntax : TEST(YourTestName) {
MockRepository mocks; }
HippoMocks● Interface Mock
Define : Used to create an instance of a class that implements a given interface.
Syntax : TEST(YourTestName) {
MockRepository mocks;ClassName *classnameMock = mocks.Mock<ClassName>(); }
HippoMocks● Expected Call
Define : Indicates that the given method on the mock will be called. If the method is not called before the Repository destructs, a runtime error is generated.
Syntax : TEST(YourTestName) {
MockRepository mocks;ClassName *classnameMock = mocks.Mock<ClassName>();mock.ExpectCall(obj, func); }
HippoMocks● Expected Call
Define : AutoExpect can be called prior to calls to ExpectCall but the order of your program increase.
Syntax : TEST(YourTestName) {
MockRepository mocks;ClassName *classnameMock = mocks.Mock<ClassName>();mocks.autoExpect = false or true; }
HippoMocks● On Call
Define : Similar to ExpectCall, except that it's okay if the method doesn't get called.
Syntax : TEST(YourTestName) {
MockRepository mocks;ClassName *classnameMock = mocks.Mock<ClassName>();mock.OnCall(obj, func); }
HippoMocks● Never Call
Define : Similar to ExpectCall, except that it sets up expectation that the method will never be called.
Syntax : TEST(YourTestName) {
MockRepository mocks;ClassName *classnameMock = mocks.Mock<ClassName>();mock.NeverCall(obj, func); }
HippoMocks● TCall class
Define : This class is not instantiated directly. It's returned by MockRepository::ExpectCall The TCall class can be used to further refine the expectations of the method call.
HippoMocks● With
Define : When used, indicates what parameters expected to be passed in.
Syntax : TEST(YourTestName) {
MockRepository mocks;ClassName *classnameMock = mocks.Mock<ClassName>();mock.ExpectCall(obj, func).With(value); }
HippoMocks● Return
Define : When used, indicates what value the method will return.
Syntax : TEST(YourTestName) {
MockRepository mocks;ClassName *classnameMock = mocks.Mock<ClassName>();mock.ExpectCall(obj, func).Return(obj); }
HippoMocks● Throw
Define : When used, indicates that the method must throw an exception.
Syntax : TEST(YourTestName) {
MockRepository mocks;ClassName *classnameMock = mocks.Mock<ClassName>();mock.ExpectCall(obj, func).Throw(exception); }
HippoMocks● Do
Define : When used, indicates what the method will do by passing in a function pointer.
Syntax : TEST(YourTestName) {
MockRepository mocks;ClassName *classnameMock = mocks.Mock<ClassName>();mock.ExpectCall(obj, func).Do(function); }