Use specific TestCase assertions throughout tests

e.g. assert x == y -> self.assertEqual(x, y);
self.assertTrue(isinstance(x, y)) -> self.assertIsInstance(x, y)

These specific methods give more useful errors in the case of a test
failure.
wip-fakessh-exit-status
Alex Willmer 7 years ago committed by David Wilson
parent df9556d1d9
commit e8e023ce59

@ -39,7 +39,7 @@ class CallFunctionTest(testlib.RouterMixin, testlib.TestCase):
self.local = self.router.local() self.local = self.router.local()
def test_succeeds(self): def test_succeeds(self):
assert 3 == self.local.call(function_that_adds_numbers, 1, 2) self.assertEqual(3, self.local.call(function_that_adds_numbers, 1, 2))
def test_crashes(self): def test_crashes(self):
exc = self.assertRaises(mitogen.core.CallError, exc = self.assertRaises(mitogen.core.CallError,
@ -47,13 +47,13 @@ class CallFunctionTest(testlib.RouterMixin, testlib.TestCase):
s = str(exc) s = str(exc)
etype, _, s = s.partition(': ') etype, _, s = s.partition(': ')
assert etype == 'exceptions.ValueError' self.assertEqual(etype, 'exceptions.ValueError')
msg, _, s = s.partition('\n') msg, _, s = s.partition('\n')
assert msg == 'exception text' self.assertEqual(msg, 'exception text')
# Traceback # Traceback
assert len(s) > 0 self.assertGreater(len(s), 0)
def test_bad_return_value(self): def test_bad_return_value(self):
exc = self.assertRaises(mitogen.core.StreamError, exc = self.assertRaises(mitogen.core.StreamError,
@ -61,7 +61,7 @@ class CallFunctionTest(testlib.RouterMixin, testlib.TestCase):
self.assertEquals(exc[0], "cannot unpickle '__main__'/'CrazyType'") self.assertEquals(exc[0], "cannot unpickle '__main__'/'CrazyType'")
def test_returns_dead(self): def test_returns_dead(self):
assert mitogen.core._DEAD == self.local.call(func_returns_dead) self.assertEqual(mitogen.core._DEAD, self.local.call(func_returns_dead))
def test_aborted_on_local_context_disconnect(self): def test_aborted_on_local_context_disconnect(self):
stream = self.router._stream_by_id[self.local.context_id] stream = self.router._stream_by_id[self.local.context_id]
@ -83,9 +83,9 @@ class CallFunctionTest(testlib.RouterMixin, testlib.TestCase):
def test_accepts_returns_context(self): def test_accepts_returns_context(self):
context = self.local.call(func_accepts_returns_context, self.local) context = self.local.call(func_accepts_returns_context, self.local)
assert context is not self.local self.assertIsNot(context, self.local)
assert context.context_id == self.local.context_id self.assertEqual(context.context_id, self.local.context_id)
assert context.name == self.local.name self.assertEqual(context.name, self.local.name)
if __name__ == '__main__': if __name__ == '__main__':

@ -9,11 +9,11 @@ class ConstructorTest(testlib.RouterMixin, testlib.TestCase):
# issue 32 # issue 32
l1 = self.router.local() l1 = self.router.local()
chan = mitogen.core.Channel(self.router, l1, 123) chan = mitogen.core.Channel(self.router, l1, 123)
assert chan.router == self.router self.assertEqual(chan.router, self.router)
assert chan.context == l1 self.assertEqual(chan.context, l1)
assert chan.dst_handle == 123 self.assertEqual(chan.dst_handle, 123)
assert chan.handle is not None self.assertIsNotNone(chan.handle)
assert chan.handle > 0 self.assertGreater(chan.handle, 0)
if __name__ == '__main__': if __name__ == '__main__':

@ -21,9 +21,9 @@ class RsyncTest(testlib.DockerMixin, unittest.TestCase):
testlib.data_path('.'), 'target:/tmp/data' testlib.data_path('.'), 'target:/tmp/data'
]) ])
assert return_code == 0 self.assertEqual(return_code, 0)
assert context.call(os.path.exists, '/tmp/data') self.assertTrue(context.call(os.path.exists, '/tmp/data'))
assert context.call(os.path.exists, '/tmp/data/simple_pkg/a.py') self.assertTrue(context.call(os.path.exists, '/tmp/data/simple_pkg/a.py'))
def test_rsync_between_direct_children(self): def test_rsync_between_direct_children(self):
# master -> SSH -> has-sudo-pubkey -> rsync(.ssh) -> master -> # master -> SSH -> has-sudo-pubkey -> rsync(.ssh) -> master ->
@ -52,9 +52,11 @@ class RsyncTest(testlib.DockerMixin, unittest.TestCase):
'rsync', '--progress', '-vvva', '.ssh/', 'target:' + dest_path 'rsync', '--progress', '-vvva', '.ssh/', 'target:' + dest_path
]) ])
assert return_code == 0 self.assertEqual(return_code, 0)
assert pubkey_acct.call(os.path.getsize, '.ssh/authorized_keys') == \ self.assertEqual(
webapp_acct.call(os.path.getsize, dest_path + '/authorized_keys') pubkey_acct.call(os.path.getsize, '.ssh/authorized_keys'),
webapp_acct.call(os.path.getsize, dest_path + '/authorized_keys'),
)
if __name__ == '__main__': if __name__ == '__main__':

@ -9,7 +9,7 @@ class SlaveTest(testlib.RouterMixin, testlib.TestCase):
def test_slave_allocates_id(self): def test_slave_allocates_id(self):
context = self.router.local() context = self.router.local()
id_ = context.call(id_allocation.allocate_an_id) id_ = context.call(id_allocation.allocate_an_id)
assert id_ == (self.router.id_allocator.next_id - 1) self.assertEqual(id_, self.router.id_allocator.next_id - 1)
if __name__ == '__main__': if __name__ == '__main__':

@ -39,8 +39,8 @@ class LoadModuleTest(ImporterMixin, testlib.TestCase):
def test_module_added_to_sys_modules(self): def test_module_added_to_sys_modules(self):
self.context.send_await.return_value = self.response self.context.send_await.return_value = self.response
mod = self.importer.load_module(self.modname) mod = self.importer.load_module(self.modname)
self.assertTrue(sys.modules[self.modname] is mod) self.assertIs(sys.modules[self.modname], mod)
self.assertTrue(isinstance(mod, types.ModuleType)) self.assertIsInstance(mod, types.ModuleType)
def test_module_file_set(self): def test_module_file_set(self):
self.context.send_await.return_value = self.response self.context.send_await.return_value = self.response
@ -50,12 +50,12 @@ class LoadModuleTest(ImporterMixin, testlib.TestCase):
def test_module_loader_set(self): def test_module_loader_set(self):
self.context.send_await.return_value = self.response self.context.send_await.return_value = self.response
mod = self.importer.load_module(self.modname) mod = self.importer.load_module(self.modname)
self.assertTrue(mod.__loader__ is self.importer) self.assertIs(mod.__loader__, self.importer)
def test_module_package_unset(self): def test_module_package_unset(self):
self.context.send_await.return_value = self.response self.context.send_await.return_value = self.response
mod = self.importer.load_module(self.modname) mod = self.importer.load_module(self.modname)
self.assertTrue(mod.__package__ is None) self.assertIsNone(mod.__package__)
class LoadSubmoduleTest(ImporterMixin, testlib.TestCase): class LoadSubmoduleTest(ImporterMixin, testlib.TestCase):
@ -96,7 +96,7 @@ class LoadModulePackageTest(ImporterMixin, testlib.TestCase):
def test_module_loader_set(self): def test_module_loader_set(self):
self.context.send_await.return_value = self.response self.context.send_await.return_value = self.response
mod = self.importer.load_module(self.modname) mod = self.importer.load_module(self.modname)
self.assertTrue(mod.__loader__ is self.importer) self.assertIs(mod.__loader__, self.importer)
def test_module_path_present(self): def test_module_path_present(self):
self.context.send_await.return_value = self.response self.context.send_await.return_value = self.response
@ -111,7 +111,7 @@ class LoadModulePackageTest(ImporterMixin, testlib.TestCase):
def test_module_data(self): def test_module_data(self):
self.context.send_await.return_value = self.response self.context.send_await.return_value = self.response
mod = self.importer.load_module(self.modname) mod = self.importer.load_module(self.modname)
self.assertTrue(isinstance(mod.func, types.FunctionType)) self.assertIsInstance(mod.func, types.FunctionType)
self.assertEquals(mod.func.__module__, self.modname) self.assertEquals(mod.func.__module__, self.modname)

@ -36,7 +36,7 @@ class IterReadTest(unittest.TestCase):
try: try:
reader = self.func(proc.stdout.fileno()) reader = self.func(proc.stdout.fileno())
for i, chunk in enumerate(reader, 1): for i, chunk in enumerate(reader, 1):
assert i == int(chunk) self.assertEqual(i, int(chunk))
if i > 3: if i > 3:
break break
finally: finally:
@ -52,7 +52,7 @@ class IterReadTest(unittest.TestCase):
got.append(chunk) got.append(chunk)
assert 0, 'TimeoutError not raised' assert 0, 'TimeoutError not raised'
except mitogen.core.TimeoutError: except mitogen.core.TimeoutError:
assert len(got) == 0 self.assertEqual(len(got), 0)
finally: finally:
proc.terminate() proc.terminate()
@ -68,7 +68,8 @@ class IterReadTest(unittest.TestCase):
except mitogen.core.TimeoutError: except mitogen.core.TimeoutError:
# Give a little wiggle room in case of imperfect scheduling. # Give a little wiggle room in case of imperfect scheduling.
# Ideal number should be 9. # Ideal number should be 9.
assert 3 < len(got) < 5 self.assertLess(3, len(got))
self.assertLess(len(got), 5)
finally: finally:
proc.terminate() proc.terminate()

@ -97,12 +97,12 @@ class GetModuleViaSysModulesTest(testlib.TestCase):
# _socket comes from a .so # _socket comes from a .so
import _socket import _socket
tup = self.call('_socket') tup = self.call('_socket')
self.assertEquals(None, tup) self.assertIsNone(tup)
def test_builtin_fails(self): def test_builtin_fails(self):
# sys is built-in # sys is built-in
tup = self.call('sys') tup = self.call('sys')
self.assertEquals(None, tup) self.assertIsNone(tup)
class ResolveRelPathTest(testlib.TestCase): class ResolveRelPathTest(testlib.TestCase):

@ -13,7 +13,7 @@ class NestedTest(testlib.RouterMixin, testlib.TestCase):
context = self.router.local(via=context, name='local%d' % x) context = self.router.local(via=context, name='local%d' % x)
pid = context.call(os.getpid) pid = context.call(os.getpid)
self.assertTrue(isinstance(pid, int)) self.assertIsInstance(pid, int)
if __name__ == '__main__': if __name__ == '__main__':

@ -52,7 +52,7 @@ class BrokenModulesTest(unittest.TestCase):
call = router.route.mock_calls[0] call = router.route.mock_calls[0]
msg, = call[1] msg, = call[1]
self.assertEquals(50, msg.handle) self.assertEquals(50, msg.handle)
self.assertTrue(msg.unpickle() is None) self.assertIsNone(msg.unpickle())
def test_ansible_six_messed_up_path(self): def test_ansible_six_messed_up_path(self):
# The copy of six.py shipped with Ansible appears in a package whose # The copy of six.py shipped with Ansible appears in a package whose
@ -75,7 +75,7 @@ class BrokenModulesTest(unittest.TestCase):
call = router.route.mock_calls[0] call = router.route.mock_calls[0]
msg, = call[1] msg, = call[1]
self.assertEquals(50, msg.handle) self.assertEquals(50, msg.handle)
self.assertTrue(isinstance(msg.unpickle(), tuple)) self.assertIsInstance(msg.unpickle(), tuple)
if __name__ == '__main__': if __name__ == '__main__':

@ -53,7 +53,7 @@ class SshTest(testlib.DockerMixin, unittest.TestCase):
except mitogen.ssh.PasswordError, e: except mitogen.ssh.PasswordError, e:
pass pass
assert e[0] == self.stream_class.password_required_msg self.assertEqual(e[0], self.stream_class.password_required_msg)
def test_password_incorrect(self): def test_password_incorrect(self):
try: try:
@ -65,7 +65,7 @@ class SshTest(testlib.DockerMixin, unittest.TestCase):
except mitogen.ssh.PasswordError, e: except mitogen.ssh.PasswordError, e:
pass pass
assert e[0] == self.stream_class.password_incorrect_msg self.assertEqual(e[0], self.stream_class.password_incorrect_msg)
def test_password_specified(self): def test_password_specified(self):
context = self.docker_ssh( context = self.docker_ssh(
@ -73,8 +73,10 @@ class SshTest(testlib.DockerMixin, unittest.TestCase):
password='y', password='y',
) )
sentinel = 'i-am-mitogen-test-docker-image\n' self.assertEqual(
assert sentinel == context.call(plain_old_module.get_sentinel_value) 'i-am-mitogen-test-docker-image\n',
context.call(plain_old_module.get_sentinel_value),
)
def test_pubkey_required(self): def test_pubkey_required(self):
try: try:
@ -85,15 +87,17 @@ class SshTest(testlib.DockerMixin, unittest.TestCase):
except mitogen.ssh.PasswordError, e: except mitogen.ssh.PasswordError, e:
pass pass
assert e[0] == self.stream_class.password_required_msg self.assertEqual(e[0], self.stream_class.password_required_msg)
def test_pubkey_specified(self): def test_pubkey_specified(self):
context = self.docker_ssh( context = self.docker_ssh(
username='has-sudo-pubkey', username='has-sudo-pubkey',
identity_file=testlib.data_path('docker/has-sudo-pubkey.key'), identity_file=testlib.data_path('docker/has-sudo-pubkey.key'),
) )
sentinel = 'i-am-mitogen-test-docker-image\n' self.assertEqual(
assert sentinel == context.call(plain_old_module.get_sentinel_value) 'i-am-mitogen-test-docker-image\n',
context.call(plain_old_module.get_sentinel_value),
)
if __name__ == '__main__': if __name__ == '__main__':

@ -21,14 +21,14 @@ class RunWithRouterTest(unittest.TestCase):
def test_run_with_broker(self): def test_run_with_broker(self):
router = mitogen.utils.run_with_router(func0) router = mitogen.utils.run_with_router(func0)
self.assertTrue(isinstance(router, mitogen.master.Router)) self.assertIsInstance(router, mitogen.master.Router)
self.assertFalse(router.broker._thread.isAlive()) self.assertFalse(router.broker._thread.isAlive())
class WithRouterTest(unittest.TestCase): class WithRouterTest(unittest.TestCase):
def test_with_broker(self): def test_with_broker(self):
router = func() router = func()
self.assertTrue(isinstance(router, mitogen.master.Router)) self.assertIsInstance(router, mitogen.master.Router)
self.assertFalse(router.broker._thread.isAlive()) self.assertFalse(router.broker._thread.isAlive())

Loading…
Cancel
Save