Skip to content

Commit d559a24

Browse files
committed
Use capsys to test tasks
1 parent 5c37f07 commit d559a24

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

appengine/flexible/tasks/main.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
"""App Engine app to serve as an endpoint for App Engine queue samples."""
1616

17-
import logging
18-
1917
from flask import Flask, request
2018

2119
app = Flask(__name__)
@@ -25,8 +23,7 @@
2523
def log_payload():
2624
"""Log the request payload."""
2725
payload = request.get_data(as_text=True) or '(empty payload)'
28-
# Send our test logging message as a "warn" to ensure visibility.
29-
logging.warn('Received task with payload: {}'.format(payload))
26+
print('Received task with payload: {}'.format(payload))
3027
return 'Printed task payload: {}'.format(payload)
3128

3229

appengine/flexible/tasks/main_test.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import mock
1615
import pytest
1716

1817

@@ -28,19 +27,19 @@ def test_index(app):
2827
assert r.status_code == 200
2928

3029

31-
@mock.patch('logging.warn')
32-
def test_log_payload(logging_mock, app):
33-
payload = 'hello'
30+
def test_log_payload(capsys, app):
31+
payload = 'test_payload'
3432

3533
r = app.post('/log_payload', data=payload)
3634
assert r.status_code == 200
3735

38-
assert logging_mock.called
36+
out, _ = capsys.readouterr()
37+
assert payload in out
3938

4039

41-
@mock.patch('logging.warn')
42-
def test_empty_payload(logging_mock, app):
40+
def test_empty_payload(capsys, app):
4341
r = app.post('/log_payload')
4442
assert r.status_code == 200
4543

46-
assert logging_mock.called
44+
out, _ = capsys.readouterr()
45+
assert 'empty payload' in out

0 commit comments

Comments
 (0)