Testes na trilha Dart

Aprenda a testar seus exercícios de Dart no Exercism


Baixe um exercício

Use a CLI do Exercism para baixar o exercício em que você quer trabalhar.

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

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

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

Instale as dependências

Antes de rodar os testes, baixe as dependências do exercício.

dart pub get

Rode os testes

dart test

Entendendo os resultados dos testes

O dart test usa o relator compacto por padrão, mostrando em uma única linha a contagem de testes que passaram, foram ignorados e falharam.

$ 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 passou
  • ~9: 9 testes ignorados
  • -1: 1 teste falhou

Para uma saída mais detalhada por teste, use o relator expandido, que mostra o resultado de cada teste à medida que ele roda.

$ 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.

Trabalhando os testes um a um

A maioria dos exercícios tem vários testes, e todos, exceto o primeiro, ficam ignorados por padrão.

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 trabalhar os testes um a um, mude skip: true para skip: false em cada teste conforme você avança. Se preferir, rode todos os testes de uma vez sem editar o arquivo.

dart test --run-skipped

Ao enviar a solução, o test runner online roda todos os testes, inclusive os ignorados.

Lidando com o erro "Could not find a command named frontend_server.dart.snapshot"

Suponha que você tenha um exercício que já resolveu e enviou, e volte a ele depois de um tempo. Você pode encontrar este erro ao testá-lo:

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

A solução é apagar o arquivo pubspec.lock. Depois, rode o comando do Dart de novo.