2

We've built cli-app with Python. Some part need ncurses, so we use npyscreen. We've successfully tested most part of app using pytest (with the help of mock and other things). But we stuck in 'how to test the part of ncurses code'

Take this part of our ncurses code that prompt user to answer:

"""
Generate text user interface:
example :
fields = [
    {"type": "TitleText", "name": "Name", "key": "name"},
    {"type": "TitlePassword", "name": "Password", "key": "password"},
    {"type": "TitleSelectOne", "name": "Role",
    "key": "role", "values": ["admin", "user"]},
]
form = form_generator("Form Foo", fields)
print(form["role"].value[0])
print(form["name"].value)
"""


def form_generator(form_title, fields):
    def myFunction(*args):
    form = npyscreen.Form(name=form_title)
    result = {}
    for field in fields:
        t = field["type"]
        k = field["key"]
        del field["type"]
        del field["key"]

        result[k] = form.add(getattr(npyscreen, t), **field)
    form.edit()
    return result

    return npyscreen.wrapper_basic(myFunction)

We have tried many ways, but failed:

  • stringIO to capture the output: failed
  • redirect the output to file: failed
  • hecate: failed
    • I think it's only work if we run whole program
  • pyautogui
    • I think it's only work if we run whole program

This is the complete steps of what I have tried

So the last thing I use is to use patch. I patch those functions. But the cons is the statements inside those functions are remain untested. Cause it just assert the hard-coded return value.

I find npyscreen docs for writing test. But I don't completely understand. There is just one example.

Thank you in advance.

azzamsa
  • 1,805
  • 2
  • 20
  • 28

1 Answers1

1

I don't see it mentioned in the python docs, but you can use the screen-dump feature of the curses library to capture information for analysis.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Oh no. I don't believe this. I got answer directly from you. what a day. Thanks lot. I am asking this, because I don't find any way to test my ncurses app. If I get an answer, I am planning to write about it and share it in talks as a topic. Cause It's hard to find tutorial to test ncurses app (cmiiw). What's your opinion, do you test all your part of your app (I have most of part tested but ncurses) ? I hear some people [don't](https://youtu.be/a-BOSpxYJ9M?t=1178) – azzamsa Sep 17 '18 at 08:42
  • I generally use applications with logging, comparing logs to do analysis. – Thomas Dickey Sep 17 '18 at 09:06
  • Thank you. maybe I need to read more about [code coverage](https://stackoverflow.com/questions/90002/what-is-a-reasonable-code-coverage-for-unit-tests-and-why). Thanks lot for answer. I will give screen-dump a try. – azzamsa Sep 17 '18 at 10:02