2014-01-31 36 views

回答

3

尝试添加下面一行在你的main()测试

void main(List<String> args) { 
    useHtmlEnhancedConfiguration(); // (or some other configuration setting) 
    unittestConfiguration.timeout = new Duration(seconds: 3); // <<== add this line 

    test(() { 
    // do some tests 
    }); 
} 

你可以很容易地安装使用setUp()tearDown()Timer

library x; 

import 'dart:async'; 
import 'package:unittest/unittest.dart'; 

void main(List<String> args) { 
    group("some group",() { 
    Timer timeout; 
    setUp(() { 
     // fail the test after Duration 
     timeout = new Timer(new Duration(seconds: 1),() => fail("timed out")); 
    }); 

    tearDown(() { 
     // if the test already ended, cancel the timeout 
     timeout.cancel(); 
    }); 

    test("some very slow test",() { 
     var callback = expectAsync0((){}); 
     new Timer(new Duration(milliseconds: 1500),() { 
     expect(true, equals(true)); 
     callback(); 
     }); 
    }); 

    test("another very slow test",() { 
     var callback = expectAsync0((){}); 
     new Timer(new Duration(milliseconds: 1500),() { 
     expect(true, equals(true)); 
     callback(); 
     }); 
    }); 


    test("a fast test",() { 
     var callback = expectAsync0((){}); 
     new Timer(new Duration(milliseconds: 500),() { 
     expect(true, equals(true)); 
     callback(); 
     }); 
    }); 

    }); 
} 

这个时间保护失灵,整个组,但组可以嵌套,因此您可以完全控制应该监视哪些测试超时。

+0

这是所有测试的全球设置,对不对?目前我们无法设置指定测试的超时时间。 – Freewind

+0

@Freewind我添加了每个测试超时的示例。到目前为止,不知道是否有情况下没有。刚发明它;-) –