How to skip Recaptcha in Django testing
Recaptcha is supposed to prevent robots from taking automated actions on your site. However, it is not desirable to have recaptcha stop your Django automated testing. It is not desirable to manually turn off receptcha before testing as well. This article provide a simple tips on how to disable Recaptcha for testing.
To skip Recaptcha, we will make use of two different setting configuations, using two setting.py files, with one setting for production and one setting for testing. First, we have to edit our program code so that the Recaptcha is configuable from setting. Most often, Recaptcha is implemented as a form field.
class RegistrationForm(forms.Form):
if settings.ENABLE_CAPTCHA:
recaptcha = ReCaptchaField()
By setting ENABLE_CAPTCHA = True in production setting.py, the Recaptcha field will be enabled. Now create setting_testing.py file in the same directory as the setting.py file and have the following content:
from settings import *
ENABLE_CAPTCHA=False
The manage.py have to be changed as well to identify the setting_testing.py file. Create a copy of manage.py as manage_testing.py, and import the setting_testing instead:
import test_settings as settings # Assumed to be in the same directory.
Now run testing using manage_testing.py instead:
$ python manage_testing.py test
ReCaptchaField is now skipped and tests should be run smoothly
Please login to post comment.