2014-01-31 38 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我添加了每個測試超時的示例。到目前爲止,不知道是否有情況下沒有。剛發明它;-) –