summaryrefslogtreecommitdiff
path: root/testing/test_monkeypatch.py
diff options
context:
space:
mode:
authorBruno Oliveira <nicoddemus@gmail.com>2018-09-29 19:55:04 -0300
committerBruno Oliveira <nicoddemus@gmail.com>2018-10-01 17:05:30 -0300
commitd24a7e6c5a415ece9ad377d13b20daefc944fdd4 (patch)
tree1160687104f1bae4f9a4436c550f81697667d145 /testing/test_monkeypatch.py
parent5d2d64c19063c177f91d9b4a6d85ac7d092e2da8 (diff)
downloadpytest-d24a7e6c5a415ece9ad377d13b20daefc944fdd4.tar.gz
Issue warning if Monkeypatch.setenv/delenv receive non-strings in Python 2
Fixes the bug described in: https://github.com/tox-dev/tox/pull/1025#discussion_r221273830 Which is more evident when using `unicode_literals`.
Diffstat (limited to 'testing/test_monkeypatch.py')
-rw-r--r--testing/test_monkeypatch.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/testing/test_monkeypatch.py b/testing/test_monkeypatch.py
index adf10e4d0..f0f63023d 100644
--- a/testing/test_monkeypatch.py
+++ b/testing/test_monkeypatch.py
@@ -3,6 +3,8 @@ import os
import sys
import textwrap
+import six
+
import pytest
from _pytest.monkeypatch import MonkeyPatch
@@ -192,6 +194,30 @@ def test_delenv():
del os.environ[name]
+@pytest.mark.skipif(six.PY3, reason="Python 2 only test")
+class TestEnvironKeysWarning(object):
+ """
+ os.environ needs keys to be native strings, otherwise it will cause problems with other modules (notably
+ subprocess). We only test this behavior on Python 2, because Python 3 raises an error right away.
+ """
+
+ VAR_NAME = u"PYTEST_INTERNAL_MY_VAR"
+
+ def test_setenv_unicode_key(self, monkeypatch):
+ with pytest.warns(
+ pytest.PytestWarning,
+ match="Environment variable name {!r} should be str".format(self.VAR_NAME),
+ ):
+ monkeypatch.setenv(self.VAR_NAME, "2")
+
+ def test_delenv_unicode_key(self, monkeypatch):
+ with pytest.warns(
+ pytest.PytestWarning,
+ match="Environment variable name {!r} should be str".format(self.VAR_NAME),
+ ):
+ monkeypatch.delenv(self.VAR_NAME, raising=False)
+
+
def test_setenv_prepend():
import os