运行DART单元测试时无限递归

时间:2014-03-21 18:01:32

标签: unit-testing dart

在异步启动的方法中进行测试时,我有一个简单的DART单元测试示例。我观察所有测试,从第一个测试开始,一遍又一遍地重复测试,最后一次测试在每个循环中重复一次。这是代码:

    import 'dart:async';
    import 'dart:html';
    import 'package:unittest/unittest.dart';
    import 'package:crypto/crypto.dart';
    import 'package:utf/utf.dart';
    var repositoryURL = r"http://usryeddrint451v.asg.com:8080/ddrint/ServiceDirect/getRepositories";

    void main() {
        test('Test 1',(){
         var a=10;
         expect(a, equals(10));
        });
        serverTrip();
        test('Test 4',(){
         var b=10;
         expect(b, equals(10));
        });
    }

    void serverTrip(){
      test('Server trip test', () {
      var repositoryURL = r"http://usryeddrint451v.asg.com:8080/ddrint/ServiceDirect/getRepositories";  
      var bytes = encodeUtf8("demouser:luminist");
           var base64 = CryptoUtils.bytesToBase64(bytes);
           var auth = "Basic " + base64;
           var httpRequest = new HttpRequest();
           httpRequest.withCredentials = true;
           httpRequest.open('POST', repositoryURL);
           httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
           httpRequest.setRequestHeader("Authoriz

ation", auth);
       httpRequest.onLoadEnd.listen((ee) =>checkProgress());     
       httpRequest.onError.listen((ee) => handleError());
       print('Sending request');
       httpRequest.send('');
  });
}

void checkProgress(){
  print("Print -- Called checkProgress");

  var bytes = encodeUtf8("demouser:luminist");
       var base64 = CryptoUtils.bytesToBase64(bytes);
       var auth = "Basic " + base64;
       var httpRequest = new HttpRequest();

       httpRequest.withCredentials = true;

       httpRequest.open('POST', repositoryURL);
       httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       httpRequest.setRequestHeader("Authorization", auth);

       httpRequest.onLoadEnd.listen((ee) => checkProgress1());     
       httpRequest.onError.listen((ee) => handleError());
       httpRequest.send('');
}

void checkProgress1(){
  test('Check progress1 test',(){
    var a=10;
    expect(a, equals(10));
  });
}


void handleError(){
  print("Error");
}

这是输出:

unittest-suite-wait-for-done
Sending request
PASS: Test 1
PASS: Server trip test
PASS: Test 4

All 3 tests passed.
unittest-suite-success
Print -- Called checkProgress
unittest-suite-wait-for-done
Sending request
PASS: Test 1
PASS: Server trip test
PASS: Test 4
PASS: Check progress1 test

All 4 tests passed.
unittest-suite-success
Print -- Called checkProgress
unittest-suite-wait-for-done
Sending request
PASS: Test 1
PASS: Server trip test
PASS: Test 4
PASS: Check progress1 test
PASS: Check progress1 test

All 5 tests passed.
unittest-suite-success
Print -- Called checkProgress
unittest-suite-wait-for-done
Sending request
PASS: Test 1
PASS: Server trip test
PASS: Test 4
PASS: Check progress1 test
PASS: Check progress1 test
PASS: Check progress1 test

All 6 tests passed.
unittest-suite-success
Print -- Called checkProgress
unittest-suite-wait-for-done
Sending request
PASS: Test 1
PASS: Server trip test
PASS: Test 4
PASS: Check progress1 test
PASS: Check progress1 test
PASS: Check progress1 test
PASS: Check progress1 test

All 7 tests passed.
unittest-suite-success
Print -- Called checkProgress
unittest-suite-wait-for-done
Sending request
PASS: Test 1
PASS: Server trip test
PASS: Test 4
PASS: Check progress1 test
PASS: Check progress1 test
PASS: Check progress1 test
PASS: Check progress1 test
PASS: Check progress1 test

为什么异步方法会重复整个测试套件?

编辑:

我尝试用以下方法替换main方法:

void main() {
    test('Test 1',(){
     var a=10;
     expect(a, equals(10));
    });
    test('Server trip test', (){
        serverTrip();
    });
    test('Test 4',(){
     var b=10;
     expect(b, equals(10));
    });
}

并从serverTrip body远程测试。

这似乎可以保证在预期时调用serverTrip。

输出是这样的:

unittest-suite-wait-for-done
Test 1
Server trip
Sending request
Test 4
PASS: Test 1
PASS: Server trip
PASS: Test 4

All 3 tests passed.
unittest-suite-success
Print -- Called checkProgress
unittest-suite-wait-for-done
Test 1
Server trip
Sending request
Test 4
PASS: Test 1
PASS: Server trip
PASS: Test 4
PASS: Check progress1 test

All 4 tests passed.
unittest-suite-success
Print -- Called checkProgress
unittest-suite-wait-for-done
Test 1
Server trip
Sending request
Test 4
PASS: Test 1
PASS: Server trip
PASS: Test 4
PASS: Check progress1 test
PASS: Check progress1 test

All 5 tests passed.
....

和无限广告

1 个答案:

答案 0 :(得分:2)

您知道在两次测试之前都执行了serverTrip();吗?

你为什么这样做

void serverTrip(){       测试('服务器旅行测试',(){

在测试的主要部分中,您应该只有测试方法

void main(args) {

  // some initializtation (unittest, ...) may go here

  setUp(() {});
  tearDown(() {});
  test('xxx1', () { testcode});
  test('xxx2', () { testcode});
  // or
  group('g1' () {
    setUp(() {});
    tearDown(() {});
    test('xxx1', () { testcode});
    test('xxx2', () { testcode});
 });

  group('g2' () {
    setUp(() {});
    tearDown(() {});
    test('xxx3', () { testcode});
    test('xxx4', () { testcode});
 });
}

我将查看可能导致递归并更新我的答案的内容

将其更改为固定递归

void main() {
  test('Test 1', () {
    var a = 10;
    expect(a, equals(10));
  });

  test('Server trip test', () {
    serverTrip();
  });
  //serverTrip();
  test('Test 4', () {
    var b = 10;
    expect(b, equals(10));
  });

  test('checkProgress1', () {
    checkProgress1();
  });
}

void serverTrip() {
  //test('Server trip test', () {
  var repositoryURL =
      r"http://usryeddrint451v.asg.com:8080/ddrint/ServiceDirect/getRepositories";
  var bytes = encodeUtf8("demouser:luminist");
  var base64 = CryptoUtils.bytesToBase64(bytes);
  var auth = "Basic " + base64;
  var httpRequest = new HttpRequest();
  httpRequest.withCredentials = true;
  httpRequest.open('GET', repositoryURL);
  httpRequest.setRequestHeader("Content-type",
      "application/x-www-form-urlencoded");
  httpRequest.setRequestHeader("Authorization", auth);
  httpRequest.onLoadEnd.listen((ee) => checkProgress());
  httpRequest.onError.listen((ee) => handleError());
  print('Sending request');
  httpRequest.send('');
  // });
}

void checkProgress1() {
  var a = 10;
  expect(a, equals(10));
}

修改

添加一些警卫以使异步测试工作

import 'dart:async';
import 'dart:html';
import 'package:unittest/unittest.dart';
import 'package:crypto/crypto.dart';
import 'package:utf/utf.dart';
var repositoryURL =
    r"'http://127.0.0.1:3030/playground/test/interacting_test_s/index.html'";

void main() {
  test('Test 1', () {
    var a = 10;
    expect(a, equals(10));
  });

  test('Server trip test', () {

    // create a callback, the unittest framework expects this callback to be called once
    var callback = expectAsync((){}); 
    serverTrip(callback);
  });
  //serverTrip();
  test('Test 4', () {
    var b = 10;
    expect(b, equals(10));
  });

  test('checkProgress1', () {
    // create a callback, the unittest framework expects this callback to be called once
    var callback = expectAsync((){});
    checkProgress1(callback);
  });
}

void serverTrip(callback) {
  //test('Server trip test', () {
  var repositoryURL =
      r"http://usryeddrint451v.asg.com:8080/ddrint/ServiceDirect/getRepositories";
  var bytes = encodeUtf8("demouser:luminist");
  var base64 = CryptoUtils.bytesToBase64(bytes);
  var auth = "Basic " + base64;
  var httpRequest = new HttpRequest();
  //    httpRequest.withCredentials = true;
  httpRequest.open('GET', repositoryURL);
  //    httpRequest.setRequestHeader("Content-type",
  //        "application/x-www-form-urlencoded");
  //    httpRequest.setRequestHeader("Authorization", auth);
  httpRequest.onLoadEnd.listen((ee) => checkProgress(callback));
  httpRequest.onError.listen((ee) => handleError());
  print('Sending request');
  httpRequest.send('');
  // });
}

void checkProgress(callback) {
  print("Print -- Called checkProgress");

  var bytes = encodeUtf8("demouser:luminist");
  var base64 = CryptoUtils.bytesToBase64(bytes);
  var auth = "Basic " + base64;
  var httpRequest = new HttpRequest();

  //httpRequest.withCredentials = true;

  httpRequest.open('GET', repositoryURL);
  //  httpRequest.setRequestHeader("Content-type",
  //      "application/x-www-form-urlencoded");
  //  httpRequest.setRequestHeader("Authorization", auth);

  httpRequest.onLoadEnd.listen((ee) => checkProgress1(callback));
  httpRequest.onError.listen((ee) => handleError());
  httpRequest.send('');
}

void checkProgress1(callback) {
  var a = 10;
  expect(a, equals(10));

  // the callback is passed as parameter, it could also be a top level variable
  // but this way it is dangerous that the tests interfere with each other
  // the second test might get executed while the first waits for a response to
  // the HttpRequest it made
  callback();
}


void handleError() {
  print("Error");
}