How to specify attributes with Mock of python

I had the opportunity to make a mock with many attributes, so I investigated how to specify the attributes. The third method introduced to expand and pass the dict seems to be easy to use.

Assign a value to the attribute of the Mock instance

This is OK for simple cases

mock = Mock()
mock.hoge = "HOGE"

Specified by an argument when creating a Mock instance

You can use this method to set attributes other than the keywords used in the argument. You can check the argument structure at here.

mock = Mock(hoge="HOGE")

How to expand by passing an object as an argument in the application of ↑

I personally like this. I feel that it is easy to see especially when the number increases.

mock = Mock(**{
  "hoge" : "HOGE"
})

Use configure_mock after instantiation

mock = Mock()
mock.configure_mock(**{
  "hoge" : "HOGE"
})

Recommended Posts

How to specify attributes with Mock of python
[Python] How to specify the download location with youtube-dl
[Python] Summary of how to specify the color of the figure
How to get started with Python
How to use FTP with Python
How to calculate date with python
[Python] Summary of how to use pandas
How to work with BigQuery in Python
How to display python Japanese with lolipop
[Python2.7] Summary of how to use unittest
How to enter Japanese with Python curses
Summary of how to use Python list
[Python] How to deal with module errors
[Python2.7] Summary of how to use subprocess
How to install python3 with docker centos
[Question] How to use plot_surface of python
How to upload with Heroku, Flask, Python, Git (4)
[Python] How to specify the window display position and size of matplotlib
How to enjoy programming with Minecraft (Ruby, Python)
[REAPER] How to play with Reascript in Python
How to specify the NIC to scan with amazon-dash
How to do multi-core parallel processing with python
How to install Python
How to crop the lower right part of the image with Python OpenCV
Summary of how to import files in Python 3
Specify the Python executable to use with virtualenv
How to crop an image with Python + OpenCV
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Summary of how to use MNIST in Python
How to deal with SSL error when connecting to S3 with boto of Python
How to specify TLS version in python requests
How to implement "named_scope" of RubyOnRails with Django
How to measure execution time with Python Part 1
How to get dictionary type elements of Python 2.7
How to use tkinter with python in pyenv
[TF] How to specify variables to update with Optimizer
[Python] How to handle Japanese characters with openCV
[Python] How to compare datetime with timezone added
How to measure execution time with Python Part 2
How to download all photos of egao school photo service with python base
Summary of how to read numerical data with python [CSV, NetCDF, Fortran binary]
Note: How to get the last day of the month with python (added the first day of the month)
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to convert / restore a string with [] in python
A memo connected to HiveServer2 of EMR with python
How to get the number of digits in Python
I tried to summarize how to use matplotlib of python
How to scrape image data from flickr with python
How to do hash calculation with salt in Python
[Introduction to Python] How to iterate with the range function?
Change IP settings to ACL of conoha with python
Specify upper directory / subdirectory with relative import of Python
How to upload with Heroku, Flask, Python, Git (Part 3)
How to run tests in bulk with Python unittest
[Chapter 5] Introduction to Python with 100 knocks of language processing
How to write a list / dictionary type of Python3
How to measure mp3 file playback time with python
How to use python interactive mode with git bash
How to use Python Kivy ① ~ Basics of Kv Language ~
How to output CSV of multi-line header with pandas