使用unittest测试命令行参数

2024-06-03 04:58:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写unittest,以查看从命令行获取的输入是否被正确存储。不是这样设置的,当我运行unittest时,它打开了终端,我必须在里面写一些东西,然后在里面进行输入和测试。我想要的是以这样一种方式编写测试用例:我可以将参数传递给unittest,而unittest会自动替换终端输出,并得到结果。这是我的示例代码:

class terminal_input(cmd.Cmd):
    user_database = {}
    def do_add_customer(self):
    """ Add a new customer to the database"""
        name = input("Enter your name: ") 
        id = 123
        user_database[id] = name

class terminal_input_test(unittest.TestCase):
    def test_add_new_customer (self, name, id):
        ticketing_system = terminal_input()
        ticketing_system.do_add_customer()
        #Do something here that name argument goes into input rather than typing it out
        self.assertEqual(name,ticketing_system.user_database[id])

if __name__ == '__main__':
test_case_1 = terminal_input_test()
test_case_1.test_add_new_customer(foo, 123)

Tags: nametestselfaddid终端newinput