diff --git a/tests/clonetest.py b/tests/clonetest.py index 167b822c..350866bb 100644 --- a/tests/clonetest.py +++ b/tests/clonetest.py @@ -148,13 +148,9 @@ class TestClone(unittest.TestCase): self._clone("skip", disks=disks, skip_list=skip_list) def testCloneFullPool(self): - try: + with self.assertRaises(Exception): self._clone("fullpool", disks=["/full-pool/test.img"], compare=False) - except Exception: - return - - raise AssertionError("Expected exception, but none raised.") def testCloneNvramAuto(self): self._clone("nvram-auto") diff --git a/tests/test_urls.py b/tests/test_urls.py index c91e6590..32ee10b9 100644 --- a/tests/test_urls.py +++ b/tests/test_urls.py @@ -196,20 +196,16 @@ class URLTests(unittest.TestCase): def test001BadURL(self): badurl = "http://aksdkakskdfa-idontexist.com/foo/tree" - try: + with self.assertRaises(ValueError) as cm: installer = Installer(hvmguest.conn, location=badurl) installer.detect_distro(hvmguest) - raise AssertionError("Expected URL failure") - except ValueError as e: - self.assertTrue("maybe you mistyped" in str(e)) + self.assertTrue("maybe you mistyped" in str(cm.exception)) # Non-existent cdrom fails - try: + with self.assertRaises(ValueError) as cm: installer = Installer(hvmguest.conn, cdrom="/not/exist/foobar") self.assertEqual(None, installer.detect_distro(hvmguest)) - raise AssertionError("Expected cdrom failure") - except ValueError as e: - self.assertTrue("non-existent path" in str(e)) + self.assertTrue("non-existent path" in str(cm.exception)) # Ensure existing but non-distro file doesn't error installer = Installer(hvmguest.conn, cdrom="/dev/null") diff --git a/tests/xmlparse.py b/tests/xmlparse.py index b7e73734..ba2b9aa2 100644 --- a/tests/xmlparse.py +++ b/tests/xmlparse.py @@ -1445,44 +1445,29 @@ class XMLParseTest(unittest.TestCase): self.assertEqual(dev.managed, "test1") self.assertEqual(dev.rom_bar, "test2") - try: + with self.assertRaises(ValueError): dev.scsi_bus = "goodbye" - raise AssertionError("Expected ValueError") - except ValueError: - pass def testXMLCoverage(self): - try: + with self.assertRaises(RuntimeError) as cm: # Ensure we validate root element virtinst.DeviceDisk(self.conn, parsexml="") - raise AssertionError("Expected parse failure") - except RuntimeError as e: - self.assertTrue("'foo'" in str(e)) + self.assertTrue("'foo'" in str(cm.exception)) - try: + with self.assertRaises(Exception) as cm: # Ensure we validate root element virtinst.DeviceDisk(self.conn, parsexml=-1) - raise AssertionError("Expected parse failure") - except Exception as e: - self.assertTrue("xmlParseDoc" in str(e)) + self.assertTrue("xmlParseDoc" in str(cm.exception)) - try: + with self.assertRaises(RuntimeError): from virtinst import xmlutil xmlutil.raise_programming_error(True, "for coverage") - raise AssertionError("We shouldn't get here") - except RuntimeError: - pass - try: + with self.assertRaises(ValueError): virtinst.DeviceDisk.validate_generic_name("objtype", None) - raise AssertionError("We shouldn't get here") - except ValueError: - pass - try: + + with self.assertRaises(ValueError): virtinst.DeviceDisk.validate_generic_name("objtype", "foo bar") - raise AssertionError("We shouldn't get here") - except ValueError: - pass # Test property __repr__ for code coverage assert str(virtinst.DeviceDisk.address) @@ -1532,8 +1517,6 @@ class XMLParseTest(unittest.TestCase): disk.set_backend_for_existing_path() self.assertEqual(bool(getattr(disk, "_storage_backend")), True) - try: + + with self.assertRaises(ValueError): disk.validate() - raise AssertionError("expected disk validate failure") - except ValueError: - pass