unittest.sh: add support for on-device testing

This allows running unit tests on an ESP32 device connected to the USB serial port.
It uses mpremote.py for this.
This commit is contained in:
Thomas Farstrike
2025-11-10 07:57:54 +01:00
parent 4c562deb17
commit 194c740589
+23 -4
View File
@@ -6,6 +6,7 @@ testdir="$mydir"
scriptdir=$(readlink -f "$mydir"/../scripts/)
fs="$mydir"/../internal_filesystem/
onetest="$1"
ondevice="$2"
# print os and set binary
@@ -26,10 +27,25 @@ one_test() {
file="$1"
pushd "$fs"
echo "Testing $file"
"$binary" -X heapsize=8M -c "import sys ; sys.path.append('lib')
if [ -z "$ondevice" ]; then
"$binary" -X heapsize=8M -c "import sys ; sys.path.append('lib')
$(cat $file)
result = unittest.main() ; sys.exit(0 if result.wasSuccessful() else 1) "
result=$?
result=$?
else
cleanname=$(echo "$file" | sed "s#/#_#g")
testlog=/tmp/"$cleanname".log
mpremote.py exec "import sys ; sys.path.append('lib')
$(cat $file)
result = unittest.main()
if result.wasSuccessful():
print('TEST WAS A SUCCESS')
else:
print('TEST WAS A FAILURE')
" > "$testlog"
grep "TEST WAS A SUCCESS" "$testlog"
result=$?
fi
popd
return "$result"
}
@@ -37,10 +53,13 @@ result = unittest.main() ; sys.exit(0 if result.wasSuccessful() else 1) "
failed=0
if [ -z "$onetest" ]; then
echo "Usage: $0 [one_test_to_run.py]"
echo "Usage: $0 [one_test_to_run.py] [ondevice]"
echo "Example: $0 tests/simple.py"
echo "Example: $0 tests/simple.py ondevice"
echo
echo "If no test is specified: run all tests from $testdir"
echo "If no test is specified: run all tests from $testdir on local machine."
echo
echo "The 'ondevice' argument will try to run the test on a connected device using mpremote.py (should be on the PATH) over a serial connection."
while read file; do
one_test "$file"
result=$?