testing interactive python programs

后端 未结 3 2318
抹茶落季
抹茶落季 2021-02-20 18:21

I would like to know which testing tools for python support the testing of interactive programs. For example, I have an application launched by:

$ python dummy_p         


        
3条回答
  •  死守一世寂寞
    2021-02-20 18:47

    A good example might be the file test_embed.py of the IPython package.

    There two different approaches are used:

    subprocess

    import subprocess
    # ...
    subprocess.Popen(cmd, env=env, stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = p.communicate(_exit_cmd_string)
    

    pexpect (as already mentioned by Brian Oakley

    import pexpect
    # ...
    child = pexpect.spawn(sys.executable, ['-m', 'IPython', '--colors=nocolor'],
                              env=env)
    # ...
    child.sendline("some_command")
    child.expect(ipy_prompt)
    

提交回复
热议问题