Agenda:
1) Testing normal functions.
2) Mock the functions.
3) Advanced Mocking.
4) Mock the class functions.
1) Testing normal functions.
Step1: Download the latest version of source code from here: https://sourceforge.net/projects/cxxtest/files/cxxtest/
Step2: Write your test suite & test cases.
First Simple Test Suite with one test:
#include <cxxtest/TestSuite.h>
class FirstTestSuite : public CxxTest::TestSuite
{
public:
void testAddition( void )
{
TS_ASSERT( 100 + 100 > 1 );
TS_ASSERT_EQUALS( 1000 + 1000, 2000 );
}
};
Step3: Generate the tests CPP code file:
F:\CODING\cxxtest-4.4\cxxtest-4.4\bin>cxxtestgen --error-printer -o tests.cpp ..\shakti\FirstCxxTestSuite.h
Step4: Compile the CPP code
F:\CODING\cxxtest-4.4\cxxtest-4.4\bin>g++ -I../ -o main tests.cpp
Step5: Run the Tests!
F:\CODING\cxxtest-4.4\cxxtest-4.4\bin>main.exe
Running cxxtest tests (1 test).OK!
THAT'S IT.
2) Mock the functions.
Mock Objects are a very useful concept for testing complex software. The key idea is to pass special objects to tested code that facilitates the testing process. For instance, a class that implements a protocol over TCP might rely on an abstract ISocket interface. Then a mock testing strategy could pass a MockSocket object that does anything that is useful for testing (e.g., keep a log of all data “sent” to verify later).
However, when a challenge for C/C++ developers is that you may need to call global functions which you cannot override. Consider any code that uses fopen(), fwrite() and fclose(). It is not very elegant to have this code actually create files while being tested. Even more importantly, you need to test how the code behaves when “bad” things happen (e.g., when fopen() fails). Handling these types of exceptional conditions is often a very challenging issue for software testing.
CxxTest addresses this challenge by providing a generic mechanism for defining mock global functions. The next section illustrates this mechanism for a single global function. The following section provides more detail about specific features of CxxTest’s support for mock testing.
STEP-1:Declare Mock Functions.
// time_mock.h
#include <time.h>
#include <cxxtest/Mock.h>
CXXTEST_MOCK_GLOBAL(time_t, /* Return type */
time, /* Name of the function */
(time_t *t), /* Prototype */
(t) /* Argument list */);
STEP-2: Mock Functions in Tested Code
// rand_example.cpp
#include <time_mock.h>
int generateRandomNumber()
{
return T::time(NULL) * 3;
}
STEP-3: Mock Source Files
// time_real.cpp
#define CXXTEST_MOCK_REAL_SOURCE_FILE
#include <time_mock.h>
// time_mock.cpp
#define CXXTEST_MOCK_TEST_SOURCE_FILE
#include <time_mock.h>
STEP-4: Test Suites using Mock Functions
// MockTestSuite.h
#include <cxxtest/TestSuite.h>
#include <time_mock.h>
int generateRandomNumber();
class MockObject : public T::Base_time
{
public:
MockObject(int initial) : counter(initial) {}
int counter;
time_t time(time_t *) { return counter++; }
};
class TestRandom : public CxxTest::TestSuite
{
public:
void test_generateRandomNumber()
{
MockObject t(1);
TS_ASSERT_EQUALS(generateRandomNumber(), 3);
TS_ASSERT_EQUALS(generateRandomNumber(), 6);
TS_ASSERT_EQUALS(generateRandomNumber(), 9);
}
};
STEP-5: Building the Test Runner
cxxtestgen --error-printer -o runner.cpp MockTestSuite.h
g++ -o runner -I. -I$CXXTEST runner.cpp time_mock.cpp rand_example.cpp
Running cxxtest tests (1 test).OK!
3) Advanced Mocking
i) Functions in Namespaces
CXXTEST_MOCK( Files_FileExists, /* Suffix of mock class */
bool, /* Return type */
fileExists, /* Name of mock member */
( const String &name ), /* Prototype */
Files::FileExists, /* Name of real function */
( name ) /* Parameter list */ );
ii) Overloaded Functions
The CXXTEST_MOCK and CXXTEST_MOCK_VOID macros have a flexible interface that can provide mock global function definitions for overloaded functions. The arguments simply need to specify different mock class names, mock member names and different prototype definitions. These different mock declarations will generate different mock objects that can be explicitly referenced in a test suite.
iii) The Mock Namespace
The default namespace for mock functions is T::. This namespace can be changed by defining the CXXTEST_MOCK_NAMESPACE macro.
4) Mock the class functions.
Class's function should be made static.
Comments
Post a Comment