Flutter自动化测试插件flutter_gherkin_automated的使用

Flutter自动化测试插件flutter_gherkin_automated的使用

1. 添加依赖

首先,在项目的 pubspec.yaml 文件中添加以下依赖项:

dev_dependencies:
  integration_test:
  build_runner:
  flutter_gherkin:

2. 配置 build.yaml

在项目根目录下创建一个 build.yaml 文件,并添加以下内容:

targets:
  $default:
    sources:
      - lib/**
      - pubspec.*
      - $package$
      - integration_test/**.dart

3. 创建 integration_test_driver.dart 文件

test_driver 目录下创建 integration_test_driver.dart 文件,并添加以下内容:

import 'package:integration_test/integration_test_driver.dart' as integration_test_driver;

Future<void> main() {
  // The Gherkin report data send back to this runner by the app after
  // the tests have run will be saved to this directory
  integration_test_driver.testOutputsDirectory = 'integration_test/gherkin/reports';

  return integration_test_driver.integrationDriver(
    timeout: Duration(minutes: 90),
  );
}

4. 创建 integration_test 目录

在项目根目录下创建一个 integration_test 目录。

5. 创建 counter.feature 文件

integration_test/features 目录下创建 counter.feature 文件,并添加以下内容:

Feature: Counter
  The counter should be incremented when the button is pressed.

  Scenario: Counter increases when the button is pressed
    Given I expect the "counter" to be "0"
    When I tap the "increment" button 10 times
    Then I expect the "counter" to be "10"

6. 创建 gherkin_suite_test.dart 文件

integration_test 目录下创建 gherkin_suite_test.dart 文件,并添加以下内容:

import 'package:flutter_gherkin_integration/flutter_gherkin_integration_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:gherkin/gherkin.dart';

// The application under test.
import 'package:example_with_integration_test/main.dart' as app;

part 'gherkin_suite_test.g.dart';

@GherkinTestSuite()
void main() {
  executeTestSuite(
    FlutterTestConfiguration.DEFAULT([])
      ..reporters = [
        StdoutReporter(MessageLevel.error)
          ..setWriteLineFn(print)
          ..setWriteFn(print),
        ProgressReporter()
          ..setWriteLineFn(print)
          ..setWriteFn(print),
        TestRunSummaryReporter()
          ..setWriteLineFn(print)
          ..setWriteFn(print),
        JsonReporter(
          writeReport: (_, __) => Future<void>.value(),
        ),
      ],
    (World world) => app.main(),
  );
}

7. 生成测试文件

在命令行中运行以下命令以生成测试文件:

flutter pub run build_runner build

8. 运行测试

在命令行中运行以下命令以运行测试:

flutter drive --driver=test_driver/integration_test_driver.dart --target=integration_test/gherkin_suite_test.dart

9. 调试测试

.vscode/launch.json 文件中添加以下配置以便调试测试:

{
  "name": "Debug integration_test",
  "program": "test_driver/integration_test_driver.dart",
  "cwd": "example_with_integration_test/",
  "request": "launch",
  "type": "dart",
  "args": [
    "--target=integration_test/gherkin_suite_test.dart",
  ],
}

10. 自定义世界类

如果需要自定义世界类,确保它扩展了 FlutterWorld 类。例如:

class MyWorld extends FlutterWorld {
  // Custom world initialization
}

11. 更改功能文件后重新生成测试

如果更改了任何功能文件,需要重新生成测试文件:

flutter pub run build_runner clean
flutter pub run build_runner build

更多关于Flutter自动化测试插件flutter_gherkin_automated的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自动化测试插件flutter_gherkin_automated的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


flutter_gherkin_automated 是一个用于 Flutter 的自动化测试插件,它允许你使用 Gherkin 语法来编写行为驱动开发(BDD)测试。Gherkin 是一种人类可读的语法,通常用于描述软件行为,并且可以与自动化测试工具结合使用。

以下是如何使用 flutter_gherkin_automated 插件的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 flutter_gherkin_automated 依赖:

dev_dependencies:
  flutter_gherkin_automated: ^3.0.0
  flutter_test:
    sdk: flutter

然后运行 flutter pub get 来安装依赖。

2. 编写 Gherkin 文件

Gherkin 文件通常以 .feature 为扩展名。你可以在 test 目录下创建一个 features 文件夹,并在其中创建 .feature 文件。

例如,创建一个 login.feature 文件:

Feature: Login
  As a user
  I want to login to the app
  So that I can access my account

  Scenario: Successful login with valid credentials
    Given I am on the login screen
    When I enter "username" in the username field
    And I enter "password" in the password field
    And I press the login button
    Then I should see the home screen

3. 编写 Step Definitions

Step Definitions 是与 Gherkin 步骤对应的代码实现。你需要在 test 目录下创建一个 steps 文件夹,并在其中创建 Dart 文件来定义这些步骤。

例如,创建一个 login_steps.dart 文件:

import 'package:flutter_gherkin_automated/flutter_gherkin_automated.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

class LoginSteps extends Given {
  @override
  Future<void> executeStep() async {
    // Implement the "Given I am on the login screen" step
    expect(find.text('Login Screen'), findsOneWidget);
  }
}

class EnterUsernameStep extends When {
  @override
  Future<void> executeStep(String username) async {
    // Implement the "When I enter "username" in the username field" step
    await tester.enterText(find.byKey(Key('usernameField')), username);
  }
}

class EnterPasswordStep extends When {
  @override
  Future<void> executeStep(String password) async {
    // Implement the "And I enter "password" in the password field" step
    await tester.enterText(find.byKey(Key('passwordField')), password);
  }
}

class PressLoginButtonStep extends When {
  @override
  Future<void> executeStep() async {
    // Implement the "And I press the login button" step
    await tester.tap(find.byKey(Key('loginButton')));
    await tester.pumpAndSettle();
  }
}

class SeeHomeScreenStep extends Then {
  @override
  Future<void> executeStep() async {
    // Implement the "Then I should see the home screen" step
    expect(find.text('Home Screen'), findsOneWidget);
  }
}

4. 配置测试运行器

test 目录下创建一个 test_runner.dart 文件,并配置 Gherkin 测试运行器:

import 'package:flutter_gherkin_automated/flutter_gherkin_automated.dart';
import 'steps/login_steps.dart';

void main() {
  final config = FlutterTestConfiguration()
    ..features = ['test/features/login.feature']
    ..stepDefinitions = [
      LoginSteps(),
      EnterUsernameStep(),
      EnterPasswordStep(),
      PressLoginButtonStep(),
      SeeHomeScreenStep(),
    ];

  executeTests(config);
}

5. 运行测试

你可以使用以下命令来运行 Gherkin 测试:

flutter test test/test_runner.dart
回到顶部