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
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()
You should create your own TestCase class, derived from unittest.TestCase. Then put your custom assert method into that test case class. If your test fails, raise an AssertionError. Your message should be a string. If you want to test all objects in the list rather than stop on a failure, then collect a list of failing indexes, and after the loop over all the objects, build an assert message that summarizes your findings.
Just an example to sum up with a numpy comparaison unittest
import numpy as np
class CustomTestCase(unittest.TestCase):
def npAssertAlmostEqual(self, first, second, rtol=1e-06, atol=1e-08):
np.testing.assert_allclose(first, second, rtol=rtol, atol=atol)
class TestVector(CustomTestCase):
def testFunction(self):
vx = np.random.rand(size)
vy = np.random.rand(size)
self.npAssertAlmostEqual(vx, vy)