を使用して関数のテストを作成したい関数がたくさんあるpythonファイルを想定していますdoctest
。たとえば、すべての関数は文字列と接続オブジェクト ( httplib.HTTPConnection(...)
) を取ります。したがって、文字列が空またはNone
. テストは次のようになります。
def function_1(mystring, conn):
r'''
>>> conn = httplib.HTTPConnection(...)
>>> function_1(None, conn)
Traceback (most recent call last):
NoneAsInputError: `mystring` should be a string and not `None`!
>>> function_1("", conn)
Traceback (most recent call last):
EmptyStringError: `mystring` should not be an empty string!
'''
pass
def function_2(mystring, conn):
r'''
>>> conn = httplib.HTTPConnection(...)
>>> function_2(None, conn)
Traceback (most recent call last):
NoneAsInputError: `mystring` should be a string and not `None`!
>>> function_2("", conn)
Traceback (most recent call last):
EmptyStringError: `mystring` should not be an empty string!
'''
pass
[...]
def function_n(mystring, conn):
r'''
>>> conn = httplib.HTTPConnection(...)
>>> function_n(None, conn)
Traceback (most recent call last):
NoneAsInputError: `mystring` should be a string and not `None`!
>>> function_n("", conn)
Traceback (most recent call last):
EmptyStringError: `mystring` should not be an empty string!
'''
pass
ご覧のとおり、テストは同じで、関数名のみが変更されています。コードの繰り返しを避けるためにそれをリファクタリングすることは可能ですか?
または、そのようなテストをまとめるためのより良い方法はありますか?