Tuesday, 15 January 2013

python - Check if the string conforms to a certain format -



python - Check if the string conforms to a certain format -

i trying pattern matching in unit test.

my source code has following

my_code = "the input %s invalid" def my_func(): check_something() homecoming my_code % some_var

in test have this

def test_checking(self): m = app.my_func() # how assert m of format my_code??? # asserttrue(my_code in m) wont work because error code has been formatted

i best way of asserting above?

looks you'd have utilize regex that:

asserttrue(re.match("the input .* invalid", m))

you seek convert format string regex translating %s .*, %d \d , on:

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

(in simple case can utilize startswith , endswith, though.)

although don't think should test @ all.

python string unit-testing python-2.7 string-formatting

No comments:

Post a Comment