Python CLI program unit testing

后端 未结 9 2212
日久生厌
日久生厌 2020-12-24 12:43

I am working on a python Command-Line-Interface program, and I find it boring when doing testings, for example, here is the help information of the program:

         


        
9条回答
  •  梦谈多话
    2020-12-24 13:33

    This isn't for Python specifically, but what I do to test command-line scripts is to run them with various predetermined inputs and options and store the correct output in a file. Then, to test them when I make changes, I simply run the new script and pipe the output into diff correct_output -. If the files are the same, it outputs nothing. If they're different, it shows you where. This will only work if you are on Linux or OS X; on Windows, you will have to get MSYS.

    Example:

    python mycliprogram --someoption "some input" | diff correct_output -

    To make it even easier, you can add all these test runs to your 'make test' Makefile target, which I assume you already have. ;)

    If you are running many of these at once, you could make it a little more obvious where each one ends by adding a fail tag:

    python mycliprogram --someoption "some input" | diff correct_output - || tput setaf 1 && echo "FAILED"

提交回复
热议问题