views:

27

answers:

1

I have just tried out ASUnit and it wasn't a success, so I'm looking for something simpler, no need for fancy UI. trace output is fine.

ASUnit was not a success because it for some strange reason generated AllTests.as files in all subdirs of /Applications. I can't figure out how to stop this from happening, so I'm looking for something simpler. I have done a lot of unit testing in ruby, c++ and objective c, so its not entirely new to me.

My project is targeted Flash 9 and uses ActionScript 2. I work in Flash CS4.

The code that needs testing is math functions, that takes one or two floating point arguments and returns a floating point value, so it's well suited for testing.

Any ideas?

UPDATE: now I have written my own test code, here it is. quickndirty.

function run_tests(test_function_names:Array):Void {
    trace("running tests");
    var tests_passed:Number = 0;         
    var tests_failed:Number = 0;    
    var tests_total:Number = test_function_names.length;

    for(var i=0; i<tests_total; ++i) {
     var funname = test_function_names[i];
     var fun = this[funname];
     if(typeof fun != 'function') {
      throw("ERROR: " + funname + " is not a function!");
      return;
     }
     trace("testing .... " + funname);
     try {
      fun.call(this);
      tests_passed += 1;
     } catch(msg) {
      trace("ERROR: " + funname + "\n" + msg);
      tests_failed += 1;
     }
    }
    if(tests_failed > 0) {
     trace("" + tests_failed + " of " + tests_total + " tests failed.");
    } else {
     trace("All " + tests_total + " tests executed successfully");
    }
}

public function assert_equal_float(v_expected:Number, v_actual:Number, v_precision:Number) {
    if(v_actual == undefined) {
     throw "v is undefined";
    }
    var v = v_expected - v_actual;
    if(v < 0) v = -v;
    if(v > v_precision) {
     var s1:String = MYUtils.print_r(v_expected);
     var s2:String = MYUtils.print_r(v_actual);
     var s:String = "expected " + s1 + ", but got " + s2;
     throw s.split("\n").join("");
    }
}

public function test_a():Void {
    assert_equal_float(2, 2, 0.01);
}

public function test_b():Void {
    assert_equal_float(2.9999, 3.001, 0.01);
}

public function test_c():Void {
    assert_equal_float(3, 3, 0.01);
}

function run():Void {
    var test_function_names:Array = new Array(
     "test_a",
     "test_b",
     "test_c"
    );
    run_tests(test_function_names)
}

output is like this:

running tests
testing .... test_a
testing .... test_b
testing .... test_c
All 3 tests executed successfully
+1  A: 

I think there are not many unit testing frameworks for as2...

I found as2lib, homepage is dead, but you can still go to its API page and get the code in its project on SourceForge

There is astuce too. But its as2 development is stopped.

You may try to port PerformanceTest from gskinner to as2 too... :P

Andy Li
Thank you Andy. Agree, its unfortunate that so many important pages go dead after some years. These projects looks very useful.
neoneye