Python doctest: expected result matches "got" result, but test failed

I am at the stage of studying the use of python as a tool for QA software.

I wrote the following simple test to find the letter "a" in the matrix of text file numbers. the problem is that the test fails, although the wait is the same as what I received.

Why? Can you tell me what I'm doing wrong?

test script:

fin = open("abc.txt", "r") arr_fin = [] for line in fin: arr_fin.append(line.split()) print arr_fin for row in arr_fin: arr_fin_1 = " ".join('{0:4}'.format(i or " ") for i in row) print arr_fin_1 def find_letter(x, arr_fin_1): """ >>> find_letter('a', arr_fin_1) 97 """ t=ord(x) #exchange to letter ASCII value for i in arr_fin_1: if i==x: print t return; def _test(): import doctest doctest.testmod() if __name__ == "__main__": _test() 

error message:

 Expected: 97 Got: 97 ********************************************************************** 1 items had failures: 1 of 1 in __main__.find_letter ***Test Failed*** 1 failures. 
+6
source share
2 answers

You have extra space after 97 - if you delete it, your test should work fine.

+11
source

It:

 return; 

Returns the None function.

Did you mean return t ?


In addition, IMHO doctest tests should be self-sufficient. This is what the user should see in their documentation and understand without context. In your example, you are using the locally local object arr_fin_1 , which is completely opaque to the user. It is better to define it in a doctrine before calling find_letter to provide a standalone example.

+2
source

Source: https://habr.com/ru/post/887114/


All Articles