Capture and check stdout/stderr during cli tests

This commit is contained in:
sneakypete81 2013-06-29 11:19:02 +01:00
parent b22a04f071
commit f083503b7a

View file

@ -1,5 +1,6 @@
import os import os
import sys import sys
from StringIO import StringIO
import mock import mock
try: try:
import unittest2 as unittest # Python2.6 import unittest2 as unittest # Python2.6
@ -19,19 +20,22 @@ class TestCli(unittest.TestCase):
TEST_FILE = os.path.join("tests", "unit", "data", "test_file.txt") TEST_FILE = os.path.join("tests", "unit", "data", "test_file.txt")
@mock.patch.object(openphoto.main, "OpenPhoto") @mock.patch.object(openphoto.main, "OpenPhoto")
def test_defaults(self, MockOpenPhoto): @mock.patch('sys.stdout', new_callable=StringIO)
def test_defaults(self, _, MockOpenPhoto):
get = MockOpenPhoto.return_value.get get = MockOpenPhoto.return_value.get
main([]) main([])
MockOpenPhoto.assert_called_with(config_file=None) MockOpenPhoto.assert_called_with(config_file=None)
get.assert_called_with("/photos/list.json", process_response=False) get.assert_called_with("/photos/list.json", process_response=False)
@mock.patch.object(openphoto.main, "OpenPhoto") @mock.patch.object(openphoto.main, "OpenPhoto")
def test_config(self, MockOpenPhoto): @mock.patch('sys.stdout', new_callable=StringIO)
def test_config(self, _, MockOpenPhoto):
main(["--config=test"]) main(["--config=test"])
MockOpenPhoto.assert_called_with(config_file="test") MockOpenPhoto.assert_called_with(config_file="test")
@mock.patch.object(openphoto.main, "OpenPhoto") @mock.patch.object(openphoto.main, "OpenPhoto")
def test_get(self, MockOpenPhoto): @mock.patch('sys.stdout', new_callable=StringIO)
def test_get(self, mock_stdout, MockOpenPhoto):
get = MockOpenPhoto.return_value.get get = MockOpenPhoto.return_value.get
get.return_value = "Result" get.return_value = "Result"
main(["-X", "GET", "-h", "test_host", "-e", "test_endpoint", "-F", main(["-X", "GET", "-h", "test_host", "-e", "test_endpoint", "-F",
@ -39,10 +43,11 @@ class TestCli(unittest.TestCase):
MockOpenPhoto.assert_called_with(host="test_host") MockOpenPhoto.assert_called_with(host="test_host")
get.assert_called_with("test_endpoint", field1="1", field2="2", get.assert_called_with("test_endpoint", field1="1", field2="2",
process_response=False) process_response=False)
# TODO: self.assertEq(mock_stdout.getvalue(), "Result") self.assertEqual(mock_stdout.getvalue(), "Result\n")
@mock.patch.object(openphoto.main, "OpenPhoto") @mock.patch.object(openphoto.main, "OpenPhoto")
def test_post(self, MockOpenPhoto): @mock.patch('sys.stdout', new_callable=StringIO)
def test_post(self, mock_stdout, MockOpenPhoto):
post = MockOpenPhoto.return_value.post post = MockOpenPhoto.return_value.post
post.return_value = "Result" post.return_value = "Result"
main(["-X", "POST", "-h", "test_host", "-e", "test_endpoint", "-F", main(["-X", "POST", "-h", "test_host", "-e", "test_endpoint", "-F",
@ -50,10 +55,11 @@ class TestCli(unittest.TestCase):
MockOpenPhoto.assert_called_with(host="test_host") MockOpenPhoto.assert_called_with(host="test_host")
post.assert_called_with("test_endpoint", field1="1", field2="2", files={}, post.assert_called_with("test_endpoint", field1="1", field2="2", files={},
process_response=False) process_response=False)
# TODO: self.assertEq(mock_stdout.getvalue(), "Result") self.assertEqual(mock_stdout.getvalue(), "Result\n")
@mock.patch.object(openphoto.main, "OpenPhoto") @mock.patch.object(openphoto.main, "OpenPhoto")
def test_post_files(self, MockOpenPhoto): @mock.patch('sys.stdout', new_callable=StringIO)
def test_post_files(self, _, MockOpenPhoto):
post = MockOpenPhoto.return_value.post post = MockOpenPhoto.return_value.post
main(["-X", "POST", "-F", "photo=@%s" % self.TEST_FILE]) main(["-X", "POST", "-F", "photo=@%s" % self.TEST_FILE])
# It's not possible to directly compare the file object, so check it manually # It's not possible to directly compare the file object, so check it manually
@ -62,34 +68,39 @@ class TestCli(unittest.TestCase):
self.assertEqual(files["photo"].name, self.TEST_FILE) self.assertEqual(files["photo"].name, self.TEST_FILE)
@mock.patch.object(sys, "exit", raise_exception) @mock.patch.object(sys, "exit", raise_exception)
def test_unknown_arg(self): @mock.patch('sys.stderr', new_callable=StringIO)
def test_unknown_arg(self, mock_stderr):
with self.assertRaises(TestException): with self.assertRaises(TestException):
main(["hello"]) main(["hello"])
# TODO: self.assertIn(mock_stdout.getvalue(), "Error: Unknown argument") self.assertIn("error: Unknown argument", mock_stderr.getvalue())
@mock.patch.object(sys, "exit", raise_exception) @mock.patch.object(sys, "exit", raise_exception)
def test_unknown_option(self): @mock.patch('sys.stderr', new_callable=StringIO)
def test_unknown_option(self, mock_stderr):
with self.assertRaises(TestException): with self.assertRaises(TestException):
main(["--hello"]) main(["--hello"])
# TODO: self.assertIn(mock_stdout.getvalue(), "Error: no such option") self.assertIn("error: no such option", mock_stderr.getvalue())
@mock.patch.object(sys, "exit", raise_exception) @mock.patch.object(sys, "exit", raise_exception)
def test_unknown_config(self): @mock.patch('sys.stdout', new_callable=StringIO)
def test_unknown_config(self, mock_stdout):
with self.assertRaises(TestException): with self.assertRaises(TestException):
main(["--config=this_config_doesnt_exist"]) main(["--config=this_config_doesnt_exist"])
# TODO: self.assertIn(mock_stdout.getvalue(), "No such file or directory") self.assertIn("No such file or directory", mock_stdout.getvalue())
# TODO: self.assertIn(mock_stdout.getvalue(), "You must create a configuration file") self.assertIn("You must create a configuration file", mock_stdout.getvalue())
# TODO: self.assertIn(mock_stdout.getvalue(), "To get your credentials") self.assertIn("To get your credentials", mock_stdout.getvalue())
@mock.patch.object(openphoto.main, "OpenPhoto") @mock.patch.object(openphoto.main, "OpenPhoto")
def test_verbose(self, _): @mock.patch('sys.stdout', new_callable=StringIO)
def test_verbose(self, mock_stdout, _):
main(["-v"]) main(["-v"])
# TODO: self.assertIn(mock_stdout.getvalue(), "Method: GET") self.assertIn("Method: GET", mock_stdout.getvalue())
# TODO: self.assertIn(mock_stdout.getvalue(), "Endpoint: /photos/list.json") self.assertIn("Endpoint: /photos/list.json", mock_stdout.getvalue())
@mock.patch.object(openphoto.main, "OpenPhoto") @mock.patch.object(openphoto.main, "OpenPhoto")
def test_pretty_print(self, MockOpenPhoto): @mock.patch('sys.stdout', new_callable=StringIO)
def test_pretty_print(self, mock_stdout, MockOpenPhoto):
get = MockOpenPhoto.return_value.get get = MockOpenPhoto.return_value.get
get.return_value = '{"test":1}' get.return_value = '{"test":1}'
main(["-p"]) main(["-p"])
# TODO: self.assertEq(mock_stdout.getvalue(), '{\n "test":1\n}") self.assertEqual(mock_stdout.getvalue(), '{\n "test":1\n}\n')