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;

Storyboard about SimpleTDD

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;

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 Android.mk 

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 Android.mk

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 createTest.sh to create the TestSuite Class
    • run "./script/createTest.sh 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 Android.mk
  • 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: