Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a custom `.assertFoo()` method in Python?

I'm writing some test cases for my application using Python's unittest. Now I need to compare a list of objects with a list of another objects to check if the objects from the first list are what I'm expecting.

How can I write a custom .assertFoo() method? What should it do? Should it raise an exception on failure? If yes, which exception? And how to pass the error message? Should the error message be a unicode string or a bytestring?

Unfortunately, the official documentation doesn't explain how to write custom assertion methods.

If you need a real-world example for this, continue reading.


The code I'm writing is somewhat like this:

def assert_object_list(self, objs, expected):     for index, (item, values) in enumerate(zip(objs, expected)):         self.assertEqual(             item.foo, values[0],             'Item {0}: {1} != {2}'.format(index, item.foo, values[0])         )         self.assertEqual(             item.bar, values[1],             'Item {0}: {1} != {2}'.format(index, item.bar, values[1])         )  def test_foobar(self):     objs = [...]  # Some processing here     expected = [         # Expected values for ".foo" and ".bar" for each object         (1, 'something'),         (2, 'nothing'),     ]     self.assert_object_list(objs, expected) 

This approach makes it extremely easy to describe the expected values of each object in a very compact way, and without needing to actually create full objects.

However... When one object fails the assertion, no further objects are compared, and this makes debugging a bit more difficult. I would like to write a custom method that would unconditionally compare all objects, and then would display all objects that failed, instead of just the first one.

like image 346
Denilson Sá Maia Avatar asked Jul 11 '11 19:07

Denilson Sá Maia


People also ask

How do you write an assert statement?

The Syntax of the assert Statement. An assert statement consists of the assert keyword, the expression or condition to test, and an optional message. The condition is supposed to always be true. If the assertion condition is true, then nothing happens, and your program continues its normal execution.

How do you assert false in Python?

assertFalse() in Python is a unittest library function that is used in unit testing to compare test value with false. This function will take two parameters as input and return a boolean value depending upon the assert condition. If test value is false then assertFalse() will return true else return false.


1 Answers

I use the multiple inheritance in these cases. For example:

First. I define a class with methods that will incorporate.

import os  class CustomAssertions:     def assertFileExists(self, path):         if not os.path.lexists(path):             raise AssertionError('File not exists in path "' + path + '".') 

Now I define a class that inherits from unittest.TestCase and CustomAssertion

import unittest  class MyTest(unittest.TestCase, CustomAssertions):     def test_file_exists(self):         self.assertFileExists('any/file/path')  if __name__ == '__main__':     unittest.main() 
like image 128
Alan Cristhian Avatar answered Sep 28 '22 08:09

Alan Cristhian