Package: unittest-extensions
unittest-extensionsExtension of Python's standard unittest libraryIntroductionIf testing is not easy, you will not do it.If you do not test, bad things will happen.Thus, if testing is not easy, bad things will happen.This minimal library aims to simplify behavioural testing with Python's standardunittestlibrary by separating
object and data creation from behaviour assertion. Furthermore, it is intended to serve users that want to write really small test functions where what is being asserted is quickly comprehended and easily visible.unittest-extensionsdoes not have any dependencies, it is solely based on the
Python standard library and mainly inspired by Ruby'sRSpecframework.DocumentationThis project is documented at:https://maxcode123.github.io/unittest-extensions/Installationpip install unittest-extensionsUsageSuppose you have some code that looks like this:fromdataclassesimportdataclass@dataclassclassUser:name:strsurname:strdefis_relative_to(self,user:"User")->bool:returnself.surname.casefold()==user.surname.casefold()This is a dummy example, meaning that how exactly the User and their methods are implemented does not really matter; what we actually care about here is how to test this code given the above implementation.Say we'd like to test theis_relative_tomethod with pairs of User names and surnames using the standardunittestlibrary.
So, here's an example of how we could do that as simply as possible:unittestfromunittestimportmain,TestCaseclassTestIsRelativeToSameName(TestCase):deftest_same_name(self):user1=User("Niklas","Strindberg")user2=User("Niklas","Ibsen")self.assertFalse(user1.is_relative_to(user2))deftest_same_empty_name(self):user1=User("","Stringberg")user2=User("","Ibsen")self.assertFalse(user1.is_relative_to(user2))classTestIsRelativeToSameSurname(TestCase):deftest_same_surname(self):user1=User("August","Nietzsche")user2=User("Henrik","Nietzsche")self.assertTrue(user1.is_relative_to(user2))deftest_same_empty_surname(self):user1=User("August","")user2=User("Henrik","")self.assertTrue(user1.is_relative_to(user2))deftest_same_surname_case_sensitive(self):user1=User("August","NiEtZsChE")user2=User("Henrik","nietzsche")self.assertTrue(user1.is_relative_to(user2))deftest_surname1_contains_surname2(self):user1=User("August","Solzenietzsche")user2=User("Henrik","Nietzsche")self.assertFalse(user1.is_relative_to(user2))if__name__=="__main__":main()The cases we check here are rather limited but still there is some duplication in our code; we use many lines to create our User subjects. Of course we can avoid that
by creating custom assertion methods that receive only the parameters that change
between tests, but that's why a testing library is here for.Here's how we could write the above code withunittest-extensions:unittest-extensionsfromunittestimportmainfromunittest_extensionsimportTestCase,argsclassTestIsRelativeToSameName(TestCase):defsubject(self,name1,name2):returnUser(name1,"Strindberg").is_relative_to(User(name2,"Ibsen"))@args({"name1":"Niklas","name2":"Niklas"})deftest_same_name(self):self.assertResultFalse()@args({"name1":"","name2":""})deftest_same_empty_name(self):self.assertResultFalse()classTestIsRelativeToSameSurname(TestCase):defsubject(self,surname1,surname2):returnUser("August",surname1).is_relative_to(User("Henrik",surname2))@args({"surname1":"Nietzsche","surname2":"Nietzsche"})deftest_same_surname(self):self.assertResultTrue()@args({"surname1":"","surname2":""})deftest_same_empty_surname(self):self.assertResultTrue()@args({"surname1":"NiEtZsChE","surname2":"Nietzsche"})deftest_same_surname_case_sensitive(self):self.assertResultTrue()@args({"surname1":"Nietzsche","surname2":"Solszenietzsche"})deftest_surname2_contains_surname1(self):self.assertResultFalse()if__name__=="__main__":main()The number of lines is still the same, but the testing code has become clearer:The subject of our assertions in each test case is documented in thesubjectmethodEach test method contains only information we care about, i.e. the input data (names/surnames) we test and the result of the assertion (true/false).LicenseMIT License