Dart是否支持参数化的单元测试? [英] Does Dart support parameterized unit tests?

查看:32
本文介绍了Dart是否支持参数化的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个Dart测试,该测试使用一组输入和预期的输出重复进行,类似于JUnit可以进行的测试.

I would like to run a Dart test which is repeated with a set of inputs and expected outputs, similar to what is possible with JUnit.

我编写了以下测试以实现类似的行为,但问题是,如果所有测试输出的计算均不正确,则测试将仅失败一次:

I wrote the following test to achieve similar behavior but the problem is that event if all the test outputs are computed incorrectly, the test will only fail once:

import 'package:test/test.dart';

void main() {
  test('formatDay should format dates correctly', () async {
    var inputsToExpected = {
      DateTime(2018, 11, 01): "Thu 1",
      ...
      DateTime(2018, 11, 07): "Wed 7",
      DateTime(2018, 11, 30): "Fri 30",
    };

    // When
    var inputsToResults = inputsToExpected.map((input, expected) =>
        MapEntry(input, formatDay(input))
    );

    // Then
    inputsToExpected.forEach((input, expected) {
      expect(inputsToResults[input], equals(expected));
    });
  });
}

我想使用参数化测试的原因是,以便可以在测试中实现以下行为:

The reason I want to use parameterized tests, is so that I can achieve the following behavior in my test:

  • 只写一个测试
  • 测试 n 个不同的输入/输出
  • 如果所有 n 个测试均被破坏,则
  • 失败 n
  • Write only one test
  • Test n different inputs/outputs
  • Fail n times if all n tests are broken

推荐答案

Dart的 test 软件包很聪明,因为它并不试图变得太聪明. test 函数只是您调用的函数,您可以在任何地方调用它,甚至在循环或其他函数调用中也可以.因此,以您的示例为例,您可以执行以下操作:

Dart's test package is smart in that it is not trying to be too clever. The test function is just a function that you call, and you can call it anywhere, even inside a loop or another function call. So, for your example, you can do something like:

group("formatDay should format dates correctly:", () {
  var inputsToExpected = {
    DateTime(2018, 11, 01): "Thu 1",
    ...
    DateTime(2018, 11, 07): "Wed 7",
    DateTime(2018, 11, 30): "Fri 30",
  };
  inputsToExpected.forEach((input, expected) {
    test("$input -> $expected", () {
      expect(formatDay(input), expected);
    });
  });
});

要记住的唯一重要的事情是,在调用 main 函数时,所有对 test 的调用都应同步发生,因此不要在异步函数中对其进行调用.如果您需要时间在运行测试之前进行设置,请改为使用 setUp .

The only important thing to remember is that all the calls to test should happen synchronously when the main function is called, so no calling it inside asynchronous functions. If you need time to set something up before running the test, do so in a setUp instead.

您还可以创建一个辅助函数,并完全删除地图(这是我通常要做的):

You can also create a helper function, and drop the map entirely (this is what I usually do):

group("formatDay should format dates correctly:", () {
  void checkFormat(DateTime input, String expected) {
    test("$input -> $expected", () {
      expect(formatDay(input), expected);
    });
  }
  checkFormat(DateTime(2018, 11, 01), "Thu 1");
  ...
  checkFormat(DateTime(2018, 11, 07), "Wed 7");
  checkFormat(DateTime(2018, 11, 30), "Fri 30");
});

在这里,每次checkFormat调用都会引入一个具有自己名称的新测试,并且每个测试都可能会单独失败.

Here each call of checkFormat introduces a new test with its own name, and each of them can fail individually.

这篇关于Dart是否支持参数化的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆