在单元测试中隐藏调用堆栈

时间:2012-12-13 12:34:04

标签: unit-testing dart

请考虑以下代码:

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

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

  });

给出以下结果:

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中的错误?

3 个答案:

答案 0 :(得分:2)

我在其中一个项目中做了类似的事情。首先,您需要创建一个新的Unittest configuration class,然后您可以覆盖默认的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 ...
}

此外,如果你很好奇,虽然与答案并不严格相关,你可以看到我的项目here,我在其中高度自定义unittest配置,以便为dart创建一个迷你教程(截至目前)不完整但有关覆盖单元测试的代码是有效的)。有问题的类在lib / src / config_koans.dart

答案 1 :(得分: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();
    }

答案 2 :(得分:1)

尝试..

import 'package:unittest/unittest.dart';

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

  test(....