Skip to content
Advertisement

What is the proper way of testing throttling in DRF?

What is the proper way of testing throttling in DRF? I coulnd’t find out any answer to this question on the net. I want to have separate tests for each endpoint since each one has custom requests limits (ScopedRateThrottle).

The important thing is that it can’t affect other tests – they have to somehow run without throttling and limiting.

Advertisement

Answer

Like people already mentioned, this doesn’t exactly fall within the scope of unit tests, but still, how about simply doing something like this:

JavaScript

Also, regarding your concerns of side-effects, as long as you do user creation in setUp or setUpTestData, tests will be isolated (as they should), so no need to worry about ‘dirty’ data or scope in that sense.

Regarding cache clearing between tests, I would just add cache.clear() in tearDown or try and clear the specific key defined for throttling.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement