在TestCase中设置重复次数

时间:2019-03-21 01:57:14

标签: python-3.x

我打算测试这样的程序

from typing import List
import random 
import unittest   
class Solution():
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        # nums_d:{value:index}
        nums_d = {nums[i]:i for i in range(len(nums))}
        for i in range(len(nums)):
            find = target - nums[i]
            print('find: ', find)
            nums_d.pop(nums[i]) #Avoid use an element twice
            j = nums_d.get(find)#hash table to search 
            print('j: ',j)
            if j: 
                return [i, j]
        return None  

尝试使用随机输入进行详尽的测试

class TestCase(unittest.TestCase):
    """
    Test for 'twoSum.py'
    """

    def test_two_sum(self):
        #random is present
        target = random.randrange(200,300)
        nums = list(range(150))
        nums = random.sample(nums, k=100)
        solution = Solution()
        print(solution.twoSum(nums, target))

unittest.main()

运行

In [22]: !python Algorithms/twoSum.py                                                                     
find:  214
j:  None
find:  234
j:  None
find:  237
j:  None
find:  229
j:  None
find:  153
j:  None
find:  232
j:  None
find:  217
j:  None
find:  215
j:  None
find:  128
j:  10
[8, 10]
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

不幸的是,它只运行一次,然后我必须手动按向上箭头才能重复。

如何设置跑步时间?

0 个答案:

没有答案