python: tests: use more targeted assert*() functions/checks

- use assertIsInstance, assertNotEqual, and assertIsNotNone as more
  specific checks (will produce better logging)
- use assertRaises when expecting exceptions being thrown
- when testing internal_test_rhashtable, instead of checking type and
  elements of the return values just check the return value as a whole
  (easier and already getting all the work needed by unittest)
This commit is contained in:
Pino Toscano
2016-02-12 17:57:58 +01:00
parent 6629cb0d91
commit 2a2a540436
6 changed files with 8 additions and 22 deletions

View File

@@ -34,13 +34,13 @@ class Test080Version (unittest.TestCase):
self.assertEqual (self.version['major'], 1)
def test_minor (self):
self.assertTrue (isinstance (self.version['minor'], cl))
self.assertIsInstance (self.version['minor'], cl)
def test_release (self):
self.assertTrue (isinstance (self.version['release'], cl))
self.assertIsInstance (self.version['release'], cl)
def test_extra (self):
self.assertTrue (isinstance (self.version['extra'], str))
self.assertIsInstance (self.version['extra'], str)
if __name__ == '__main__':
unittest.main ()

View File

@@ -51,7 +51,7 @@ class Test420LogMessages (unittest.TestCase):
g.close ()
self.assertFalse (log_invoked == 0)
self.assertNotEqual (log_invoked, 0)
if __name__ == '__main__':
unittest.main ()

View File

@@ -37,11 +37,7 @@ class Test800ExplicitClose (unittest.TestCase):
# Expect an exception if we call a method on a closed handle.
g = guestfs.GuestFS (python_return_dict=True)
g.close ()
try:
g.set_memsize (512)
raise Exception("expected an exception from previous statement")
except guestfs.ClosedHandle:
pass
self.assertRaises (guestfs.ClosedHandle, g.set_memsize, 512)
del g
# Verify that the handle is really being closed by g.close, by

View File

@@ -31,10 +31,7 @@ class Test810RHBZ811650 (unittest.TestCase):
# Because error() wasn't being called, guestfs_last_error
# would return NULL, causing a segfault in the Python bindings
# (RHBZ#811650).
try:
g.launch ()
except:
pass
self.assertRaises (RuntimeError, g.launch)
def tearDown (self):
os.unlink ("rhbz811650.img")

View File

@@ -89,7 +89,7 @@ class Test820RHBZ912499 (unittest.TestCase):
def test_rhbz912499 (self):
dom = conn.createXML (self.xml,
libvirt.VIR_DOMAIN_START_AUTODESTROY)
self.assertFalse (dom == None)
self.assertIsNotNone (dom)
print ("temporary domain %s is running" % self.domname)

View File

@@ -27,7 +27,6 @@ class Test900PythonDict (unittest.TestCase):
g = guestfs.GuestFS (python_return_dict=False)
r = g.internal_test_rhashtable ("5")
self.assertTrue (isinstance (r, list))
self.assertEqual (r, [ ("0","0"), ("1","1"), ("2","2"),
("3","3"), ("4","4") ])
@@ -35,13 +34,7 @@ class Test900PythonDict (unittest.TestCase):
g = guestfs.GuestFS (python_return_dict=True)
r = g.internal_test_rhashtable ("5")
self.assertTrue (isinstance (r, dict))
self.assertEqual (sorted (r.keys()), ["0","1","2","3","4"])
self.assertEqual (r["0"], "0")
self.assertEqual (r["1"], "1")
self.assertEqual (r["2"], "2")
self.assertEqual (r["3"], "3")
self.assertEqual (r["4"], "4")
self.assertEqual (r, {"0": "0", "1": "1", "2": "2", "3": "3", "4": "4"})
if __name__ == '__main__':
unittest.main ()