2

考虑以下代码:

  group('Tweets: ', () {
    test('Create tweets', () {
      var tweets = new Tweets();
      expect(tweets, isNull);  // intentionally wrong!    
    });

    test('Add', () {
      var tweets = new Tweets();
      expect(tweets, isNotNull);      
    });

  });

给出以下 ok 结果:

dart --enable-checked-mode TwitterSearchTest.dart

unittest-suite-wait-for-done
FAIL: Tweets:  Create tweets
  Expected: null
       but: was <Instance of 'Tweets'>.

  #0      DefaultFailureHandler.fail (package:unittest/src/expect.dart:86:5)
  #1      DefaultFailureHandler.failMatch (package:unittest/src/expect.dart:90:9)
  #2      expect (package:unittest/src/expect.dart:55:29)
  #3      main.<anonymous closure>.<anonymous closure> (file:///C:/Users/John/tmp/Dart/TwitterSearchTest/TwitterSearchTest.dart:8:13)
  #4      TestCase.run (package:unittest/src/test_case.dart:83:11)
  #5      _nextBatch._nextBatch.<anonymous closure> (package:unittest/unittest.dart:807:19)
  #6      guardAsync (package:unittest/unittest.dart:766:19)

PASS: Tweets:  Add

1 PASSED, 1 FAILED, 0 ERRORS
Unhandled exception:
Exception: Some tests failed.
#0      Configuration.onDone (package:unittest/src/config.dart:130:7)
#1      _completeTests (package:unittest/unittest.dart:835:17)
#2      _nextBatch._nextBatch (package:unittest/unittest.dart:818:17)
#3      runTests.runTests.<anonymous closure> (package:unittest/unittest.dart:755:16)
#4      _defer.<anonymous closure> (package:unittest/unittest.dart:712:13)
#5      _ReceivePortImpl._handleMessage (dart:isolate-patch:37:92)

我的问题:

  • 如何摆脱结果中的调用堆栈?
  • 我假设最后一个未处理的异常仍然是 unittester 中的错误?
4

3 回答 3

2

我在我的一个项目中做了类似的事情。首先你需要创建一个新的Unittest 配置类,然后你可以覆盖默认onDone方法。然后,您需要在第一次测试之前将其作为单元测试的配置传递。

一个小例子:

import 'package:unittest/unittest.dart' as ut;

class MyConfig extends ut.Configuration {

  MyConfig();

  void onDone(int passed, int failed, int errors, List<ut.TestCase> results,
          String uncaughtError) {
    // Only look at first failure:
    if(failed > 0) {
      var fail;
      for(var test in results) {
        if(test.result == 'fail' || test.result == 'error') {
          fail = test;
          break;
        }
      }

      print('Failed at: ${fail.description}');
      print(fail.message);
    }
  }
}

void main() {
  var unitConf = new MyConfig();
  ut.configure(unitConf);

  // Tests here ...
}

另外,如果您很好奇,尽管与答案不完全相关,您可以在此处查看我的项目,在该项目中我高度自定义了 unittest 配置,以便为 dart 创建一个迷你教程(目前尚未完成,但有关覆盖的代码unittests 是功能性的)。有问题的类在 lib/src/config_koans.dart

于 2012-12-13T13:38:53.770 回答
1

尝试..

import 'package:unittest/unittest.dart';

main() {
  SimpleConfiguration config = new SimpleConfiguration();
  config.throwOnTestFailures = false;
  config.stopTestOnExpectFailure = false;
  unittestConfiguration = config;

  test(....
于 2014-09-12T13:50:39.997 回答
1

也许测试库从那以后发生了变化?我建议这个

        // Use by having this line unittestConfiguration = new FilterTests(); ensureInitialized();
    class FilterTests extends Configuration {
      // Means you can use filterTests('some test filter'); runTests();
      get autoStart => false;

      // Tidies up output a bit 
      void onDone(bool success) { 
        try {
          super.onDone(success);
        }
        catch (e) {
          print (e);
        }
      }
    }

    void main() {
    // ... 
    unittestConfiguration = new FilterTests();
      ensureInitialized();
    // ...
    // Option: filterTests('my filter here');
    runTests();
    }
于 2013-04-16T17:12:41.803 回答