@@ -194,29 +194,39 @@ def test_delenv():
194
194
del os .environ [name ]
195
195
196
196
197
- @pytest .mark .skipif (six .PY3 , reason = "Python 2 only test" )
198
- class TestEnvironKeysWarning (object ):
197
+ class TestEnvironWarnings (object ):
199
198
"""
200
- os.environ needs keys to be native strings, otherwise it will cause problems with other modules (notably
201
- subprocess). We only test this behavior on Python 2, because Python 3 raises an error right away.
199
+ os.environ keys and values should be native strings, otherwise it will cause problems with other modules (notably
200
+ subprocess). On Python 2 os.environ accepts anything without complaining, while Python 3 does the right thing
201
+ and raises an error.
202
202
"""
203
203
204
204
VAR_NAME = u"PYTEST_INTERNAL_MY_VAR"
205
205
206
+ @pytest .mark .skipif (six .PY3 , reason = "Python 2 only test" )
206
207
def test_setenv_unicode_key (self , monkeypatch ):
207
208
with pytest .warns (
208
209
pytest .PytestWarning ,
209
210
match = "Environment variable name {!r} should be str" .format (self .VAR_NAME ),
210
211
):
211
212
monkeypatch .setenv (self .VAR_NAME , "2" )
212
213
214
+ @pytest .mark .skipif (six .PY3 , reason = "Python 2 only test" )
213
215
def test_delenv_unicode_key (self , monkeypatch ):
214
216
with pytest .warns (
215
217
pytest .PytestWarning ,
216
218
match = "Environment variable name {!r} should be str" .format (self .VAR_NAME ),
217
219
):
218
220
monkeypatch .delenv (self .VAR_NAME , raising = False )
219
221
222
+ def test_setenv_non_str_warning (self , monkeypatch ):
223
+ value = u"2" if six .PY2 else b"2"
224
+ msg = (
225
+ "Environment variable value {!r} should be str, converted to str implicitly"
226
+ )
227
+ with pytest .warns (pytest .PytestWarning , match = msg .format (value )):
228
+ monkeypatch .setenv (str (self .VAR_NAME ), value )
229
+
220
230
221
231
def test_setenv_prepend ():
222
232
import os
0 commit comments