Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: manually build a unittest/doctest object. Date: Wed, 9 Dec 2015 01:27:10 +1100 Lines: 28 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de POmifLaBE6z0l0m4Ju45TQ017PQ4CFnRWmgxnACRqpQQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:build': 0.07; 'cc:addr:python-list': 0.09; 'none.': 0.09; 'sys.stdout': 0.09; 'output': 0.13; 'def': 0.13; 'wed,': 0.15; "'hello": 0.16; '(modulo': 0.16; 'contextlib': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'string:': 0.16; 'subject:unittest': 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'work,': 0.21; 'function,': 0.22; 'am,': 0.23; 'dec': 0.23; 'tried': 0.24; 'import': 0.24; 'header :In-Reply-To:1': 0.24; 'example': 0.26; 'error': 0.27; 'message- id:@mail.gmail.com': 0.27; "i'm": 0.30; 'subject:/': 0.30; 'wrap': 0.33; 'received:google.com': 0.35; 'skip:c 30': 0.35; 'could': 0.35; 'something': 0.35; 'received:209.85': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'sure': 0.39; 'skip:e 20': 0.39; 'forget': 0.60; 'your': 0.60; 'capture': 0.66; 'chrisa': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=oPyfLNr9Z9cud5Kp6EUrkgwozAlsG6oPHCzfmszhdZY=; b=Rjm0SfOxudw+DcIuJ3eiuLjPH/qdjo0gx311DYpular6+gP1auxj7Gec4Taq8ZmLKe MwWqadaF8ec2s6zQDQpqc24P8eKOu92lypdVaKbm4aNMOiZlkZlPjhQwylzxyPVv6KbF mc6VB7/YahUHAp0OXOTKzGyVVntvxe7XbNDVW29fNHd+olOCkah34CE+MRFH3IioGwTQ pbf43tSLTC/NXr/NJ+JJPKSG93WwxH8xu4xODZt/ANY6FQu0aE9xNB+fBlW/5KX4pixf Pas9L2g9X36/iExeUzM+6U3SgzqrEX+uzlvgN7gh7l2cd+JtYBsHAA07sG4vZ3m2qZY+ aatw== X-Received: by 10.50.61.164 with SMTP id q4mr3491687igr.13.1449584830686; Tue, 08 Dec 2015 06:27:10 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100158 On Wed, Dec 9, 2015 at 1:04 AM, Vincent Davis wrote: > I also tried something like: > assert exec("""print('hello word')""") == 'hello word' I'm pretty sure exec() always returns None. If you want this to work, you would need to capture sys.stdout into a string: import io import contextlib output = io.StringIO() with contextlib.redirect_stdout(output): exec("""print("Hello, world!")""") assert output.getvalue() == "Hello, world!\n" # don't forget the \n You could wrap this up into a function, if you like. Then your example would work (modulo the \n): def capture_output(code): """Execute 'code' and return its stdout""" output = io.StringIO() with contextlib.redirect_stdout(output): exec(code) return output.getvalue() assert capture_output("""print('hello word')""") == 'hello word\n' # no error ChrisA