AS2的UnitTest框架?

时间:2009-10-20 13:37:13

标签: flash unit-testing actionscript-2

我刚试过ASUnit并没有成功, 所以我正在寻找更简单的东西,不需要花哨的UI。 跟踪输出很好。

ASUnit并不成功,因为它有一些奇怪的原因 在/ Applications的所有子目录中生成AllTests.as文件。 我无法弄清楚如何阻止这种情况发生,所以我 寻找更简单的东西。我做了很多单元测试 在ruby,c ++和objective c中,所以对我来说并不是全新的。

我的项目针对Flash 9并使用ActionScript 2。 我在Flash CS4中工作。

需要测试的代码是数学函数,需要 一个或两个浮点参数并返回一个浮点值, 所以它非常适合测试。

有什么想法吗?

更新:现在我已经编写了自己的测试代码。 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)
}

输出是这样的:

running tests
testing .... test_a
testing .... test_b
testing .... test_c
All 3 tests executed successfully

1 个答案:

答案 0 :(得分:1)

我认为as2的单元测试框架并不多......

我发现as2lib,主页已经死了,但你仍然可以访问API page并获取其project on SourceForge

中的代码

也有astuce。但它的as2开发已停止。

您也可以尝试将PerformanceTest from gskinner移植到as2 ......:P

相关问题