10

I am on a learning stage of using python as a tool for software QA.

I wrote the next simple test in order to find the letter 'a' in a text file number matrix. problem is that the test fails even though the expect equals to what i got.

Why is that? Can you tell me what am I 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's 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.
skaffman
  • 398,947
  • 96
  • 818
  • 769
user734349
  • 101
  • 1
  • 3

2 Answers2

14

You've got an extra space after the 97 - if you remove it, your test should run fine.

Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107
2

This:

return;

Makes your function return None.

Did you mean return t?


Besides that, IMHO doctest tests are meant to be self-contained. This is something the user should see in your documentation and understand without context. In your example, you're using a module-local arr_fin_1 object which is completely opaque to the user. It's better to define it in the doctest before the find_letter call to provide a self-contained example.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • 1
    Actually in this case, either "print t" or "return t" seems to make the doctest pass, as long as you remove the space @Frank Schmitt mentioned. Having both "print t" and "return t" makes the doctest _fail_ since it gets 97 _twice_. – Mu Mind May 02 '11 at 13:42
  • But I agree it's a strange usage of doctests (as I understand it, you should think of doctests as documentation you can check, not as QA on your software). They should be tightly self-contained and not depend on outside context. – Mu Mind May 02 '11 at 13:55