Skip to content Skip to sidebar Skip to footer
Showing posts with the label Mocking

Writing A Contextmanager That Patches An Object

In my Python 3 test code, I have a lot of these statements: from unittest.mock import patch user =… Read more Writing A Contextmanager That Patches An Object

Mock A Pyqt Method

I have an existing class which inherits from the PyQT5 classes QWidget and Ui_Dialog. I want to use… Read more Mock A Pyqt Method

Mock Open() Function Used In A Class Method

I tried to mock the open function used in a method of my class. I found this thread How do I mock a… Read more Mock Open() Function Used In A Class Method

How To Patch The Python 3 Breakpoint() Builtin?

I wrote a decorator that halts my program with breakpoint() if a critical error happens. def the_br… Read more How To Patch The Python 3 Breakpoint() Builtin?

Mocking Render To Response With Pyramid

I have a decorator that looks like so: def validate_something(func): def validate_s(request): … Read more Mocking Render To Response With Pyramid

How To Mock Python's Datetime.now() In A Class Method For Unit Testing?

I'm trying to write tests for a class that has methods like: import datetime import pytz class… Read more How To Mock Python's Datetime.now() In A Class Method For Unit Testing?

Max Recursion Depth While Trying To Mock Instance Method

I want to mock an instance method. First I want to modify the arguments and then I want to call the… Read more Max Recursion Depth While Trying To Mock Instance Method

Making A Wrapper For `mock.patch`

Some customized patches from mock.patch I want to use over and over without littering my test code … Read more Making A Wrapper For `mock.patch`

Mocking Grpc Status Code ('rpcerror' Object Has No Attribute 'code') In Flask App

I have to create a unittest that should mock a specific grpc status code (in my case I need NOT_FOU… Read more Mocking Grpc Status Code ('rpcerror' Object Has No Attribute 'code') In Flask App

Python Mock And Libraries That Are Not Installed

I am working on software for a robot, which is normally run on the Raspberry Pi. Let's consider… Read more Python Mock And Libraries That Are Not Installed

Patch - Why Won't The Relative Patch Target Name Work?

I've imported a class from a module, but when I try to patch the class name without it's mo… Read more Patch - Why Won't The Relative Patch Target Name Work?

How Do I Patch A Sys Attribute Using A Decorator?

I have a function which depends on the Python version. I would like to test this in a unittest by m… Read more How Do I Patch A Sys Attribute Using A Decorator?

How To Mock Just The Method Inside The Class

Trying to write a testcase for my class based function. This is skeleton of my class class Library(… Read more How To Mock Just The Method Inside The Class

How Do I Test That I'm Calling Pickle.dump() Correctly?

I want to test this method: class Data(object): def save(self, filename=''): i… Read more How Do I Test That I'm Calling Pickle.dump() Correctly?

How Does @mock.patch Know Which Parameter To Use For Each Mock Object?

Looking at this webpage: http://www.toptal.com/python/an-introduction-to-mocking-in-python -- The a… Read more How Does @mock.patch Know Which Parameter To Use For Each Mock Object?

Mocking File Reads Using Patch Decorator And Side_effect

I am testing a function that reads a file, does operations to the contents and returns a value depe… Read more Mocking File Reads Using Patch Decorator And Side_effect

Integrating Mock And Patch In A Python Unit Test

I have a class with a few methods that I am writing unit test cases for. For minimum reproducible e… Read more Integrating Mock And Patch In A Python Unit Test

Why Does Mocking 'open' And Returning A FileNotFoundError Raise AttributeError: __exit__?

Testing by mocking open with a FileNotFoundError raises AttributeError: __exit__. Why is this happe… Read more Why Does Mocking 'open' And Returning A FileNotFoundError Raise AttributeError: __exit__?