[Answered ]-Pytest / Django: How to change a decorator that is defined during build time?

1👍

You should have the DEBUG variable in your settings.py and assume that you initialize this variable from .env to determine if its production or not. Then you can initialize the TIMEOUT variable in settings.py depending on DEBUG variable like this:

DEBUG = os.getenv("DEBUG") == "True"
TIMEOUT = 0.1 if DEBUG else 10

0👍

To fix this I did 2 things:

  1. I changed the way the module is loaded:
import request_maker
  1. I reloaded the module after I updated the settings
settings.TIMEOUT = 0.1
reload(request_maker)

Here is the full new code:

###############
tests.py
###############

import pytest
from timeout_decorator import TimeoutError
import request_maker
from importlib import reload


def test_make_request(settings):
    
    settings.TIMEOUT = 0.1
    reload(request_maker)

    mr = request_maker.MakeRequest()

    with pytest.raises(TimeoutError) as e:
        mr.make_the_request()   # < Sleeps for 0.1 seconds only!

Leave a comment