それが特定の形式であるとアサートしたいresult
場合、戻り値を次のように設定するとします。(True, 'Success')
def batch_move(self, *args, **kwargs):
'''
Move a batch of files to its respective destinations.
Return type: tuple (boolean, message)
T/F , 'string' / str(exception)
'''
srcs= kwargs.get('srcs', None)
dests = kwargs.get('dests', None)
try:
if srcs and dests:
# map srcs and dests into dictionary (srcs --> keys, dests --> values)
src_dest= dict(zip(srcs, dests))
for src, dest in src_dest:
if os.path.exists(src):
if os.path.exists(dest):
shutil.rmtree(dest)
shutil.move(src, dest) # either way we will proceed to move
else:
return (False, '%s does not exist!' % src)
return (True, 'Success!')
else:
return (False, 'Something gone wrong with those kwargs...')
except Exception as e:
return (False, e)
たどり着くためにreturn (True, 'Success!')
- 戻り値としてパッチ
os.path.exists
します。True
しかし、ある単体テストではこれをスキップしたいのですが、どうすればパッチを当てることができos.path.exists
ますか?
if os.path.exists(dest): # I want to skip this shutil.rmtree(dest)
パッチを適用するにはどうすればよい
shutil.move(src, dest)
ですか?True
エラーが発生しないように与えるだけですか?失敗して例外をキャッチしたい場合はどうすればよいですか? それをシミュレートするにはどうすればよいですか?(どの例外をキャッチするべきか、私は常に知っているわけではありません。主な理由は を使用するためException as e
です)。実際に関数を渡すと、例外がキャッチされず、すべての行を通過したことを本当に意味しますか? それとも、`mock_object.return_value = (True, 'Success!')' を設定したからですか?
2 つの依存関係しか使用していません。(os、sys、math、datetime) などのすべての外部依存関係を 1 つにまとめて修正する必要がありますか? または、関数が他の関数 (リファクタリングされた) を使用している場合
def f1(*args, **kwargs): f2(..) # use math, plot, datetime f3(..) # use math and datetime f4(..) # use datetime ....
ありがとう。長い質問で申し訳ありません。私はユニットテストを書くのが本当に上手になりたいです。