question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide instructions on how to write decorators like @mock_{resource}

See original GitHub issue

I apologize, but this isn’t a bug report or a feature request, and there doesn’t appear to be a discussion group anywhere. If someone can point me to somewhere where it’s more appropriate to ask questions like this, I’ll be happy to oblige.

This is one of the best testing libraries I’ve ever worked with and I want to use its principles to mock other APIs I work with.

My main questions is how the decorators (or context managers) work under the hood. I’ve spent a few hours reading the code, but I’ll admit there’s too much abstraction involved for me to follow some of the details. I’m looking for how I can recreate the terrific convenience of decorators like mock_s3 as opposed to having to directly patch imports for every module in my code which imports them.

e.g., it’s great how in this library you can do this

@mock_s3
def test_some_biz_logic():
    biz_logic_caller()

as opposed to having to do

@patch("biz_logic.boto3", mock_boto3):
def test_biz_logic_caller():
    biz_logic_caller()

where a @patch is needed for every module involved where boto3 is imported.

I posted an SO post here https://stackoverflow.com/questions/67138758/use-decorator-to-mock-an-import-within-the-wrapped-function

Am I right that the patching here https://github.com/spulec/moto/blob/master/moto/core/models.py#L508 is somehow globally patching the boto module?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
bblommerscommented, Apr 24, 2021

Glad you got it working!

0reactions
brno32commented, Apr 24, 2021

Not sure either. But I figured it out using responses, and I’m storing state in an exported, instantiated class like moto does. Thank you for the help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Primer on Python Decorators
In this tutorial on decorators, we'll look at what they are and how to create and use them. Decorators provide a simple syntax...
Read more >
How to Create and Use Decorators in Python With Examples
Python decorators allow you to change the behavior of a function without modifying the function itself. In this article I will show you...
Read more >
6. Decorators and Decoration | Advanced - Python Courses eu
To be precise, the usage of decorators is very easy, but writing ... We offer live Python training courses covering the content of...
Read more >
Decorators in Python - GeeksforGeeks
Decorators are a very powerful and useful tool in Python since it allows programmers to modify the behaviour of a function or class....
Read more >
Python Decorators: How to Use it and Why? - Programiz
A decorator takes in a function, adds some functionality and returns it. In this tutorial, you will learn how you can create a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found