Okay I am running Chromedriver test that gathers console logs and then tests an assertion against pythons unittest.
class ConsoleLogsTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = get_browser_driver() cls.base = BasePage(cls.driver, BASE_URL) cls.page = MultiplePages(cls.driver, BASE_URL) cls.verificationErrors =  @classmethod def tearDownClass(cls): cls.driver.quit() def test_logs_for_error(self): self.page.navigate() self.base.wait_for_footer() self.verificationErrors = self.page.check_for_severe_console_logs( BASE_URL) try: assert len(self.verificationErrors) == 0 except AssertionError, e: for message in self.verificationErrors: print str(message)
check_for_severe_console_logs returns a list of the entries from
self.driver.get_log('browser') and I'm trying to loop through my list all the errors (which includes the URI and the console error) as a failure to the test instead of the actual exception
# does not equal 0 because that's not very useful. If I run test with nose and include the sdout (e.g.
nosetests -verbosity=1 -s) you it will print out my error message for every log. But if you run it without
nosetests -verbosity=1 the test isn't actually failing it just says "Ok". I tried adding
self.fail after the message loop but that didn't fix it. What am I doing wrong or know a better way to get useful errors?
Update: I figured out one way to do this is to use a custom exception so adding that solution here. But will probably go with @alecxe suggestion
self.assertEqual(self.verificationErrors, ) unless somebody has something better or how to do that without self. I added a custom exception at the top of the file
class ConsoleException(Exception): def __init__(self, messages): self.value = '' for message in messages: self.value += '\r\n' + str(message) def __str__(self): return str(self.value)
Note that I has to use str instead of repr as suggested in the docs (and I'm worried that might cause problems if it finds lots) to get it to format how I wanted. And then I'm calling it like this:
try: assert len(self.verificationErrors) == 0 except AssertionError: raise ConsoleException(self.verificationErrors)
If you want this to fail as an assertion error, you could also just make function . to loop through errors.
def loop_thru_messages(messages): value = '' for message in messages: value += '\r\n' + str(message) return str(value)
Then run a function at the end of your assert.
assert len(self.verificationErrors) == 0, loop_thru_messages(self.verificationErrors)