2016-06-16 7 views
0

にとValueErrorを主張するために、私は次の関数私は不正な形式のタイムスタンプをテストする方法をどのようpytest

from datetime import datetime 

def validate_timestamp(timestamp): 

"""Confirm that the passed in string is in the proper format %Y%m%d_%H""" 

try: 
    ts = datetime.strptime(str(timestamp),'%Y%m%d_%H') 
except: 
    raise ValueError("{0} must be in the format `%Y%m%d_%H".format(timestamp)) 
return datetime.strftime(ts,'%Y%m%d_%H') 

ためpytestのユニットテストを書いていますか?これを書くための最高のユニットテストは何でしょうか?

+0

[PyTestで例外が発生することを正しくアサートする方法は?](http://stackoverflow.com/questions/23337471/how-to-properly-assert-that-exception-raises-in-pytest) –

答えて

1

ランのようwithブロックで例外を発生させることが期待されるコード:詳細とオプションについて

with pytest.raises(ValueError): 
    # code 

https://pytest.org/latest/assert.htmlをお読みください。

関連する問題