我在一个类中有一个try / except子句,当我运行代码时,它会正确执行以解决错误,但是当我在pycharm中运行doctest时,它会失败,并在控制台中看到相同的输出。因此,例如:
import numpy as np
import sys
class Area:
def __init__(self, array):
self.array = np.array(array)
self.shape = self.array.shape
try:
self.col_len = self.shape[1]
except (ValueError, IndexError):
print("Subarrays must be the same length")
sys.exit(42)
是课程。当我在main中运行一个简单的测试时,
a = [[1,1,1,1], [1,1,1], [2,2,2,2]]
print(a)
我得到:
子数组的长度必须相同
以退出代码42结束的过程
当我将相同的代码插入doctest时,我失败了,消息是:
错误
File "/home/joshua/PycharmProjects/Test/Earth/Crust.py", line 12, in Crust.Area
Failed example:
a1 = Area(a)
Exception raised:
Traceback (most recent call last):
File "/home/joshua/PycharmProjects/Test/Earth/Crust.py", line 55, in __init__
self.col_len = self.shape[1]
IndexError: tuple index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/snap/pycharm-professional/107/helpers/pycharm/docrunner.py", line 140, in __run
compileflags, 1), test.globs)
File "<doctest Crust.Area[1]>", line 1, in <module>
a1 = Area(a)
File "/home/joshua/PycharmProjects/Test/Earth/Crust.py", line 58, in __init__
sys.exit(42)
SystemExit: 42
我尝试将上面的整个错误消息放入doctest中,以查看是否可行,但仍然失败。通常情况下,我希望它说出“ Expla blah,got bleh”之类的字眼,但是它甚至没有这样做。我正在使用Anaconda python 3。