Skip to content

Commit a39a168

Browse files
authored
[py] Use mock.patch for environment variables in tests (SeleniumHQ#15607)
Better use of mocking in tests
1 parent 7b530cd commit a39a168

File tree

6 files changed

+28
-39
lines changed

6 files changed

+28
-39
lines changed

py/test/selenium/webdriver/chrome/chrome_service_tests.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import subprocess
1919
import time
20+
from unittest.mock import patch
2021

2122
import pytest
2223

@@ -125,8 +126,6 @@ def test_uses_path_from_env_variable(self, service):
125126
assert "chromedriver" in service.path
126127

127128
def test_updates_path_after_setting_env_variable(self, service):
128-
new_path = "/foo/bar"
129-
os.environ["SE_CHROMEDRIVER"] = new_path
130129
service.executable_path = self.service_path # Simulating the update
131-
132-
assert "chromedriver" in service.executable_path
130+
with patch.dict("os.environ", {"SE_CHROMEDRIVER": "/foo/bar"}):
131+
assert "chromedriver" in service.executable_path

py/test/selenium/webdriver/chrome/proxy_tests.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
import os
18+
from unittest.mock import patch
1919

2020
import pytest
2121
import urllib3
@@ -25,20 +25,15 @@
2525

2626
@pytest.mark.no_driver_after_test
2727
def test_bad_proxy_doesnt_interfere(clean_driver, clean_service):
28-
# these values should be ignored if ignore_local_proxy_environment_variables() is called.
29-
os.environ["https_proxy"] = "bad"
30-
os.environ["http_proxy"] = "bad"
28+
# Proxy environment variables should be ignored if
29+
# ignore_local_proxy_environment_variables() is called.
3130

3231
options = webdriver.ChromeOptions()
33-
3432
options.ignore_local_proxy_environment_variables()
35-
3633
chrome_kwargs = {"options": options, "service": clean_service}
37-
driver = clean_driver(**chrome_kwargs)
38-
34+
with patch.dict("os.environ", {"http_proxy": "bad", "https_proxy": "bad"}):
35+
driver = clean_driver(**chrome_kwargs)
3936
assert hasattr(driver, "command_executor")
4037
assert hasattr(driver.command_executor, "_proxy_url")
4138
assert isinstance(driver.command_executor._conn, urllib3.PoolManager)
42-
os.environ.pop("https_proxy")
43-
os.environ.pop("http_proxy")
4439
driver.quit()

py/test/selenium/webdriver/edge/edge_service_tests.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import subprocess
1919
import time
20+
from unittest.mock import patch
2021

2122
import pytest
2223

@@ -125,8 +126,6 @@ def test_uses_path_from_env_variable(self, service):
125126
assert "msedgedriver" in service.path
126127

127128
def test_updates_path_after_setting_env_variable(self, service):
128-
new_path = "/foo/bar"
129-
os.environ["SE_EDGEDRIVER"] = new_path
130129
service.executable_path = self.service_path # Simulating the update
131-
132-
assert "msedgedriver" in service.executable_path
130+
with patch.dict("os.environ", {"SE_EDGEDRIVER": "/foo/bar"}):
131+
assert "msedgedriver" in service.executable_path

py/test/selenium/webdriver/firefox/firefox_service_tests.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717
import os
1818
import subprocess
19+
from unittest.mock import patch
1920

2021
import pytest
2122

@@ -79,8 +80,6 @@ def test_uses_path_from_env_variable(self, service):
7980
assert "geckodriver" in service.path
8081

8182
def test_updates_path_after_setting_env_variable(self, service):
82-
new_path = "/foo/bar"
83-
os.environ["SE_GECKODRIVER"] = new_path
8483
service.executable_path = self.service_path # Simulating the update
85-
86-
assert "geckodriver" in service.executable_path
84+
with patch.dict("os.environ", {"SE_GECKODRIVER": "/foo/bar"}):
85+
assert "geckodriver" in service.executable_path

py/test/selenium/webdriver/safari/safari_service_tests.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717

1818
import os
19+
from unittest.mock import patch
1920

2021
import pytest
2122

@@ -41,11 +42,9 @@ def test_uses_path_from_env_variable(self, service):
4142
assert "safaridriver" in service.path
4243

4344
def test_updates_path_after_setting_env_variable(self, service):
44-
new_path = "/foo/bar"
45-
os.environ["SE_SAFARIDRIVER"] = new_path
4645
service.executable_path = self.service_path # Simulating the update
47-
48-
assert "safaridriver" in service.executable_path
46+
with patch.dict("os.environ", {"SE_SAFARIDRIVER": "/foo/bar"}):
47+
assert "safaridriver" in service.executable_path
4948

5049

5150
def test_enable_logging():

py/test/unit/selenium/webdriver/remote/remote_connection_tests.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
import os
1918
from unittest.mock import patch
2019
from urllib import parse
2120

@@ -168,18 +167,17 @@ def test_get_proxy_direct_via_client_config():
168167

169168

170169
def test_get_proxy_system_matches_no_proxy_via_client_config():
171-
os.environ["HTTP_PROXY"] = "http://admin:admin@system_proxy.com:8080"
172-
os.environ["NO_PROXY"] = "localhost,127.0.0.1"
173-
client_config = ClientConfig(
174-
remote_server_addr="http://localhost:4444", proxy=Proxy({"proxyType": ProxyType.SYSTEM})
175-
)
176-
remote_connection = RemoteConnection(client_config=client_config)
177-
conn = remote_connection._get_connection_manager()
178-
assert isinstance(conn, urllib3.PoolManager)
179-
proxy_url = remote_connection._client_config.get_proxy_url()
180-
assert proxy_url is None
181-
os.environ.pop("HTTP_PROXY")
182-
os.environ.pop("NO_PROXY")
170+
with patch.dict(
171+
"os.environ", {"HTTP_PROXY": "http://admin:admin@system_proxy.com:8080", "NO_PROXY": "localhost,127.0.0.1"}
172+
):
173+
client_config = ClientConfig(
174+
remote_server_addr="http://localhost:4444", proxy=Proxy({"proxyType": ProxyType.SYSTEM})
175+
)
176+
remote_connection = RemoteConnection(client_config=client_config)
177+
conn = remote_connection._get_connection_manager()
178+
assert isinstance(conn, urllib3.PoolManager)
179+
proxy_url = remote_connection._client_config.get_proxy_url()
180+
assert proxy_url is None
183181

184182

185183
def test_get_proxy_url_none(mock_proxy_settings_missing):

0 commit comments

Comments
 (0)