jarus / flask-testing

Unittest extensions for Flask
http://pythonhosted.org/Flask-Testing/
Other
502 stars 111 forks source link

trouble capturing stdout in test #123

Open aedenj opened 6 years ago

aedenj commented 6 years ago

I'm attempting to write some tests around telemetry of a flask app using pytest asserting on the contents of stdout. Suppose I have this app,

imports ...

app = Flask(__name__)

@app.route("/")
def hello():
    return "hello"

@app.before_first_request
def app_start():
    app.logger.addHandler(logging.StreamHandler(sys.stdout))
    app.logger.setLevel(logging.INFO)

@app.after_request
def after_request(response):
   app.logger.info('signal')

and this test

imports ...

__flask__ = app.test_client()
__flask__.testing = True

def test_endpoint_produces_signal(capsys):
    __flask__.get('/')
    out, err = capsys.readouterr()
    assert out == 'signal'

Unfortunately this test fails. However, if I move the logging configuration and info call all to after_request is works. Thanks for your help.