Unit test in Dart e Flutter con test
Da quello che ho capito, test è considerato il package ufficiale per Dart e Flutter per lo unit test.
Infatti nel mio progetto era già installato.
In caso contrario:
PER DART
dart pub add test --dev
PER FLUTTER
flutter pub add test --dev
Vediamo qualche esempio, cominciando da una classica funzione somma:
import 'package:test/test.dart';
void main() async {
test('test trim', () {
expect(testSomma(10, 3), 13);
expect(testSomma(-1, 1), 0);
expect(testSomma(10, 3), 11);
});
}
int testSomma(int a, int b) {
return a + b;
}
Possiamo anche operare sulle stringhe:
import 'package:test/test.dart';
void main() async {
test('test trim', () {
var parola = " ciao!";
expect(testTrim(parola), equals("ciao!"));
});
}
String testTrim(String parola) {
return parola.trim();
}
Volendo possiamo raggruppare i test:
import 'package:test/test.dart';
void main() async {
group("test stringhe", () {
test('test trim', () {
var parola = " ciao!";
expect(testTrim(parola), equals("ciao!"));
});
test('test split', () {
var str = "uno|due|tre";
expect(testSplit(str), equals(["uno", "due", "tre"]));
});
});
}
String testTrim(String parola) {
return parola.trim();
}
List testSplit(String str) {
return str.split(",");
}
Enjoy!
dart flutter test expect
Commentami!