Creating Pseudo Terminals for Test Scripts with tmux
I recently tried to automate a test setup, where the program I tried to automate insisted on having a tty. Oh no, yet another tty adventure (previous one) for me ;).
The app doesn’t really interact with a tty, except listening for Ctrl-C. Without a tty, the app just shut itself down. So, I needed a way to create a TTY in my test script.
I searched for the internet, and couldn’t find a good utility for it. Either the utilities seem very obscure, or you needed to create a python script or something else.
Luckily, at some point I found this Unix Stackexchange. The solution: Use tmux to run the program with a pseudo terminal. I would have never thought of that. I use tmux once in a while, so tmux is for me was an interactive tool. I would not have though it as a tool for scripting. Here is how you do it:
# Create a new tmux session tmux new-session -s 'app-under-test-session' -d -x 140 -y 35 # Create a new tmux windows, where the app is running tmux new-window -t 'app-under-test-session' -n 'app-window' -d 'app-to-launch.sh > app-log.log' # Do the test with the app. # Bonus, stopping the tmux session cleans up sub processes etc as well tmux kill-session -t 'app-under-test-session'
Added bonus, you can inspect the scripted session with tmux attach
,
like any other tmux session:
tmux attach -t 'app-under-test-session'
Anyway, tmux allowed me to create a pseudo tty for programs which insist on having a pseudo tty.