How to run all my tests at once?

Asked

Viewed 235 times

3

How to run all unit tests at once on the flutter?

Each test has only the option Run as in the image:

inserir a descrição da imagem aqui

So I’d like to know how to run all my tests at once without having to click each one.

2 answers

2


[Edit]

If your tests are grouped in several paths and folders you must leave them inside the directory test of the project and all files must have the suffix _test.dart.

With this configuration done you can go to the terminal and simply apply the command related to the project thus making all the programmed tests run:

Flutter Project

flutter test

Project Dart

pub run test

In addition, there is the possibility that you group your tests together and so run all present there at once.

Example

File: test/widget_test.Dart

import 'package:flutter_test/flutter_test.dart';

void main() {
  group('Counter', () {
    test('value should start at 0', () {
      expect(Counter().value, 0);
    });

    test('value should be incremented', () {
      final counter = Counter();

      counter.increment();

      expect(counter.value, 1);
    });

    test('value should be decremented', () {
      final counter = Counter();

      counter.decrement();

      expect(counter.value, -1);
    });
  });

  group('Counter 2', () {
    test('value should start at 0', () {
      expect(Counter().value, 0);
    });

    test('value should be incremented', () {
      final counter = Counter();

      counter.increment();

      expect(counter.value, 1);
    });

    test('value should be decremented', () {
      final counter = Counter();

      counter.decrement();

      expect(counter.value, -1);
    });
  });
}

class Counter {
  int value = 0;

  void increment() => value++;

  void decrement() => value--;
}

To run the tests you can do the following ways:

  • Click on Run of the desired group;
  • Click on the tab Debug of VSCode and start the process there (Obs: only Vscode is used for development);
  • Go to the terminal and run the command flutter test test/widget_test.dart ;

For more details see the documentation:

https://flutter.dev/docs/cookbook/testing/unit/introduction

https://pub.dev/packages/test#running-tests

  • It can even resolve to single file but in my case are several separate files in several folders.

  • 1

    @rubStackOverflow I edited the answer adding the scenario described in your comment. If I understand correctly what you are looking for I think it will solve your problem.

0

An alternative for those who prefer to view in the visual studio test tab

  • Edit the file launch.json and add:

    { "name": "All Tests", "type": "dart", "request": "launch", "program": "test/", }

  • Select All Tests, to run the test Crtl+F5

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.