Skip to content

Instantly share code, notes, and snippets.

@markph0204
Created June 14, 2018 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markph0204/d77137b60fa73e6db8b4f10ede59568b to your computer and use it in GitHub Desktop.
Save markph0204/d77137b60fa73e6db8b4f10ede59568b to your computer and use it in GitHub Desktop.
Pytest monkey patching builtins open
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