質問

I have written a Python module and I'm using doctest to test it out. I have the tests embedded within the module itself and I'm calling doctest with

if __name__ == '__main__':
    import doctest
    doctest.testmod()

All of the tests pass (or fail) exactly I expect them to. The only problem with this approach is that as the number of test cases increases it becomes difficult to follow the code. I've read that doctest will allow you to have tests in a separate file so I am attempting to do that. I find that tests which work wonderfully inside my module are failing when I put them in another file.

Here is an example test file.

>>> from modbusServer import ModbusServer
>>> s = ModbusServer('/dev/ttyUSB0')
>>> s.server # doctest: +ELLIPSIS
<modbus_tk.modbus_rtu.RtuServer instance at 0x...>

Here is what happens when I run that test

test@testpc ~/code/newmodbus $ python -m doctest test.txt 
**********************************************************************
File "test.txt", line 3, in test.txt
Failed example:
    s.server # doctest: +ELLIPSIS
Expected:
    <modbus_tk.modbus_rtu.RtuServer instance at 0x...>
Got:
    <modbus_tk.modbus_rtu.RtuServer instance at 0xa37adec>

This test works just fine when I call doctest from my module yet it fails now. Any ideas as to what needs to change in my test file?

役に立ちましたか?

解決

This is not an answer yet, but it will look ugly in comments. Following works for me, can you check it in your env:

(test)alko@work:~$ cd /tmp
(test)alko@work:/tmp$ cat test.txt
>>> from collections import deque
>>> deque().__init__ # doctest: +ELLIPSIS
<method-wrapper '__init__' of collections.deque object at 0x...>
(test)alko@work:/tmp$ python -m doctest test.txt
(test)alko@work:/tmp$

Update based on comments

As all is ok with this code for you, so your doctest module and ELLIPSIS directive are ok. And as you mentioned that files originate from windows, it is clear, that problem lies in lines endings. Doctest tries to match 0xa37adec>\r\n with expression 0x...>\n consumes with ... variable a37adec part, just prior to > sign, and fails on carriage return character.

You might want to run fromdos utility for all your windows-originated files.

Alternatively you can (and I advise to do so) use git to manage your development, and it will gladly replace line-endings for you.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top