cocos2d-x: Adding Unit Test Framework “SimpleTDD”

SimpleTDD is a simple unit test framework that help you to develop tests to examine the behaviour of your codes;

My Goals is to help developer to make a Test for specific function easy and fast.

Storyboard about SimpleTDD

SimpleTDD demo flow

Setup Overview

This is the steps to install SimpleTDD in your project:

  1. Download SimpleTDD
  2. Run the setup script
  3. Set Macro for Debug version
  4. Add Test Button
  5. Create Unit Tests

Required software for SimpleTDD is just python, which should be installed when Xcode is installing;

Download SimpleTDD

Simplify clone the SimpleTDD project from the Github

Run the setup script

Setup script is python script that help you to copy Library classes and script to your project;

cd (your project path)
../SimpleTDD/script/ ./

(Assume your project path and SimpleTDD have the same parent folder)

After setup script is run, add the files to your project build;
For XCode (iOS):

  • File -> Add Files to “your project”
  • Select “UnitTest” and “SimpleTDD” folders

For Android: Add them in the

Set Macro for Debug version

SimpleTDD need you to set Macro “ENABLE_TDD” to open the route to the Test Main Menu so that It won’t affect the release build;

For XCode(iOS):

  • Open “Build Setting”
  • Search for “Macro”
  • Modify “Preprocessor Macros” values
  • Add “ENABLE_TDD”

For Android: Define “-DENABLE_TDD” at “LOCAL_CPPFLAGS”  in

Add Test Button

To enter the Test Main, you need add a button at your main scene;

This are the codes:

// At the header
#include “TDDHelper.h”

// Inside the Scene or Layer setup method
TDDHelper::addTestButton(this, Vec2(100, 80));

Create Unit Tests

These are the steps to create unit tests that help you to test individual code; You are going to do these steps many many times; It will be fine because it is not hard to do;

Create the TestSuite Class

  • Run the to create the TestSuite Class
    • run “./script/ Example1”
    • It will generate source code “Example1Test.h” & “Example1Test.cpp” in Classes/UnitTest Folder
  • Add Test Class to project
    • For Xcode, use Add files to project
    • For Android, include these files in
  • Edit “MyTDDCase.h”
    • MyTDDCase.h is the header file to control which Unit Test will be shown in the Test Menu
    • First, include the new Test Class using “#include Example1Test.h”
    • Second, add “TEST(Example1Test)” in the TDD_CASES block
  • When it is done, the test suite “Example1” will be shown

Create a test in the TestSuite Class

  • By default, there is test method called “subTest” in the TestSuite class; it will simply print something to the console;
  • To add a new test (e.g: testSpriteRotate), just follow the way “subTest” do;
    • Define the method in .h & .cpp;  example: void Example1Test::testSpriteRotate(Ref *sender);
    • Add the method in “setSubTest” method: example: SUBTEST(Example1Test::testSpriteRotate)
    • Do the unit test code inside testSpriteRotate method
    • Test & Run!!!

SimpleTDD Demo

A simple demo of SimpleTDD is located at github:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s