Skip to content

Commit 194c740

Browse files
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.
1 parent 4c562de commit 194c740

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

tests/unittest.sh

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ testdir="$mydir"
66
scriptdir=$(readlink -f "$mydir"/../scripts/)
77
fs="$mydir"/../internal_filesystem/
88
onetest="$1"
9+
ondevice="$2"
910

1011

1112
# print os and set binary
@@ -26,21 +27,39 @@ one_test() {
2627
file="$1"
2728
pushd "$fs"
2829
echo "Testing $file"
29-
"$binary" -X heapsize=8M -c "import sys ; sys.path.append('lib')
30+
if [ -z "$ondevice" ]; then
31+
"$binary" -X heapsize=8M -c "import sys ; sys.path.append('lib')
3032
$(cat $file)
3133
result = unittest.main() ; sys.exit(0 if result.wasSuccessful() else 1) "
32-
result=$?
34+
result=$?
35+
else
36+
cleanname=$(echo "$file" | sed "s#/#_#g")
37+
testlog=/tmp/"$cleanname".log
38+
mpremote.py exec "import sys ; sys.path.append('lib')
39+
$(cat $file)
40+
result = unittest.main()
41+
if result.wasSuccessful():
42+
print('TEST WAS A SUCCESS')
43+
else:
44+
print('TEST WAS A FAILURE')
45+
" > "$testlog"
46+
grep "TEST WAS A SUCCESS" "$testlog"
47+
result=$?
48+
fi
3349
popd
3450
return "$result"
3551
}
3652

3753
failed=0
3854

3955
if [ -z "$onetest" ]; then
40-
echo "Usage: $0 [one_test_to_run.py]"
56+
echo "Usage: $0 [one_test_to_run.py] [ondevice]"
4157
echo "Example: $0 tests/simple.py"
58+
echo "Example: $0 tests/simple.py ondevice"
59+
echo
60+
echo "If no test is specified: run all tests from $testdir on local machine."
4261
echo
43-
echo "If no test is specified: run all tests from $testdir"
62+
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."
4463
while read file; do
4564
one_test "$file"
4665
result=$?

0 commit comments

Comments
 (0)