1

ファイルが見つからない場合に IOError を予期する doctest があります。

>>> configParser('conffig.ini') # should not exist
Traceback (most recent call last):
    ...
IOError: No such file: /homes/ndeklein/workspace/MS/PyMS/conffig.ini

ただし、これを別の PC からテストしたい場合、または他の人がテストしたい場合、パスは /homes/ndeklein/workspace/MS/PyMS/ にはなりません。やりたい

>>> configParser('conffig.ini') # should not exist
Traceback (most recent call last):
    ...
IOError: No such file: os.path.abspath(conffig.ini)

しかし、docstring にあるため、結果の一部として os.path.abspath( が表示されます。

docstring テスト変数の結果を作成するにはどうすればよいですか?

4

1 に答える 1

2

実際にパス名と照合する必要がありますか? そうでない場合は、省略記号を使用して出力のその部分をスキップします。

>>> configParser('conffig.ini') # should not exist
Traceback (most recent call last):
    ...
IOError: No such file: ...

その場合は、エラーをキャッチして値を手動でテストする必要があります。何かのようなもの:

>>> try:
...   configParser('conffig.ini') # should not exist
... except IOError as e:
...   print('ok' if str(e).endswith(os.path.abspath('conffig.ini')) else 'fail')
ok
于 2012-03-05T14:06:32.347 に答える