Skip to content

Instantly share code, notes, and snippets.

@dangra
Last active June 7, 2016 11:32
Show Gist options
  • Save dangra/b7605a56c87704c541a1517caa05126b to your computer and use it in GitHub Desktop.
Save dangra/b7605a56c87704c541a1517caa05126b to your computer and use it in GitHub Desktop.
$ py.test test-json-envvar.py
======================================== test session starts ========================================
platform linux2 -- Python 2.7.6, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /home/daniel, inifile:
plugins: hypothesis-3.4.0
collected 1 items
test-json-envvar.py .
===================================== 1 passed in 5.29 seconds ======================================
# Test environment variable can contain any json encoded value
import sys
import json
from subprocess import check_output
from hypothesis import strategies as st, given
jsonst = st.recursive(
st.floats(allow_nan=False) | st.booleans() | st.text() | st.none(),
lambda children: st.lists(children) | st.dictionaries(st.text(), children),
)
CMD = '''
{} -c '
import json, os
value = json.loads(os.getenv("TEST"))
print(json.dumps(value))
'
'''.format(sys.executable)
@given(jsonst)
def test_json_envvar(value):
enc = json.dumps(value)
out = check_output(CMD, shell=True, env={'TEST': enc})
assert json.loads(out) == value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment