Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为带有输入的代码编写单元测试(Python)_Python - Fatal编程技术网

如何为带有输入的代码编写单元测试(Python)

如何为带有输入的代码编写单元测试(Python),python,Python,我是单元测试新手,我必须为问题的解决方案添加一个单元测试类。问题就在这里,我的代码如下。我很难弄清楚如何使用用户输入进行单元测试 T = int(input()) for x in range(T): # Allows us to input and apply method to all iterables in a | no. of tests \n x_energy y_energy z_energy | format. T = number of tests, x is Loh

我是单元测试新手,我必须为问题的解决方案添加一个单元测试类。问题就在这里,我的代码如下。我很难弄清楚如何使用用户输入进行单元测试

T = int(input())

for x in range(T):
    # Allows us to input and apply method to all iterables in a | no. of tests \n x_energy y_energy z_energy | format. T = number of tests, x is Lohia energy, y is Gosu energy and z is Goalkeeper energy
    x, y, z = list(map(int, input().split(" ")))
    Goal_Lohia, Goal_Gosu = 0, 0

    # Session ends when goalkeepers energy decrements to 1
    while z > 1:
        # Goalkeepers energy in this case is a factor of both strikers' energy, so both score.
        if x % z == 0 and y%z == 0:
            x = x - 1
            Goal_Lohia = Goal_Lohia + 1
            y = y - 1
            Goal_Gosu = Goal_Gosu + 1
        # In this case, the goalkeepers energy is only a factor of Lohia's. Note Lohia goes first as the problem states.
        elif x % z == 0:
            x = x - 1
            Goal_Lohia = Goal_Lohia + 1
        # And in this case, it is only a factor of Gosu's
        elif y % z == 0:
            y = y - 1
            Goal_Gosu = Goal_Gosu + 1
        # If goalkeepers energy is a factor of neither Gosu nor Lohia, the goalkeeper will save (no goal scored) and will lose energy.
        else:
            z = z - 1
    print(Goal_Lohia, Goal_Gosu)

非常感谢

您希望重构代码,以便通过从用户获取输入和提供硬编码值来调用代码。然后,您可以在程序正常运行时调用一个版本,在运行测试时调用另一个版本

类似地,您可以分离输出的操作,以便在正常情况下打印输出,但在测试情况下检查其正确性

以下是您的代码的外观:

def function_to_test(T):
    for x in range(T):
        # Allows us to input and apply method to all iterables in a | no. of tests \n x_energy y_energy z_energy | format. T = number of tests, x is Lohia energy, y is Gosu energy and z is Goalkeeper energy
        x, y, z = list(map(int, input().split(" ")))
        Goal_Lohia, Goal_Gosu = 0, 0
        ...
        return Goal_Lohia, Goal_Gosu

def function_that_takes_user__input():
    T = int(input())
    Goal_Lohia, Goal_Gosu = function_to_test(T)
    print(Goal_Lohia, Goal_Gosu)

def test_the_function():
    Goal_Lohia, Goal_Gosu = function_to_test(<test input 1>)
    # add code to test result here

    Goal_Lohia, Goal_Gosu = function_to_test(<test input 2>)
    # add code to test result here
def功能到测试(T):
对于范围(T)内的x:
#允许我们以|数量的测试\n x|u energy y|u energy z|u energy |格式输入并将方法应用于所有iterables。T=测试次数,x为洛希亚能量,y为戈苏能量,z为守门员能量
x、 y,z=list(映射(int,input().split(“”))
目标_Lohia,目标_Gosu=0,0
...
回程球门(卢希亚),球门(戈苏)
获取用户输入()的def函数:
T=int(输入())
目标为Lohia,目标为Gosu=功能测试(T)
打印(Goal_Lohia,Goal_Gosu)
def test__函数():
Goal\u Lohia,Gosu=function\u to\u test()
#在此处向测试结果添加代码
Goal\u Lohia,Gosu=function\u to\u test()
#在此处向测试结果添加代码

最好的方法是将此逻辑提取为纯函数,并将解析后的数据传递给它:

def logic(records):
    for x, y, z in records:
        ...

    return goal_lohia, goal_gosu
    

def main():
    t = int(input())
    records = []
    for _ in range(t):
        records.append(list(int(c) for c in input().split()))

    print(logic(records))

现在,您可以独立于输入/输出问题来测试
logic()

欢迎使用SO!您是否使用unittest作为您的框架?您是否已经编写了一些样板文件来设置测试,您可以将这些测试作为一个起点来共享,并准确地指出您所处的困境?可能会有帮助,并且有pyunit和unittest的信息…嘿。这对你有帮助吗?我只是回去检查我贴出的答案,看看我是否能帮上更多的忙,或者我是否能找出为什么我的答案没有被提问者接受或投票。