我计划测试bi_search函数
def bi_search(L: List[int], find: int) -> int:
if len(L) < 1: #terninating case
return None
else:
mid = len(L) // 2
logging.info(f"mid: {mid}")
if find == L[mid]:
return find
if find > L[mid]:
upper_half = L[mid+1:]
logging.info(f"upper_half: {upper_half}")
return bi_search(upper_half, find)
if find < L[mid]:
lower_half = L[:mid-1]
logging.info(f"lower_half: {lower_half}")
return bi_search(lower_half, find)
还有TestCase
class TestCase(unittest.TestCase):
def test_bi_search(self):
L = list(range(-100,101,3 ))
find = random.randint(-100, 101)
logging.info(f"find: {find}")
result = bi_search(L, find)
print(f"\nThe result is {result}.")
for i in range(30):
unittest.main()
使用for loop
将测试重复30次。
不幸的是,它运行一次并停止了
In [58]: !python threeSum.py
INFO find: -96
INFO mid: 33
INFO lower_half: [-100, -97, -94, -91, -88, -85, -82, -79, -76, -73, -70, -67, -64, -61, -58, -55, -52, -49, -46, -43, -40, -37, -34, -31, -28, -25, -22, -19, -16, -13, -10, -7]
INFO mid: 16
INFO lower_half: [-100, -97, -94, -91, -88, -85, -82, -79, -76, -73, -70, -67, -64, -61, -58]
INFO mid: 7
INFO lower_half: [-100, -97, -94, -91, -88, -85]
INFO mid: 3
INFO lower_half: [-100, -97]
INFO mid: 1
INFO upper_half: []
The result is None.
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
如何为TestCase指定运行时间,以便我可以按预期运行多次。