def setUp(self): """Create a downloader and disable interactive features as we cannot test them in an automated environment.""" self.downloader = BatchDownloader("download", default_overwrite = True, default_create_directory = True)
class TestBatchDownloader(unittest.TestCase): """ Test class for the BatchDownloader class. Define a set of test cases to check handling of invalid URL lists, invalid URLs, incorrect URLs and valid testcases. Attributes: downloader (object): A BatchDownloader object that is initialized for all test cases. Interactive user requests are disabled. """ def setUp(self): """Create a downloader and disable interactive features as we cannot test them in an automated environment.""" self.downloader = BatchDownloader("download", default_overwrite = True, default_create_directory = True) def test_empty_list(self): """Test an empty list. The code should throw an exception in this case.""" try: self.downloader.download(None) # We should not be able to reach this code line assert False except TypeError: # This example should fail with a Type Error assert True def test_invalid_list(self): """Test an invalid list. The code should throw an exception in this case.""" try: self.downloader.download([None]) # We should not be able to reach this code line assert False except TypeError: # This example should fail with a Type Error assert True def test_invalid_link(self): """Test an invalid link. The code should throw an exception if the validators module is present.""" try: # Test importing the validators module. If this fails we cannot execute the test and pass it by default. import validators self.downloader.download([ "https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg", \ "https:///upload.wikimedia.org/wikipedia/en/7/72/Example-serious.jpg", \ "https://upload.wikimedia.org/wikipedia/commons/2/29/Example_image_not_be_used_in_article_namespace.jpg"]) # We should not be able to reach this code line assert False except (ImportError, IOError): # This example should fail with an IO Error or an Import Error assert True def test_incorrect_link(self): """Test an incorrect link. The code should throw an exception.""" try: self.downloader.download([ "https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg", \ "https://thislinkdoesnotexist.com/Example-serious.jpg", \ "https://upload.wikimedia.org/wikipedia/commons/2/29/Example_image_not_be_used_in_article_namespace.jpg"]) # We should not be able to reach this code line assert False except IOError: # This example should fail with an IO Error assert True def test_valid(self): """Test a valid list. This method does not check the results but assumes correctness if no exception was raised.""" self.downloader.download([ "https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg", \ "https://upload.wikimedia.org/wikipedia/en/7/72/Example-serious.jpg", \ "https://upload.wikimedia.org/wikipedia/commons/2/29/Example_image_not_be_used_in_article_namespace.jpg"]) assert True