LoginSignup
2
1

More than 1 year has passed since last update.

Flutterでテスト実行する際に設定を加えたかったのでdart_test.ymlについて調べた

Last updated at Posted at 2022-05-04

概要

Flutterでテストを実行する際に設定を加えたかったので、dart_test.ymlについて調べました。

dart_test.ymlとは?

テスト実行時の設定を定義できるファイルです。
下記はドキュメントの抜粋です。

Each package may include a configuration file that applies to the package as a whole. This file can be used to provide custom defaults for various options, to define configuration for multiple files, and more.
The file is named dart_test.yaml and lives at the root of the package, next to the package's pubspec. Like the pubspec, it's a YAML file.

設定するにはプロジェクトのrootに、dart_test.yaml という名前のファイルを追加します。

設定例はこんな感じ。

# This package's tests are very slow. Double the default timeout.
timeout: 2x

# This is a browser-only package, so test on chrome by default.
platforms: [chrome]

tags:
  # Integration tests are even slower, so increase the timeout again.
  integration: {timeout: 2x}

  # Sanity tests are quick and verify that nothing is obviously wrong. Declaring
  # the tag allows us to tag tests with it without getting warnings.
  sanity:

設定できる項目は下記のように5つあり、その項目の中に複数のoptionが存在します。

optionの詳細な内容はこの記事では書かないので、ドキュメントから確認いただくのが良いと思います。

  • Test Configuration
  • Runner Configuration
  • Configuring Tags
  • Configuring Platforms
  • Configuration Presets
  • Global Configuration

構成フィールドには、「テスト(Test Configuration)」と「ランナー(Runner Configuration)」の2つの主要なカテゴリーがあります。Test Configuration は個々のテストの実行方法を制御し、Runner Configurationはテストランナー全体を制御します。

TagやPlatgormは定義しておくことで、optionをつけて実行するテストのターゲットを絞り込むことができます。

以下は各項目の要約。

Test Configuration

  • テストの実行方法を制御し、構成ファイルの最上位で使用できます。
  • 後述するタグまたはプラットフォームの構成に対しても使用できます。下記のような感じ。
tags:
chrome:
   skip: "Our Chrome launcher is busted. See issue 1234."

Runner Configuration

  • テストランナー全体を制御し、構成ファイルの最上位でのみ使用できます。

Configuring Tags

  • テストコードにタグフィールドを定義しておくことで、個々のテストにoptionを適用できます。
  • 構成ファイルに定義する際はこんな感じ。
tags:
  # Integration tests need more time to run.
 tag_a:
    timeout: 1m
  • 実際にテストコードにTagを定義する際はこんな感じ。
test('xxxのテスト', () async {
   ....
}, tags: ['tag_a', 'tag_b']);

Configuring Platforms

  • プラットフォーム構成には、on_oson_platform の2種類があり、on_os に対しては、Runner Configurationも行うことができます。
    • 使えるosはこちらで確認できます。
  • override_platformsdefine_platforms を利用することで既存のプラットフォーム構成を拡張したり、独自のプラットフォーム構成を定義することができます。
  • 構成ファイルに定義する際はこんな感じ。
on_os:
  windows:
    # Both of these are the defaults anyway, but let's be explicit about it.
    color: false
    runner: expanded

    # My Windows machine is real slow.
    timeout: 2x

  # My Linux machine has SO MUCH RAM.
  linux:
    concurrency: 500

Configuration Presets

  • プリセットを利用することで設定をまとめて定義しておくことができ、コマンドラインで --preset または -P フラグを使用して選択することができます。
  • 構成ファイルに定義する際はこんな感じ
presets:
  # Use this when you need completely un-munged stack traces.
  debug:
    verbose_trace: false
    js_trace: true

  # Shortcut for running only browser tests.
  browser:
    paths:
    - test/runner/browser
    - test/runner/pub_serve_test.dart

Global Configuration

  • グローバルな構成ファイルも利用することができ、DART_TEST_CONFIG環境変数を使用して明示的に設定することができます。

補足

ドキュメントからだけではoptionのデフォルト設定が確認できないものもあったので、気になった時はコードも合わせてみるのが良さそうです。
https://github.com/dart-lang/test/blob/master/pkgs/test_core/lib/src/runner/configuration/args.dart

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1