Unit testing

28
Introduction to Unit Testing Author : Pooya Sagharchi Ha January 27, 2015

Transcript of Unit testing

Page 1: Unit testing

Introduction to Unit Testing

Author : Pooya Sagharchi HaJanuary 27, 2015

Page 2: Unit testing

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"
Page 3: Unit testing

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
Page 4: Unit testing

Unit Testing

input

Main Class Testing Class

output

True or False

Page 5: Unit testing

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"
Page 6: Unit testing

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
Page 7: Unit testing

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
Page 8: Unit testing

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++

Page 9: Unit testing

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

Page 10: Unit testing

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.
Page 11: Unit testing

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!
Page 12: Unit testing

UnitTest++● Check macro

Define : It will fail if the boolean expression evaluates to false and conversely.

Syntax : TEST(YourTestName) {

Check(value); }

Page 13: Unit testing

UnitTest++● Check Equal macro

Define : It will compare expected argument and actual argument.

Syntax : TEST(YourTestName) {

Check_EQUAL(expected, actual); }

Page 14: Unit testing

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.
Page 15: Unit testing

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

}

Page 16: Unit testing

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.
Page 17: Unit testing

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

Page 18: Unit testing

HippoMocks● MockRepository class

Define : Create a MockRepository on the stack.

Syntax : TEST(YourTestName) {

MockRepository mocks; }

Page 19: Unit testing

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

Page 20: Unit testing

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

Page 21: Unit testing

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

Page 22: Unit testing

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

Page 23: Unit testing

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

Page 24: Unit testing

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.

Page 25: Unit testing

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

Page 26: Unit testing

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

Page 27: Unit testing

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

Page 28: Unit testing

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