def test_multiple_replace(self): def test_y(self): return 'test y' def test_z(): return 'test z' compare(sample1.z(), 'original z') compare(sample1.X().y(), 'original y') @replace('testfixtures.tests.sample1.z', test_z) @replace('testfixtures.tests.sample1.X.y', test_y) def test_something(passed_test_y, passed_test_z): compare(test_z, passed_test_z) compare(test_y, passed_test_y) compare(sample1.z(), 'test z') compare(sample1.X().y(), 'test y') compare(sample1.z(), 'original z') compare(sample1.X().y(), 'original y') test_something() compare(sample1.z(), 'original z') compare(sample1.X().y(), 'original y')
def test_method(self): def test_y(self): return self compare(sample1.X().y(), 'original y') @replace('testfixtures.tests.sample1.X.y', test_y) def test_something(): self.failUnless(isinstance(sample1.X().y(), sample1.X)) compare(sample1.X().y(), 'original y') test_something() compare(sample1.X().y(), 'original y')
def test_class_method(self): def rMethod(cls): return (cls, 1) compare(sample1.X().aMethod(), sample1.X) @replace('testfixtures.tests.sample1.X.aMethod', rMethod) def test_something(r): compare(r, rMethod) compare(sample1.X().aMethod(), (sample1.X, 1)) compare(sample1.X().aMethod(), sample1.X) test_something() compare(sample1.X().aMethod(), sample1.X)
def test_method(self): from testfixtures.tests import sample1 assert sample1.X().y() == 'original y' def test_y(self): return 'replacement y' r = Replacer() r.replace('testfixtures.tests.sample1.X.y',test_y) assert sample1.X().y()[:38] == 'replacement y' r.restore() assert sample1.X().y() == 'original y'
def test_class(self): from testfixtures.tests import sample1 x = sample1.X() assert x.__class__.__name__ == 'X' class XReplacement(sample1.X): pass r = Replacer() r.replace('testfixtures.tests.sample1.X', XReplacement) x = sample1.X() assert x.__class__.__name__ == 'XReplacement' assert sample1.X().y() == 'original y' r.restore() x = sample1.X() assert x.__class__.__name__ == 'X'
def test_class(self): OriginalX = sample1.X class ReplacementX(sample1.X): pass self.failIf(OriginalX is ReplacementX) self.failUnless(isinstance(sample1.X(), OriginalX)) @replace('testfixtures.tests.sample1.X', ReplacementX) def test_something(): self.failIf(OriginalX is ReplacementX) self.failUnless(isinstance(sample1.X(), ReplacementX)) self.failIf(OriginalX is ReplacementX) self.failUnless(isinstance(sample1.X(), OriginalX)) test_something() self.failIf(OriginalX is ReplacementX) self.failUnless(isinstance(sample1.X(), OriginalX))
def test_multiple_replace(self): from testfixtures.tests import sample1 assert sample1.z() == 'original z' assert sample1.X().y() == 'original y' def test_y(self): return self.__class__.__name__ def test_z(): return 'replacement z' r = Replacer() r.replace('testfixtures.tests.sample1.z',test_z) r.replace('testfixtures.tests.sample1.X.y',test_y) assert sample1.z() == 'replacement z' assert sample1.X().y() == 'X' r.restore() assert sample1.z() == 'original z' assert sample1.X().y() == 'original y'
def test_multiple_context_managers(self): def test_y(self): return 'test y' def test_z(): return 'test z' compare(sample1.z(), 'original z') compare(sample1.X().y(), 'original y') with Replacer() as replace: z = replace('testfixtures.tests.sample1.z', test_z) y = replace('testfixtures.tests.sample1.X.y', test_y) compare(z(), 'test z') if PY3: compare(y, sample1.X.y) compare(sample1.X().y(), 'test y') compare(sample1.z(), 'test z') compare(sample1.X().y(), 'test y') compare(sample1.z(), 'original z') compare(sample1.X().y(), 'original y')
def test_something(passed_test_y, passed_test_z): compare(test_z, passed_test_z) compare(test_y, passed_test_y) compare(sample1.z(), 'test z') compare(sample1.X().y(), 'test y')
def test_something(r): compare(r, rMethod) compare(sample1.X().aMethod(), (sample1.X, 1))
def test_something(): self.failUnless(isinstance(sample1.X().y(), sample1.X))
def test_something(): self.failIf(OriginalX is ReplacementX) self.failUnless(isinstance(sample1.X(), ReplacementX))