Created
June 14, 2018 23:16
-
-
Save markph0204/d77137b60fa73e6db8b4f10ede59568b to your computer and use it in GitHub Desktop.
Pytest monkey patching builtins open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import builtins | |
import pytest | |
from io import StringIO | |
import yaml | |
TEST_CONFIG = """ | |
environment: prod | |
""" | |
def read_file(): | |
with open("config.yaml", 'r') as stream: | |
try: | |
data = yaml.load(stream) | |
return data | |
def test_read_file(monkeypatch): | |
mopen = lambda x, y: StringIO(initial_value=TEST_CONFIG) | |
monkeypatch.setattr(builtins, 'open', mopen) | |
config = read_environment_config() | |
assert config.environment == 'prod' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment