flutter_test library - Dart API
가장 자주 사용되는 주요 테스트 함수다.
구조는 다음과 같다.
void expect(
dynamic actual, // actual value to be verified
dynamic matcher, // characterises the expected result
{ String? reason, // added to the output in case of failure
dynamic skip, } // true or a String with the reason to skip
) {...}
test('expect: value ✅', () {
final result = 0;
expect(result, 0);
})
//결과
//Expected: <0>
// Actual: <1>
테스트 실패 시 TestFailure
오류가 발생한다.
reason 파라미터를 이용하면 실패 시 추가 로그가 출력되게 설정할 수 있다.
test('expect: reason ❌', () {
final result = 1;
expect(result, 0, reason: 'Result should be 0!');
});
//Expected: <0>
// Actual: <1>
//Result should be 0!
skip 파라미터를 이용하면 결괏값이 넘겨준 값일 경우 skip 로그를 출력하게 설정할 수 있다.
test('expect: skip ✅', () {
final result = 1;
expect(result, 0, skip: true);
expect(result, 0, skip: 'for a reason');
});
//Skip expect: (<0>).
//Skip expect: for a reason
expectLater
expect
함수와 비슷한 기능을 하지만 넘겨준 matcher의 매칭이 완료될 때 까지 비동기적으로 수행하는 Future
를 반환한다.
구조는 다음과 같다.
Future<void> expectLater(
dynamic actual, // actual value to be verified
dynamic matcher, { // characterises the expected result
String? reason, // added to the output in case of failure
dynamic skip, // true or a String with the reason to skip
}) {...}
matcher 인자로 모든 인자를 받을 수는 있지만, 비동기 처리를 수행하는 AsyncMatcher
객체를 넘겨주는 것이 가장 좋다.