Python 使用unittest测试命令行参数
我正在编写unittest,以查看从命令行获取的输入是否正确存储。正确的情况是,当我运行unittest时,它打开了终端,我必须在其中写入一些东西,然后需要进行输入和测试。我想要的是以这样的方式编写测试用例:我可以将参数传递给unittest,它会自动替换终端输出,然后我得到结果。以下是我的示例代码:Python 使用unittest测试命令行参数,python,windows,python-3.x,unit-testing,Python,Windows,Python 3.x,Unit Testing,我正在编写unittest,以查看从命令行获取的输入是否正确存储。正确的情况是,当我运行unittest时,它打开了终端,我必须在其中写入一些东西,然后需要进行输入和测试。我想要的是以这样的方式编写测试用例:我可以将参数传递给unittest,它会自动替换终端输出,然后我得到结果。以下是我的示例代码: class terminal_input(cmd.Cmd): user_database = {} def do_add_customer(self): """ Add a
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)
只需
unittest.mock
out函数并提供您想要的任何用户输入。您可以详细说明在这种特殊情况下我将如何进行此操作吗?请仔细查看并至少尝试一下。