from __future__ import unicode_literals, print_function
import os
import pytest

from codecs import open
from contextlib import contextmanager

from requests import Response


def make_requests_get_mock(filename):
    def mockreturn(*args, **kwargs):
        response = Response()
        with open(os.path.join(os.path.dirname(__file__), 'assets', filename), 'r', 'utf-8') as fd:
            response._content = fd.read().encode('utf-8')
        return response
    return mockreturn


def make_simple_text_mock(filename):
    def mockreturn(*args, **kwargs):
        with open(os.path.join(os.path.dirname(__file__), 'assets', filename), 'r', 'utf-8') as fd:
            return fd.read()
    return mockreturn


@contextmanager
def assert_raises(exception_class, msg=None):
    """Check that an exception is raised and its message contains `msg`."""
    with pytest.raises(exception_class) as exception:
        yield
    if msg is not None:
        message = '%s' % exception
        assert msg.lower() in message.lower()

from __future__ import unicode_literals, print_function
import os
from codecs import open
from contextlib import contextmanager

# Third-party modules
import pytest

# Project modules
from requests import Response
from tests import request_is_valid


def make_requests_get_mock(filename):
    def mockreturn(*args, **kwargs):
        response = Response()
        with open(os.path.join(os.path.dirname(__file__), '../assets', filename), 'r', 'utf-8') as fd:
            response._content = fd.read().encode('utf-8')
        return response
    return mockreturn


class ContextualTest(object):
    def __init__(self, monkeypatch, manager, action, service):
        self.manager = manager
        self.action = action
        self.service = service
        monkeypatch.setattr('requests.post', make_requests_get_mock(self.action + '_response.xml'))

    def close(self):
        request = getattr(self.manager, self.service + '_request')
        assert request_is_valid(request, self.action, self.service)



Use context manager for the offline tests
Use context manager for the offline tests

  from __future__ import unicode_literals, print_function
  import os
- import pytest
- 
  from codecs import open
  from contextlib import contextmanager
  
+ # Third-party modules
+ import pytest
+ 
+ # Project modules
  from requests import Response
+ from tests import request_is_valid
  
  
  def make_requests_get_mock(filename):
      def mockreturn(*args, **kwargs):
          response = Response()
-         with open(os.path.join(os.path.dirname(__file__), 'assets', filename), 'r', 'utf-8') as fd:
+         with open(os.path.join(os.path.dirname(__file__), '../assets', filename), 'r', 'utf-8') as fd:
              response._content = fd.read().encode('utf-8')
          return response
      return mockreturn
  
  
- def make_simple_text_mock(filename):
-     def mockreturn(*args, **kwargs):
-         with open(os.path.join(os.path.dirname(__file__), 'assets', filename), 'r', 'utf-8') as fd:
-             return fd.read()
-     return mockreturn
+ class ContextualTest(object):
+     def __init__(self, monkeypatch, manager, action, service):
+         self.manager = manager
+         self.action = action
+         self.service = service
+         monkeypatch.setattr('requests.post', make_requests_get_mock(self.action + '_response.xml'))
+ 
+     def close(self):
+         request = getattr(self.manager, self.service + '_request')
+         assert request_is_valid(request, self.action, self.service)
  
  
- @contextmanager
- def assert_raises(exception_class, msg=None):
-     """Check that an exception is raised and its message contains `msg`."""
-     with pytest.raises(exception_class) as exception:
-         yield
-     if msg is not None:
-         message = '%s' % exception
-         assert msg.lower() in message.lower()
  
Use context manager for the offline tests
## Code Before:
from __future__ import unicode_literals, print_function
import os
import pytest

from codecs import open
from contextlib import contextmanager

from requests import Response


def make_requests_get_mock(filename):
    def mockreturn(*args, **kwargs):
        response = Response()
        with open(os.path.join(os.path.dirname(__file__), 'assets', filename), 'r', 'utf-8') as fd:
            response._content = fd.read().encode('utf-8')
        return response
    return mockreturn


def make_simple_text_mock(filename):
    def mockreturn(*args, **kwargs):
        with open(os.path.join(os.path.dirname(__file__), 'assets', filename), 'r', 'utf-8') as fd:
            return fd.read()
    return mockreturn


@contextmanager
def assert_raises(exception_class, msg=None):
    """Check that an exception is raised and its message contains `msg`."""
    with pytest.raises(exception_class) as exception:
        yield
    if msg is not None:
        message = '%s' % exception
        assert msg.lower() in message.lower()

## Instruction:
Use context manager for the offline tests
## Code After:
from __future__ import unicode_literals, print_function
import os
from codecs import open
from contextlib import contextmanager

# Third-party modules
import pytest

# Project modules
from requests import Response
from tests import request_is_valid


def make_requests_get_mock(filename):
    def mockreturn(*args, **kwargs):
        response = Response()
        with open(os.path.join(os.path.dirname(__file__), '../assets', filename), 'r', 'utf-8') as fd:
            response._content = fd.read().encode('utf-8')
        return response
    return mockreturn


class ContextualTest(object):
    def __init__(self, monkeypatch, manager, action, service):
        self.manager = manager
        self.action = action
        self.service = service
        monkeypatch.setattr('requests.post', make_requests_get_mock(self.action + '_response.xml'))

    def close(self):
        request = getattr(self.manager, self.service + '_request')
        assert request_is_valid(request, self.action, self.service)


