Unit testing

Preview:

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

Mohammad Ali Sharpasand
Maybe "Isolate Units for testing
Mohammad Ali Sharpasand
Probably "Why should we Unit Test"

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

Mohammad Ali Sharpasand
The latest bullet is ambiguous. I cannot understand are you commanding the audience?! or defining something?
Mohammad Ali Sharpasand
a programmer tests
Mohammad Ali Sharpasand
..., are called units

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

Mohammad Ali Sharpasand
Also, maybe changing appearance of the leftmost arrow is a good idea to distinguish it from data flows.
Mohammad Ali Sharpasand
I suggest putting some arrows between classes on the left to show data flow of them
Mohammad Ali Sharpasand
It's goal is to ...and maybe "to show that each unit is correct"

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

Mohammad Ali Sharpasand
Helps to find the problems early in the software development

UnitTest++

● lightweight

● Simplicity

● Portability ( platform Linux, Mac and windows )

● Speed

Mohammad Ali Sharpasand
It is a good idea that your bullets be coherent and of the same kind.the first one is a fact but others are titles

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) { }

Mohammad Ali Sharpasand
You are supposed to clearly state what a tool does in its definition.

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) { } }

Mohammad Ali Sharpasand
This is an actual definition. Good job!

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); }

Mohammad Ali Sharpasand
It is used for floating-point comparison.

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); }

Mohammad Ali Sharpasand
Which ... is a phrase describing what is came before it, not a sentence.

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); }