[test] print debug info when expect tests fail (#5181)

This stores debug logs from expect and prints them if a test fails to
enable easier debugging.
diff --git a/.gitignore b/.gitignore
index 581e43b..3f44e28 100644
--- a/.gitignore
+++ b/.gitignore
@@ -57,3 +57,4 @@
 third_party/nlbuild-autotools/repo/third_party/autoconf/m4/ltversion.m4
 third_party/nlbuild-autotools/repo/third_party/autoconf/m4/lt~obsolete.m4
 third_party/nlbuild-autotools/repo/third_party/autoconf/missing
+/tmp/
diff --git a/script/test b/script/test
index 85ee827..a5b263e 100755
--- a/script/test
+++ b/script/test
@@ -219,19 +219,25 @@
         test_patterns=(-name 'cli-*.exp' -o -name 'simulation-*.exp')
     fi
 
+    local log_file="tmp/log_expect"
     while read -r script; do
         sudo rm -rf tmp
+        mkdir tmp
         {
             if [[ ${OT_NATIVE_IP} == 1 ]]; then
-                OT_COMMAND="${ot_command}" RCP_COMMAND="${rcp_command}" sudo -E "${script}"
+                OT_COMMAND="${ot_command}" RCP_COMMAND="${rcp_command}" sudo -E expect -df "${script}" 2>"${log_file}"
             else
-                OT_COMMAND="${ot_command}" RCP_COMMAND="${rcp_command}" "${script}"
+                OT_COMMAND="${ot_command}" RCP_COMMAND="${rcp_command}" expect -df "${script}" 2>"${log_file}"
             fi
         } || {
             local exit_code=$?
+            cat "${log_file}" >&2
             echo -e "${COLOR_FAIL}FAIL${COLOR_NONE} ${script}"
             exit "${exit_code}"
         }
+        if [[ ${VERBOSE} == 1 ]]; then
+            cat "${log_file}" >&2
+        fi
         echo -e "${COLOR_PASS}PASS${COLOR_NONE} ${script}"
     done < <(
         if [[ $# != 0 ]]; then