[PYTHON] I want a mox generator

Writing tests has become so tedious these days that I wondered if mox could easily stub.

First of all, the class.

mox_generator.py


import mox
class MoxGenerator(object):

    def __init__(self):
        self.mox = mox.Mox()

I'm still starting to think of it, so I wrote one for the time being. Anything is fine, stub that returns True. Without even considering the arguments. This is terrible (sweat).

python


    def true_stub(self, stub_class, stub_method, num_args):
        self.mox.StubOutWithMock(stub_class, stub_method)
        stubbed = getattr(stub_class, stub_method)
        args = tuple([mox.IgnoreArg() for i in range(num_args)])
        stubbed(*args).AndReturn(True)

When calling, it looks like this. Here, MyClass.hoge (x, x, x) should return True. The last number is the number of arguments to pass to MyClass.hoge.

python


    moxgen= MoxGenerator()
    moxgen.true_stub(MyClass, 'hoge', 3)

Of course, Replay is also troublesome, so the name is strange, but put it in the generator!

python


    import contextlib
    @contextlib.contextmanager
    def mox_replay(self):
        self.mox.ReplayAll()
        yield
        self.mox.VerifyAll()

Test execution side.

example_test.py


    with moxgen.mox_replay():
        MyClass.method_which_should_call_hoge()

I also uploaded it to github. https://github.com/norobust/mox_generator Why isn't it committed with my real name? How do you change it to a username?

Recommended Posts

I want a mox generator
I want a mox generator (2)
I want to iterate a Python generator many times
I want to print in a comprehension
I want to build a Python environment
I made a stamp generator with GAN
I want to easily create a Noise Model
I want to INSERT a DataFrame into MSSQL
I want to create a window in Python
I want to make a game with Python
I don't want to take a coding test
I want to create a plug-in type implementation
I want to easily find a delicious restaurant
I want to write to a file with Python
I want to upload a Django app to heroku
〇✕ I made a game
I got a sqlite3.OperationalError
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want DQN Puniki to hit a home run
100 image processing knocks !! (021-030) I want to take a break ...
I want to give a group_id to a pandas data frame
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to transition with a button in flask
I want to climb a mountain with reinforcement learning
I want to write in Python! (2) Let's write a test
I want to find a popular package on PyPi
I want to randomly sample a file in Python
I want to easily build a model-based development environment
I want to work with a robot in python.
I want to split a character string with hiragana
I want to install a package of Php Redis
[Python] I want to make a nested list a tuple
I want to manually create a legend with matplotlib
I want a Spotify sleep timer on my PC
I want to send a business start email automatically
I want to run a quantum computer with Python
I want to bind a local variable with lambda
I want to make a blog editor with django admin
I want to start a jupyter environment with one command
[Python] I want to get a common set between numpy
I want to start a lot of processes from python
I want to make a click macro with pyautogui (desire)
I want to automatically generate a modern metal band name
NikuGan ~ I want to see a lot of delicious meat! !!
I want to solve Sudoku (Sudoku)
I want to make a click macro with pyautogui (outlook)
I want to use a virtual environment with jupyter notebook!
I want to install a package from requirements.txt with poetry
I want to send a message from Python to LINE Bot
I made a python text
I made a discord bot
[Visualization] I want to draw a beautiful graph with Plotly
I want to make input () a nice complement in python
I made a neural network generator that runs on FPGA
I want to create a Dockerfile for the time being.
I want to record the execution time and keep a log.
I want to use a wildcard that I want to shell with Python remove
MacBookPro Setup After all I want to do a clean installation
numpy: I want to convert a single type ndarray to a structured array
Qiskit: I want to create a circuit that creates arbitrary states! !!