Testar no percurso de Dart

Aprende a testar os teus exercícios de Dart no Exercism


Descarregar um exercício

Usa a CLI do Exercism para descarregar o exercício em que queres trabalhar.

exercism download --track=dart --exercise=hello-world

Depois, entra no diretório do exercício.

cd /path/to/exercism/dart/hello-world

Instalar as dependências

Antes de correres os testes, descarrega as dependências do exercício.

dart pub get

Correr os testes

dart test

Compreender os resultados dos testes

O dart test usa por omissão o relatório compacto, que mostra numa única linha a contagem de testes passados, ignorados e falhados à medida que avançam.

$ dart test
00:00 +1 -1: test/binary_search_test.dart: finds a value in the middle of an array [E]
  Expected: <3>
    Actual: <0>

  package:matcher                    expect
  test/binary_search_test.dart 13:5  main.<fn>


To run this test again: dart test test/binary_search_test.dart -p vm --plain-name 'finds a value in the middle of an array'
00:00 +1 ~9 -1: Some tests failed.
  • +1: 1 teste passado
  • ~9: 9 testes ignorados
  • -1: 1 teste falhado

Para veres resultados mais detalhados de cada teste, usa o relatório expandido, que mostra o resultado de cada teste à medida que corre.

$ dart test -r expanded
00:00 +0: loading test/binary_search_test.dart
00:00 +0: test/binary_search_test.dart: finds a value in an array with one element
00:00 +1: test/binary_search_test.dart: finds a value in the middle of an array
00:00 +1 -1: test/binary_search_test.dart: finds a value in the middle of an array [E]
  Expected: <3>
    Actual: <0>
  
  package:matcher                    expect
  test/binary_search_test.dart 13:5  main.<fn>
  
00:00 +1 -1: test/binary_search_test.dart: finds a value at the beginning of an array
00:00 +1 ~1 -1: test/binary_search_test.dart: finds a value at the end of an array
00:00 +1 ~2 -1: test/binary_search_test.dart: finds a value in an array of odd length
00:00 +1 ~3 -1: test/binary_search_test.dart: finds a value in an array of even length
00:00 +1 ~4 -1: test/binary_search_test.dart: identifies that a value is not included in the array
00:00 +1 ~5 -1: test/binary_search_test.dart: a value smaller than the array's smallest value is not found
00:00 +1 ~6 -1: test/binary_search_test.dart: a value larger than the array's largest value is not found
00:00 +1 ~7 -1: test/binary_search_test.dart: nothing is found in an empty array
00:00 +1 ~8 -1: test/binary_search_test.dart: nothing is found when the left and right bounds cross
00:00 +1 ~9 -1: Some tests failed.

Trabalhar os testes um a um

A maioria dos exercícios tem vários testes e, por omissão, todos menos o primeiro estão ignorados.

void main() {
  final twoFer = TwoFer();

  group('TwoFer', () {
    test('no name given', () {
      final result = twoFer.statement();
      expect(result, equals('One for you, one for me.'));
    }, skip: false);

    test('a name given', () {
      final result = twoFer.statement('Alice');
      expect(result, equals('One for Alice, one for me.'));
    }, skip: true);

    test('another name given', () {
      final result = twoFer.statement('Bob');
      expect(result, equals('One for Bob, one for me.'));
    }, skip: true);
  });
}

Para trabalhares os testes um a um, muda skip: true para skip: false em cada teste à medida que avanças. Em alternativa, corre todos os testes de uma vez sem editares o ficheiro.

dart test --run-skipped

Ao submeteres, todos os testes são executados pelo test runner online, incluindo os ignorados.

Como resolver o erro "Could not find a command named frontend_server.dart.snapshot"

Imagina que tens um exercício que já resolveste e submeteste e ao qual voltas passado algum tempo. Podes encontrar este erro quando o testas:

$ dart test
...
Could not find a command named "/path/to/frontend_server.dart.snapshot".

A solução é apagar o ficheiro pubspec.lock. Depois, corre outra vez o comando do dart.