したがって、デコレーターの順序が重要であり、これらのどれも機能しないことがわかっています。
@it.should('do something')
@mock.patch('datetime.date')
@mock.patch('datetime.time')
def test_1(date, time, case):
pass
@mock.patch('datetime.time')
@mock.patch('datetime.date')
@it.should('do something')
def test_2(case, date, time):
pass
patch
方法のためにshould
実装されています。どちらのライブラリも、結果として装飾された関数がどうなるかについていくつかの仮定を行うため、装飾の結果を一方から他方に直接渡すことはできません。
しかし、「アダプター」デコレーターを使用して外部から修正することができます。
import mock
import nose2.tools
def fix_case(f):
def test(case):
f(case=case)
return test
with nose2.tools.such.A('system') as it:
@it.should('do something')
@fix_case
@mock.patch('datetime.time')
@mock.patch('datetime.date')
def test_3(date, time, case=None):
print(date, time, case)
it.createTests(globals())
これは機能し、結果は次のようになります。
$ nose2 -v
test 0000: should do something (tests.test_nose2_such.A system) ...
(<MagicMock name='date' id='4334752592'>, <MagicMock name='time' id='4334762000'>,
<tests.test_nose2_such.A system testMethod=test 0000: should do something>)
ok
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
これはかなり迅速で汚れていますが、仕事は完了です。これを改善して、より良いものにすることができるかどうかを確認します。