Scripting testing with pexpect

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Post Reply
scudderfish
Posts: 24
Joined: Sun Oct 04, 2015 9:06 am

Scripting testing with pexpect

Post by scudderfish » Sat Nov 14, 2015 12:33 pm

I'm editing code on my Mac, and pushing the edits to the Wipy using sitecopy. It was getting a little tedious reloading my new code and testing it via the REPL so I scripted it using pexpect (https://github.com/pexpect/pexpect)

The only tricky bit was figuring out I needed to append '\r' to all commands sent, but not to the user/pwd.

This could be _much_ prettier, but it should give the idea. I save a file, run this script on my box and it sends it over and runs the tests remotely putting the REPL output back into my console :-

Code: Select all

gedge:ulcd dgs$ cat test.py
#!/usr/bin/env python
import pexpect,sys
pexpect.run('sitecopy -u wipy')
a=pexpect.spawn('telnet 192.168.1.1')
a.logfile=sys.stdout
a.expect('Login as:')
a.sendline('micro')
a.expect('assword:')
a.sendline('python')
a.expect('>>>')
a.sendcontrol('d')
a.expect('>>>')
a.sendline('import ulcd\r')
a.expect('>>>')
a.sendline('ulcd.lcd(debugLCD=True).test()\r')
a.expect('>>>')
gedge:ulcd dgs$ ./test.py
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
MicroPython v1.5-94-gae58035 on 2015-11-07; WiPy with CC3200
Login as: micro
micro
Password: python

Login succeeded!
Type "help()" for more information.

>>> 
PYB: soft reboot
MicroPython v1.5-94-gae58035 on 2015-11-07; WiPy with CC3200
Type "help()" for more information.
>>> import ulcd
import ulcd
>>> ulcd.lcd(debugLCD=True).test()
ulcd.lcd(debugLCD=True).test()
begin(2,16)
display
clear
backlight
setBacklight(255)
backlightPin(3,0)
setBacklight(1)
home
write(Hello World!)
>>> gedge:ulcd dgs$ 
I hope this idea helps someone.

Regards,
David

nmz787
Posts: 29
Joined: Sun Jul 10, 2016 7:57 am

Re: Scripting testing with pexpect

Post by nmz787 » Mon Oct 30, 2017 10:27 am

Thanks!

I just used this along with a Python TK example for line plots... here's the result if anyone is interested (code is subject to being moved, as we cleanup the repo in the future... it should be findable in the commit history in the far-future anyway):
https://github.com/nmz787/culture_shock ... adc_gui.py

User avatar
pythoncoder
Posts: 3247
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Scripting testing with pexpect

Post by pythoncoder » Tue Oct 31, 2017 5:39 am

@scudderfish You might be interested in pyboard.py in the micropython/tools directory which allows remote execution on the Pyboard. This is used to run automated testing (tests directory).
Peter Hinch

Post Reply