olexale / bdd_widget_test

A BDD-style widget testing library
MIT License
101 stars 30 forks source link

[Proposal] Put tests in a function so they can be called from `integration_test` #50

Closed lsaudon closed 12 months ago

lsaudon commented 12 months ago

Put tests in a function so they can be called from integration_test

Before

// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: unused_import, directives_ordering

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import './step/common/the_app_is_running.dart';
import './step/i_do_not_see_text.dart';
import 'package:bdd_widget_test/step/i_see_text.dart';
import 'package:bdd_widget_test/step/i_tap_icon.dart';
import './step/i_tap_icon_times.dart';

void main() {
  group('''Counter''', () {
    Future<void> bddSetUp(WidgetTester tester) async {
      await theAppIsRunning(tester);
    }
    Future<void> bddTearDown(WidgetTester tester) async {
      await iDoNotSeeText(tester, 'surprise');
    }
    testWidgets('''Initial counter value is 0''', (tester) async {
      try {
        await bddSetUp(tester);
        await iSeeText(tester, '0');
      } finally {
        await bddTearDown(tester);
      }
    });
    testWidgets('''Add button increments the counter''', (tester) async {
      try {
        await bddSetUp(tester);
        await iTapIcon(tester, Icons.add);
        await iSeeText(tester, '1');
      } finally {
        await bddTearDown(tester);
      }
    });
    testWidgets('''Outline: Plus button increases the counter (0, '0')''', (tester) async {
      try {
        await bddSetUp(tester);
        await theAppIsRunning(tester);
        await iTapIconTimes(tester, Icons.add, 0);
        await iSeeText(tester, '0');
      } finally {
        await bddTearDown(tester);
      }
    });
    testWidgets('''Outline: Plus button increases the counter (1, '1')''', (tester) async {
      try {
        await bddSetUp(tester);
        await theAppIsRunning(tester);
        await iTapIconTimes(tester, Icons.add, 1);
        await iSeeText(tester, '1');
      } finally {
        await bddTearDown(tester);
      }
    });
    testWidgets('''Outline: Plus button increases the counter (42, '42')''', (tester) async {
      try {
        await bddSetUp(tester);
        await theAppIsRunning(tester);
        await iTapIconTimes(tester, Icons.add, 42);
        await iSeeText(tester, '42');
      } finally {
        await bddTearDown(tester);
      }
    });
  });
}

After

// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: unused_import, directives_ordering

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import './step/common/the_app_is_running.dart';
import './step/i_do_not_see_text.dart';
import 'package:bdd_widget_test/step/i_see_text.dart';
import 'package:bdd_widget_test/step/i_tap_icon.dart';
import './step/i_tap_icon_times.dart';

void main() {
  counterFeature();
}

void counterFeature() {
  group('''Counter''', () {
    Future<void> bddSetUp(WidgetTester tester) async {
      await theAppIsRunning(tester);
    }

    Future<void> bddTearDown(WidgetTester tester) async {
      await iDoNotSeeText(tester, 'surprise');
    }

    testWidgets('''Initial counter value is 0''', (tester) async {
      try {
        await bddSetUp(tester);
        await iSeeText(tester, '0');
      } finally {
        await bddTearDown(tester);
      }
    });
    testWidgets('''Add button increments the counter''', (tester) async {
      try {
        await bddSetUp(tester);
        await iTapIcon(tester, Icons.add);
        await iSeeText(tester, '1');
      } finally {
        await bddTearDown(tester);
      }
    });
    testWidgets('''Outline: Plus button increases the counter (0, '0')''',
        (tester) async {
      try {
        await bddSetUp(tester);
        await theAppIsRunning(tester);
        await iTapIconTimes(tester, Icons.add, 0);
        await iSeeText(tester, '0');
      } finally {
        await bddTearDown(tester);
      }
    });
    testWidgets('''Outline: Plus button increases the counter (1, '1')''',
        (tester) async {
      try {
        await bddSetUp(tester);
        await theAppIsRunning(tester);
        await iTapIconTimes(tester, Icons.add, 1);
        await iSeeText(tester, '1');
      } finally {
        await bddTearDown(tester);
      }
    });
    testWidgets('''Outline: Plus button increases the counter (42, '42')''',
        (tester) async {
      try {
        await bddSetUp(tester);
        await theAppIsRunning(tester);
        await iTapIconTimes(tester, Icons.add, 42);
        await iSeeText(tester, '42');
      } finally {
        await bddTearDown(tester);
      }
    });
  });
}

And in integration_test :

import 'package:integration_test/integration_test.dart';

import '../test/sample_test.dart';

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  counterFeature();
}
olexale commented 12 months ago

You may achieve the same with plain old main function too:

import 'package:integration_test/integration_test.dart';

import '../test/sample_test.dart' as counter;

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  counter.main();
}

By the way, you may generate integration tests from bdd_widget_test directly.