单元测试不会运行我的模拟,但仍然要求输入
我在Pycharm中具有简单的ATM刺激功能,并想为我的功能编写测试。但是,每次我在第一个函数要求使用用户PIN的功能上进行测试时,终端都会要求我输入而不是运行模拟。这是我的功能:
def validate_pin(correct_pin):
pin_input = input('PLEASE ENTER YOUR 4-DIGIT PIN: ')
if pin_input.isnumeric() is True and int(pin_input) == correct_pin:
return True
return False
def calculate_balance(current_balance):
withdraw_amount = input("Please enter the amount you would like to withdraw: ")
if withdraw_amount.isnumeric() is not True:
raise TypeError
new_balance = current_balance - int(withdraw_amount)
if new_balance < 0:
raise Exception
return new_balance
def withdraw_money():
correct_pin = 1223
attempts = 3
balance = 100
while attempts != 0:
pin_validity = validate_pin(correct_pin)
if pin_validity is True:
print('PIN is correct.')
try:
new_balance = calculate_balance(balance)
except TypeError:
print("Please enter numbers only!")
except Exception:
print("You have put an unaccepted amount! Try again later.")
else:
print("You have £{} remaining in your account.".format(new_balance))
finally:
print("☻☻☻ THANK YOU FOR USING THIS ATM. HAVE A GOOD DAY! ☻☻☻")
break
else:
attempts -= 1
print("Wrong pin, try again! You only have {} attempts remaining.".format(attempts))
withdraw_money()
我尝试了不同版本的语法来运行测试,其中哪些有效...有什么想法?
1。
import unittest
from unittest.mock import patch
import atm
from unittest import TestCase
class TestingVersionThree(unittest, TestCase):
@patch('builtins.input', lambda *args: '1223')
def test_func1(self):
self.assertEqual(atm.validate_pin(), True)
@patch('builtins.input', lambda *args: '1111')
def test_func2(self):
self.assertEqual(atm.validate_pin(), False)
if __name__ == '__main__':
unittest.main()
class TestPinMockedInput(unittest.TestCase):
@patch('builtins.input', lambda x: "1223")
def test_with_valid_input(self):
result = atm.validate_pin()
expected_result = True
self.assertTrue(result == expected_result)
@patch('builtins.input', lambda x: "1111")
def test_invalid_input_wrong_number(self):
with self.assertRaises(ValueError):
result = atm.validate_pin()
expected_result = False
self.assertTrue(result == expected_result)
@patch('builtins.input', lambda x: "hi12")
def test_invalid_input_non_numeric(self):
with self.assertRaises(ValueError):
result = atm.validate_pin()
expected_result = False
self.assertTrue(result == expected_result)
if __name__ == '__main__':
unittest.main()
I have a simple ATM stimulating function in Pycharm and want to write tests for my functions. However every time I run my test on the first function asking for a User Pin, the terminal asks me for an input instead of running the mock. Here is my function:
def validate_pin(correct_pin):
pin_input = input('PLEASE ENTER YOUR 4-DIGIT PIN: ')
if pin_input.isnumeric() is True and int(pin_input) == correct_pin:
return True
return False
def calculate_balance(current_balance):
withdraw_amount = input("Please enter the amount you would like to withdraw: ")
if withdraw_amount.isnumeric() is not True:
raise TypeError
new_balance = current_balance - int(withdraw_amount)
if new_balance < 0:
raise Exception
return new_balance
def withdraw_money():
correct_pin = 1223
attempts = 3
balance = 100
while attempts != 0:
pin_validity = validate_pin(correct_pin)
if pin_validity is True:
print('PIN is correct.')
try:
new_balance = calculate_balance(balance)
except TypeError:
print("Please enter numbers only!")
except Exception:
print("You have put an unaccepted amount! Try again later.")
else:
print("You have £{} remaining in your account.".format(new_balance))
finally:
print("☻☻☻ THANK YOU FOR USING THIS ATM. HAVE A GOOD DAY! ☻☻☻")
break
else:
attempts -= 1
print("Wrong pin, try again! You only have {} attempts remaining.".format(attempts))
withdraw_money()
I have tried different versions of syntax to run the tests, non of which works ... Any thoughts?
1.
import unittest
from unittest.mock import patch
import atm
from unittest import TestCase
class TestingVersionThree(unittest, TestCase):
@patch('builtins.input', lambda *args: '1223')
def test_func1(self):
self.assertEqual(atm.validate_pin(), True)
@patch('builtins.input', lambda *args: '1111')
def test_func2(self):
self.assertEqual(atm.validate_pin(), False)
if __name__ == '__main__':
unittest.main()
class TestPinMockedInput(unittest.TestCase):
@patch('builtins.input', lambda x: "1223")
def test_with_valid_input(self):
result = atm.validate_pin()
expected_result = True
self.assertTrue(result == expected_result)
@patch('builtins.input', lambda x: "1111")
def test_invalid_input_wrong_number(self):
with self.assertRaises(ValueError):
result = atm.validate_pin()
expected_result = False
self.assertTrue(result == expected_result)
@patch('builtins.input', lambda x: "hi12")
def test_invalid_input_non_numeric(self):
with self.assertRaises(ValueError):
result = atm.validate_pin()
expected_result = False
self.assertTrue(result == expected_result)
if __name__ == '__main__':
unittest.main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
导入Python文件时,执行函数中未使用的任何代码。这意味着任何未缩进的东西都是运行的。
当单元测试文件执行
导入ATM
时,它将最终运行fort_money()
函数调用,该函数在atm.py
的底部运行。文件2中的语句,
如果__name __ ==“ __ main __”:
是,如果从命令行运行文件,则允许执行函数,但是如果该文件是从命令行运行的,但是在该函数中可以防止该函数在该函数时。是从另一个模块导入的。When a Python file is imported, any code that is not in a function is executed. That means anything that is not indented is run.
When the unit test file does
import atm
it will end up running thewithdraw_money()
function call that is at the bottom ofatm.py
.The statement in file 2,
if __name__ == "__main__":
, is the way of allowing the function to be executed if the file is run from the command line, but prevents the function being called if it is imported from another module.