什么是Flutter中的jest快照测试? [英] What is the equivalent of jest snapshot testing in Flutter?

查看:102
本文介绍了什么是Flutter中的jest快照测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Jest,它是JS的测试库,可以按照以下方式进行快照:

Using Jest, a testing library for JS, it is possible to have a "snapshot" as followed:

test('foo', () => {
    expect(42).toMatchSnapshot("my_snapshot");
})

基本上,第一次运行时,会将测试值保存到文件中。在以后的运行中,它将传递的值与文件中的值进行比较。因此,如果传递的值与该文件中的值不同,则测试将失败。

Basically, on first run this saves the tested value into a file. And on later runs, it compares the passed value with what was into the file. So that if the passed value differ from the value inside that file, the test fail.

这非常有用,因为它可以轻松创建测试。

This is quite useful because it allows to create tests easily.

可以使用Flutter提供的测试框架来做到这一点吗?

Is there any way to do this using the testing framework provided by Flutter?

推荐答案

只能使用 testWidgets

testWidgets('golden', (tester) async {
  await tester.pumpWidget(Container(
    color: Colors.red,
  ));

  await expectLater(
      find.byType(Container), matchesGoldenFile("red_container.png"));
});

首先,您必须 pump 要测试的小部件(此处

First, you have to pump the widget you want to test (here a red container).

然后可以将 matchesGoldenFile expectLater 。这将对小部件进行屏幕截图,并将其与先前保存的截图进行比较。

Then you can use matchesGoldenFile combined with expectLater. This will take a screen capture of the widget and compare it to the previously saved capture.

在第一次运行时或当您要更新黄金时,您将拥有将标志传递给颤振测试

On the first run or when you want to update your goldens, you'll have to pass a flag to flutter test:

flutter test --update-goldens

这篇关于什么是Flutter中的jest快照测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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