From 3ee61dceebcb7773699297a43507931230f06c16 Mon Sep 17 00:00:00 2001 From: Katherine Domingo Date: Fri, 24 Nov 2023 10:43:38 +0800 Subject: [PATCH] Replace unstable EnvironmentVarGuard with mock --- tests/test_models.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/test_models.py b/tests/test_models.py index 0f92d56..e1d5aa0 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,9 +1,6 @@ from __future__ import absolute_import, unicode_literals -try: - from test.support import EnvironmentVarGuard # Python < 3.10 -except ImportError: - from test.support.os_helper import EnvironmentVarGuard # Python >= 3.10 +from unittest import mock from django.test import TestCase from home.forms import CustomCaptchaFormBuilder @@ -21,11 +18,18 @@ class CaptchaTestingModeMixin(TestCase): """Allow Captcha to pass regardless of the value provided""" def setUp(self): - self.captcha_testing_mode_env = EnvironmentVarGuard() - self.captcha_testing_mode_env.set("RECAPTCHA_TESTING", "True") + # Use unittest.mock.patch to set the environment variable + self.captcha_testing_mode_patch = mock.patch.dict( + "os.environ", {"RECAPTCHA_TESTING": "True"} + ) + self.captcha_testing_mode_patch.start() self.captcha_form_data = {"recaptcha_response_field": "PASSED"} + def tearDown(self): + # Clean up the patch after the test + self.captcha_testing_mode_patch.stop() + class TestCaptchaEmailFormPageTestCase(CaptchaTestingModeMixin, TestCase): fixtures = ["test_data.json"]