Testing Python Scripts

后端 未结 5 1464
小蘑菇
小蘑菇 2021-02-14 01:23

How do I test the STDOUT output of a Python script with a testing framework like doctest, unittest, nose, etc? For example, say running my script "todo.py --list" shou

5条回答
  •  面向向阳花
    2021-02-14 02:00

    Python's own test suite does this quite a bit, and we use two main techniques:

    1. Redirecting stdout (as others have suggested). We use a context manager for this:

      import io
      import sys
      import contextlib
      
      @contextlib.contextmanager
      def captured_output(stream_name):
          """Run the 'with' statement body using a StringIO object in place of a
             specific attribute on the sys module.
             Example use (with 'stream_name=stdout'):
      
             with captured_stdout() as s:
                 print("hello")
                 assert s.getvalue() == "hello"
          """
          orig_stdout = getattr(sys, stream_name)
          setattr(sys, stream_name, io.StringIO())
          try:
              yield getattr(sys, stream_name)
          finally:
              setattr(sys, stream_name, orig_stdout)
      
      def captured_stdout():
          return captured_output("stdout")
      
      def captured_stderr():
          return captured_output("stderr")
      
      def captured_stdin():
          return captured_output("stdin")
      
    2. Using the subprocess module. We use this when we specifically want to test handling of command line arguments. See http://hg.python.org/cpython/file/default/Lib/test/test_cmd_line_script.py for several examples.

提交回复
热议问题