Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Extension of Python's standard unittest library

License

NotificationsYou must be signed in to change notification settings

Maxcode123/unittest-extensions

Repository files navigation

imageimageimageActions status


Documentation:https://maxcode123.github.io/unittest-extensions/
Source code:https://github.com/Maxcode123/unittest-extensions
PyPI:https://pypi.org/project/unittest-extensions/


unittest-extensions

Extension of Python's standard unittest library

Introduction

If 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 standardunittest library by separatingobject 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-extensions does not have any dependencies, it is solely based on thePython standard library and mainly inspired by Ruby'sRSpec framework.

Installation

pip install unittest-extensions

Usage

Suppose 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_to method with pairs of User names and surnames using the standardunittest library.So, here's an example of how we could do that as simply as possible:

std unittest

fromunittestimportmain,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 thatby creating custom assertion methods that receive only the parameters that changebetween tests, but that's why a testing library is here for.
Here's how we could write the above code withunittest-extensions:

unittest-extensions

fromunittestimportmainfromunittest_extensionsimportTestCase,argsclassTestIsRelativeToSameName(TestCase):defsubject(self,name1,name2):returnUser(name1,"Strindberg").is_relative_to(User(name2,"Ibsen"))@args("Niklas","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("Nietzsche","Nietzsche")deftest_same_surname(self):self.assertResultTrue()@args(surname1="",surname2="")deftest_same_empty_surname(self):self.assertResultTrue()@args("NiEtZsChE",surname2="Nietzsche")deftest_same_surname_case_sensitive(self):self.assertResultTrue()@args("Nietzsche","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:

  1. The subject of our assertions in each test case is documented in thesubject method
  2. Each 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).

[8]ページ先頭

©2009-2025 Movatter.jp