私はTDDを初めて使い、テストを書くときに状況に遭遇しました。文字列が有効なintであるかどうかをアサート
マイFUNC:
def nonce():
return str(int(1000 * time.time()))
私はそれのためにテストを書いた - そしてそれは私が何をしたいんしながら、これを処理するunittest
モジュールの中で何か?:
def test_nonce_returns_an_int_as_string(self):
n = 'abc' # I want it to deliberately fail
self.assertIsInstance(n, str)
try:
int(n)
except ValueError:
self.fail("%s is not a stringified integer!" % n)
これをアサートする方法はありますか?try/except
?
この回答はSO postですが、回答ではassert
afaictの使用はありません。
特に気になるのは、私のfailed test
メッセージは、純粋なunittest.TestCase
メソッドを使用するのとは対照的に、あまりにもかわいくてクラッタではないということです。
Failure
Traceback (most recent call last):
File "/git/bitex/tests/client_tests.py", line 39, in test_restapi_nonce
int(n)
ValueError: invalid literal for int() with base 10: 'a'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "//git/bitex/tests/client_tests.py", line 41, in test_restapi_nonce
self.fail("%s is not a stringified integer!" % n)
AssertionError: a is not a stringified integer!
テストをデバッグするためのメッセージがあります。ある例外が別の例外を処理するコンテキストで発生した場合、通常はこれについて知りたいと思う*。 –
* "答えは' assert' afaict "の使用法を提供していません" * - あなたは何を言っているのですか?それらの全ては、例えば、 'self.assertTrue(n.isdigit())'のようなテストに簡単に適応させることができます。 – jonrsharpe
答えに何が書いてあるのか考えるのを止めずに、私はそれを言います。あなたが正しい。シャック。 – nlsdfnbch