I got an AttributeError when mocking the open method in python

In python

open(file_path, 'r')

When mocking something like, in a test script

if __name__ == '__main__':
    unittest.main()

When you write and execute it directly

__builtins__.open = MagicMock()

I was like.

But from the command line $ python -m unittest2 -v test_module When you run like AttributeError: 'dict' object has no attribute 'open' Get angry. At this time

__builtins__['open']

You can refer to it by doing like, so in the test code

def get_builtins_open(self):
    if __name__ == '__main__':
        return __builtins__.open
    else:
        return __builtins__['open']

def set_builtins_open(self, input):
    if __name__ == '__main__':
        __builtins__.open = input
    else:
        __builtins__['open'] = input

I made a method like this. .. ..

Recommended Posts

I got an AttributeError when mocking the open method in python
I tried the least squares method in Python
[Python] I want to know the variables in the function when an error occurs!
I got an error when I put opencv in python3 with Raspberry Pi [Remedy]
What I did when I got stuck in the time limit with lambda python
I got an error in vim and zsh in Python 3.7 series
I got lost in the maze
Hash method (open address method) in Python
I wrote the queue in Python
I wrote the stack in Python
Mode line when you open the appropriate Python code in Vim
I got an error when I tried to process luigi in parallel on windows, but the solution
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
Open an Excel file in Python and color the map of Japan
I get an error when I put a Python plugin in Visual Studio Code under the pyenv environment
I got an error when saving with OpenCV
Learn the design pattern "Template Method" in Python
When I try matplotlib in Python, it says'cairo.Context'
I tried simulating the "birthday paradox" in Python
To dynamically replace the next method in python
Learn the design pattern "Factory Method" in Python
Install the python package in an offline environment
I implemented the inverse gamma function in python
[Question] What happens when I use% in python?
Try implementing the Monte Carlo method in Python
I want to display the progress in Python!
In the Chainer tutorial, I get an error when importing a package. (mock)
I stumbled on the character code when converting CSV to JSON in Python
I got an error when trying to run Hello World in Go language
I get a strange window when I use the open directory dialog in Tkinter
I referred to it when I got stuck in the django geodjango tutorial (editing)
I checked the reference speed when using python list, dictionary, and set type in.
What I do when imitating embedded go in python
I got an error that Python couldn't read settings.ini
When I tried to introduce python3 to atom, I got stuck
What I got into Python for the first time
I get an Import Error in Python Beautiful Soup
Initial settings when using the foursquare API in python
Determine the threshold using the P tile method in python
The story of FileNotFound in Python open () mode ='w'
I got stuck when trying to specify a relative path with relative_to () in python
I get an error when I put opencv in pyautoGUI
Simplex method (simplex method) in Python
Private method in python
I want to write in Python! (3) Utilize the mock
Let's use the open data of "Mamebus" in Python
I got InsecurePlatformWarning in python, so I installed requests [security]
When you get an error in python scraping (requests)
I got an error when using Tensorboard with Pytorch
I want to use the R dataset in python
I want to do something in Python when I finish
python (2) requires self because the method is an instance method
When I installed python on macOS and used it, I got an error when I put an https connection
A story that went missing when I specified a path starting with a tilde (~) in python open
I clustered the dollar yen using the k-medoids method in python and calculated the correct answer rate.
When I got a list of study sessions in Python, I found something I wanted to make
I checked the Python package pre-installed in Google Cloud Dataflow
When I made CaboCha usable with python3, I got stuck (Windows 10)
I tried the accuracy of three Stirling's approximations in python
[Python] Open the csv file in the folder specified by pandas
Mezzanine introduction memo that I got stuck in the flow