How to Mock a user input in Python

落花浮王杯 提交于 2019-12-11 01:07:36

问题


I am currently trying to learn how to Unit Test with Python and was introduced to the concept of Mocking, I am a beginner Python developer hoping to learn the concepts of TDD alongside my development of Python skills. I am struggling to learn the concept of mocking a class with a given input from the user for Python unittest.mock documentation. If I could get an example of how I would mock a certain function, I would be really grateful. I will use the example found here: Example Question

class AgeCalculator(self):

    def calculate_age(self):
        age = input("What is your age?")
        age = int(age)
        print("Your age is:", age)
        return age

    def calculate_year(self, age)
        current_year = time.strftime("%Y")
        current_year = int(current_year)
        calculated_date = (current_year - age) + 100
        print("You will be 100 in", calculated_date)
        return calculated_date

Please could somebody create my an example Unit Test using Mocking to automate the input for age so that it would return the year which the mock'ed age would be 100.

Thank you.


回答1:


Here - I fixed calculate_age(), You try `calculate_year.

    class AgeCalculator:  #No arg needed to write this simple class

        def calculate_age():  # Check your sample code, no 'self' arg needed
            #age = input("What is your age?") #Can't use for testing
            print ('What is your age?') #Can use for testing 
            age = '9' # Here is where I put in a test age, substitutes for User Imput
            age = int(age)
            print("Your age is:", age)
            #return age -- Also this is not needed for this simple function

        def calculate_year(age): # Again, no 'Self' arg needed - I cleaned up your top function, you try to fix this one using my example
            current_year = time.strftime("%Y")
            current_year = int(current_year)
            calculated_date = (current_year - age) + 100
            print("You will be 100 in", calculated_date)
            return calculated_date


    AgeCalculator.calculate_age()

From what I see in your code, you should look up how to build functions - And please don't take that in an offensive manner. Also you could just test your function by hand by running it. As your code stands, it does not run.

Good luck!




回答2:


You can mock the buildins.input method in Python3.x, and using with statements to control the scope of mocking period.

import unittest.mock
def test_input_mocking():
    with unittest.mock.patch('builtins.input', return_value=100):
         xxx



回答3:


You do not mock inputs, but functions. Here, mocking input is basically the easiest thing to do.

from unittest.mock import patch

@patch('yourmodule.input')
def test(mock_input):
    mock_input.return_value = 100
    # Your test goes here


来源:https://stackoverflow.com/questions/46222661/how-to-mock-a-user-input-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!