Python testing ncurses

1.6k views Asked by At

I'm developing terminal app. And I'm wondering how can one test the terminal user interface made with ncurses. Does any one have some experience with this kind of testing?

  • So far my best shot will be to test app with capturing stdout and compare it with what it should be. But I'm concerned that i could never create the comparable case for every terminal size, text colors codes for (256bit,24bit), etc...

  • One way to test would be to simulate the keyboard but how could I test the visual behaivour?

I am clueless obout this problem...

2

There are 2 answers

1
Thomas Dickey On BEST ANSWER

Comparing screen output is difficult, since ncurses updates only the altered parts of the screen, using cursor-addressing and other methods. Instead, comparing the content of the screen known to ncurses at different points in time is the best approach.

You could construct a screen-dump using instr to get the text (but that omits attributes such as color). The Python interface to ncurses also has inch (but that assumes characters are 8-bits)

It would be nice to use putwin, but that (until recently) saves the window in binary form. Upcoming ncurses6 uses a text dump, which could be diff'd. The change is not visible to callers (since putwin/getwin always have treated the file-format as a secret), and in principle could be turned on in a build of ncurse5. For that, you would probably have to build your own ncurses.

1
clay On

The program you probably want to check out is a TCL script called Expect. It's designed to automate text based interactions.

Per Wikipedia:

[Expect] is used to automate control of interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others. Expect uses pseudo terminals (Unix) or emulates a console (Windows), starts the target program, and then communicates with it, just as a human would, via the terminal or console interface.

I imagine that you could set up an Expect script to run through a standard interaction and report any interaction problems along the way.

I doubt any program can test for aesthetics (i.e. how it looks to the eye, etc...) but you can probably test if the interface fits on the screen and that the various windows don't overlap etc.. by adding a decorator to the PyCurses function calls that tracks the dimensions of the various parts and reports any overlaps or problems.

For more on Python decorators, check out this article: Understanding Python Decorators in 12 Easy Steps!