# coding: utf-8
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()

@@ -8,16 +8,34 @@
 g: utf-8
+%0A%0A# Python modules
 %0Afrom __
@@ -97,23 +97,8 @@
  os%0A
-import pytest%0A%0A
 from
@@ -160,37 +160,127 @@
 er%0A%0A
-from requests import Response
+# Third-party modules%0Aimport pytest%0A%0A# Project modules%0Afrom requests import Response%0Afrom tests import request_is_valid
 %0A%0A%0Ad
@@ -434,32 +434,35 @@
 ame(__file__), '
+../
 assets', filenam
@@ -596,542 +596,447 @@
 n%0A%0A%0A
-def make_simple_text_mock(filename):%0A    def mockreturn(*args, **kwargs):%0A        with open(os.path.join(os.path.dirname(__file__), 'assets', filename), 'r', 'utf-8') as fd:%0A            return fd.read()%0A    return mockreturn%0A%0A%0A@contextmanager%0Adef assert_raises(exception_class, msg=None):%0A    %22%22%22Check that an exception is raised and its message contains %60msg%60.%22%22%22%0A    with pytest.raises(exception_class) as exception:%0A        yield%0A    if msg is not None:%0A        message = '%25s' %25 exception%0A        assert msg.lower() in message.lower()
+class ContextualTest(object):%0A    def __init__(self, monkeypatch, manager, action, service):%0A        self.manager = manager%0A        self.action = action%0A        self.service = service%0A        monkeypatch.setattr('requests.post', make_requests_get_mock(self.action + '_response.xml'))%0A%0A    def close(self):%0A        request = getattr(self.manager, self.service + '_request')%0A        assert request_is_valid(request, self.action, self.service)%0A%0A
 %0A

Use context manager for the offline tests