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
https://github.com/tklee1975/SimpleTDD-cocos2dx

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/setupTDD.py ./

(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 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:
https://github.com/tklee1975/SimpleTDD-ccx3-demo

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s