Python CLI program unit testing

后端 未结 9 2214
日久生厌
日久生厌 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:17

    Maybe too little too late, but you can always use

    import os.system
    result =  os.system(<'Insert your command with options here'>
    assert(0 == result)
    

    In that way, you can run your program as if it was from command line, and evaluate the exit code.

    (Update after I studied pytest) You can also use capsys. (from running pytest --fixtures)

    capsys Enable text capturing of writes to sys.stdout and sys.stderr.

    The captured output is made available via ``capsys.readouterr()`` method
    calls, which return a ``(out, err)`` namedtuple.
    ``out`` and ``err`` will be ``text`` objects.
    

提交回复
热议问题