相关疑难解决方法(0)

使用模拟库在循环中对用户输入进行 Python 测试

我正在尝试使用模拟库来测试一段代码。在此代码中,用户原始输入在 for 循环中被接受,如下所示。我已经编写了test_apple_record可以为托盘编号提供单个用户输入值的测试用例。

但是,对于 for 循环中的每次迭代,它只采用与预期相同的值 (5)。

问题是:如何为每次迭代提供不同的值?例如,对于 i=0、1 和 2,托盘编号的具体值分别为 5、6 和 7。

class SomeClass(unittest.TestCase):    
    def apple_counter(self):
        apple_record = {}
        for i in range(3):
            apple_tray = input("enter tray number:")
            apple_record[apple_tray]  =  (i+1)*10
            print("i=%d, apple_record=%s"%(i, apple_record))

    def test_apple_record(self):
        with mock.patch('builtins.input', return_value='5'):
            self.apple_counter()
Run Code Online (Sandbox Code Playgroud)

python unit-testing mocking

6
推荐指数
1
解决办法
2940
查看次数

标签 统计

mocking ×1

python ×1

unit-testing ×1