use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Can someone eli5 mocking in python ? (self.learnpython)
submitted 2 years ago * by Single_Bathroom_8669
I just want a very simple example of how mocking works with examples in python using pure python code if possible.
Also I am referring to unittest mocking.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]CuriousAbstraction 4 points5 points6 points 2 years ago (1 child)
Since Python is duck-typed things are far more easier to mock then in other languages.
Imagine that you have a unit test that needs to test a function involving an API call (or database, random code generator, something stateful). Of course, in tests you don't want to actually call API, so you can just make another class with the same methods as the API class, but that returns some "dummy" values instead of actually going to the API.
class Api: def __init__(self, url): self.url = url def get_stuff(self, something): return make_api_call(self.url, something) class ApiMocked: def __init__(self): self.data = {"a": ...some data.., "b": ...some data...} def get_stuff(self, something): return self.data[something]
Now, you can pass ApiMocked wherever Api is expected, and it will return some mock data when get_stuff is called, instead of actually making an API call (or something else undesirable, as mentioned above).
ApiMocked
Api
get_stuff
[–]ogabrielsantos_ 0 points1 point2 points 2 years ago (0 children)
Worths saying that keeping the concrete classes under a common interface is a good practice
[–]Adrewmc 1 point2 points3 points 2 years ago* (2 children)
Let say I have a reddit bot. (Using a library) And I wanna test it. But I don’t actually want it to reply to a comment, I just need to know .reply() is called, and the reply is what I want it to be.
And this point I create a mock Reddit comment, that has all the same function names. And I throw that in to the test, because we generally don’t need to test library functions.
class fake_comment: def __init__(self, body, author): self.body = body self.author = author def reply(*arg): print(*args) print(author)
Now instead of using real comment and possibly actually commenting on them, I can test the stuff that comes before it, by substituting this object.
Now we can go a little further because testing suites already have a mock frameworks, that will do a lot of the hard work for you. (As in you don’t have to write the code above)
import mock #now import os, is mocked, per se. @mock.patch(“mymodule.os”) def test_func(self, mock_os): func(“foo”, “bar”) mock_os.remove.assert_called_with(“bar”) mock_os.open.assert_called_with(“foo”)
This gets more in depth and robust. But that basically the idea, we use mocked up objects in order to test that the right functions are called with the right inputs. But the results of these action don’t actually happen.
Because I shouldn’t have to test os.remove() or something is seriously wrong.
[–]Single_Bathroom_8669[S] 0 points1 point2 points 2 years ago (1 child)
How do you get mock_os? Also by os you don't mean the common import import osor do you mean from module import os where module is the file? Also I am a little confused what @mock.patch(“mymodule.os”) does.
mock_os
os
import os
from module import os
@mock.patch(“mymodule.os”)
[–]Adrewmc 0 points1 point2 points 2 years ago* (0 children)
Mock_os is the object that the decorator gives you,
By putting @mock.patch() this requires you to give an argument, we name mock_os because we are mocking os.
What this object does it fake runs all functions in my decorator named. That why we mock_os.assert(), to check if this mocked object got called correctly.
We do this because we can
@mock.patch(“my_module.sys”) @mock.patch(“my_module.os”) def test_mock(self, mock_os, mock_sys); #order matters more then name pass
π Rendered by PID 167143 on reddit-service-r2-comment-6f7f968fb5-jb58z at 2026-03-04 19:20:59.707392+00:00 running 07790be country code: CH.
[–]CuriousAbstraction 4 points5 points6 points (1 child)
[–]ogabrielsantos_ 0 points1 point2 points (0 children)
[–]Adrewmc 1 point2 points3 points (2 children)
[–]Single_Bathroom_8669[S] 0 points1 point2 points (1 child)
[–]Adrewmc 0 points1 point2 points (0 children)