Enjoy learning new things • Started coding in Python since three years ago... • http://www.plurk.com/API/ • Python (with mox), also include • Perl, PHP, JS, Go in these years. 13年9月15日星期日
and ... • Code: • if os.path.exists(folder) and \ os.path.isdir(folder): foo = bar • Mock via @decorator: • @patch('os.path.exists', Mock(return_value=True)) @patch('os.path.isdir', Mock(return_value=True)) def test_create_subfolders(self): ... • Mock via context manager: • with patch('os.path.exists', Mock(return_value=True)) as m: os.path.exists('foo.txt') • with patch('os.path.isdir', return_value=True) as mm: os.path.isdir('bar') 13年9月15日星期日
reaches external resources • Expensive • test code which costs lots of time/ components to execute (or setup) • Exception • test code which raises side effects 13年9月15日星期日
Lots of requests and Json responses Lots of exceptions including {403 Rate Limit Exceeded, 401 Invalid Access Token, 412 Precondition Failed, 500 Internal Server Error, ...} Simulate disk i/o event for specified folder and reflect to remote storage, i.e. Google Drive. 13年9月15日星期日
...] >>> with patch('__builtin__.open') as m: m.return_value = MagicMock() enter = m.return_value.__enter__ enter.return_value.__iter__.return_value = content with open('a', 'r') as f: for line in f: print line 13年9月15日星期日
...]) >>> with patch('__builtin__.open') as m: m.return_value = MagicMock() m.return_value.__enter__.return_value = strIO with open('a', 'r') as f: for line in f: print line f.seek(0) for line in f: print line 13年9月15日星期日
patch # mock the retry decorator before any module loads it patch('gdapi.utils.retry', lambda x, y, delay: lambda z: z).start() from gdapi.apirequest import APIRequest from gdapi.errors import GoogleApiError import requests class Test_cred_functions(unittest.TestCase): ... 13年9月15日星期日