Aprende a testar os teus exercícios de Dart no Exercism
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
Antes de correres os testes, descarrega as dependências do exercício.
dart pub get
dart test
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 falhadoPara 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.
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.
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.