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

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?

Mock A Remote Host In Python

I am writing some functions, using paramiko, to execute commands and create files on a remote host.… Read more Mock A Remote Host In Python

How To Mock A Socket Object Via The Mock Library

How to mock a TCPSocket wrapper for the socket from the Python's standard libary via the mock l… Read more How To Mock A Socket Object Via The Mock Library

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__?

Concept: Mocking Db Python

I'm working with postgresql and I have used MagicMock to test, but I'm not sure that I have… Read more Concept: Mocking Db Python

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?

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

Python Patch Object With A Side_effect

I'm trying to have a Mock object return certain values based on the input given. I looked up a … Read more Python Patch Object With A Side_effect

Mock.patch And Multiprocessing

I'm struggling to use mock.patch in a multiprocessing environment while without multiprocessing… Read more Mock.patch And Multiprocessing

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

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?

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 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

Really Weird... Can't Set Attributes Of Built-in/extension Type 'lxml.etree._element'

I've changed attributes for other classes before without issues. _Element is obviously not a bu… Read more Really Weird... Can't Set Attributes Of Built-in/extension Type 'lxml.etree._element'

Mocked Unit Test Raises A "stop Called On Unstarted Patcher" Error

When running the test bellow, I got a stop called on unstarted patcher. def test_get_subvention_int… Read more Mocked Unit Test Raises A "stop Called On Unstarted Patcher" Error

Mocking Methods On An Instance Variable In Python

I'm trying to figure out how to properly Mock an instance variable which is an instance of anot… Read more Mocking Methods On An Instance Variable In Python

Pytest - Patched Method Of A Class Does Not Return The Mock Value

My code is fairly simple but i don't understand what is going on : class MyDb : def some_fun… Read more Pytest - Patched Method Of A Class Does Not Return The Mock Value

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 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 Permanently Mock Return Value Of A Function In Python Unittest

I have a function # foo.py NUM_SHARDS = 10 def get_shard(shard_key: int) -> int return (shar… Read more How To Permanently Mock Return Value Of A Function In Python Unittest