What is the equivalent of jest snapshot testing in Flutter?

前端 未结 1 1429
盖世英雄少女心
盖世英雄少女心 2021-01-05 09:30

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

test(\'foo\', () => {
    expect(42).toMatchSnapshot(\"my_snapsho         


        
相关标签:
1条回答
  • 2021-01-05 10:04

    It is possible for widgets only, using testWidgets:

    testWidgets('golden', (tester) async {
      await tester.pumpWidget(Container(
        color: Colors.red,
      ));
    
      await expectLater(
          find.byType(Container), matchesGoldenFile("red_container.png"));
    });
    

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

    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
    
    0 讨论(0)
提交回复
热议问题