Unittest 不会 运行 我的模拟并且仍然要求输入

Unittest would not run my mock and still asks for an input

我在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()

导入 Python 文件时,将执行不在函数中的任何代码。这意味着任何不缩进的内容都是 运行.

当单元测试文件执行 import atm 时,它将结束 运行ning 位于 atm.py 底部的 withdraw_money() 函数调用。

文件 2 中的语句 if __name__ == "__main__": 是允许函数在命令行中执行的方式 运行,但在命令行中阻止函数被调用的方式从另一个模块导入。