Mock in python-how to use mox

I want to make a mock for a class I haven't made yet

If you want to create the control side for the time being, you can postpone the model creation.

test_login.py


import unittest

class TestLoginMethod(unittest.TestCase)

    def test_login_returns_true(self):
        m = mox.Mox()
        user = m.CreateMockAnything() #Intention of User model class to be created in the future
        user.account().AndReturn("norobust") #Return the account name for the time being
        l = Login() #Login class to be developed
        l.user = user #Pass a mock so that it can be referenced from within the Login class

        m.ReplayAll() #start mox!
        self.assertTrue(l.login()) #Login test!
        m.VerifyAll() #mox result check!

VerifyAll with mox to check if the method you set up (user.account () in this case) was called.

I want to make a mock of a class that already exists

When the above test is over and the User class is complete, stop CreateMockAnything and Just make it m.CreateMock (User).

I want to replace the method and pass dummy data (mock)

In the above case, it is `l.user = user```, but in reality, write something like `self.user = get_user_by_id (1) ``` inside Login.login (). I think you will get a user model. If get_user_by_id is really called, you can't pass the User mock you created. In this case, instead of mocking the Login class, you only need to stub get_user_by_id ().

test_login.py


import unittest

class TestLoginMethod(unittest.TestCase)

    def test_login_returns_true(self):
        m = mox.Mox()
        user = m.CreateMockAnything() #User model I haven't made yet ...
        user.account().AndReturn("norobust") #Return the account name for the time being
        l = Login() #Login class to be developed
        m.StubOutWithMock(Login, 'get_user_by_id') #Stub
        Login.get_user_by_id().AndReturn(user) #Set the return value of the stubbed function

        m.ReplayAll() #start mox!
        self.assertTrue(l.login()) #Login test!
        m.VerifyAll() #mox result check!

Be careful

Summary

  1. CreateMockAnything is good for writing rough implementation code, such as starting to write code
  2. I don't like CreateMockAnything, so I'll add a test with CreateMock.
  3. With CreateMock, you need to actually write the class, so write the required class
  4. Aim for completion with StubOutWithMock to stack implementations in more detail
  5. If you notice it, it's completed ( ̄ ー  ̄)

Recommended Posts

Mock in python-how to use mox
Python-How to use pyinstaller
How to use classes in Theano
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
How to use calculated columns in CASTable
[Introduction to Python] How to use class in Python?
How to use Google Test in C
Easy way to use Wikipedia in Python
Minimum knowledge to use Form in Flask
How to use Anaconda interpreter in PyCharm
How to use __slots__ in Python class
How to use regular expressions in Python
How to use Map in Android ViewPager
How to use is and == in Python
How to use the C library in Python
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
EP 11 Use `zip` to Process Iterators in Parallel
Use cryptography module to handle OpenSSL in Python
How to use tkinter with python in pyenv
Use ELMo, BERT, USE to detect anomalies in sentences
Use pygogo to get the log in json.
How to Mock a Public function in Pytest
Use os.getenv to get environment variables in Python
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
Use config.ini in Python
How to use Seaboan
How to use image-match
How to use shogun
Use Mock with pytest
How to use Pandas 2
Use DataFrame in Java
Use dates in Python
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
Easy to use Flask
Use Valgrind in Python
A memorandum on how to use keras.preprocessing.image in Keras
How to use partial
How to install Google Test / Google Mock in Visual Studio 2019
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use bootstrap in Django generic class view
How to use virtualenv
How to use template engine in pyramid 1 file application
How to use the exists clause in Django's queryset
Set constructor keyworded arguments to mock attributes in Python
Reasons to use long type in SQLite3 (C # Mono.Data.Sqlite)
How to use Matplotlib
Use ujson in requests
How to use variables in systemd Unit definition files