2

単体テストでパターン マッチングを実行しようとしています。

私のソースコードには次のものがあります

MY_CODE = "The input %s is invalid"

def my_func():
    check_something()
    return MY_CODE % some_var

私のテストでは、このようなものがあります

def test_checking(self):
    m = app.my_func()
    # How do I assert that m is of the format MY_CODE???
    # assertTrue(MY_CODE in m) wont work because the error code has been formatted

上記を主張する最良の方法を教えてください。

4

1 に答える 1

3

そのためには正規表現を使用する必要があるようです:

assertTrue(re.match("The input .* is invalid", m))

、などに変換することにより、フォーマット文字列を正規表現に変換することが%sできます。.*%d\d

pattern = MY_CODE.replace('%s', '.*').replace(...)

(ただし、この単純なケースではstartswith、 andendswithを使用できます。)

実際には、それをテストする必要はないと思いますが。

于 2013-02-20T15:27:28.853 に答える