How to use test_object method in Ddt

Best Python code snippet using ddt_python

test_monkey.py

Source:test_monkey.py Github

copy

Full Screen

1# Copyright (c) 2010 Twisted Matrix Laboratories.2# See LICENSE for details.3"""Tests for testtools.monkey."""4from testtools import TestCase5from testtools.matchers import MatchesException, Raises6from testtools.monkey import MonkeyPatcher, patch7class TestObj:8 def __init__(self):9 self.foo = 'foo value'10 self.bar = 'bar value'11 self.baz = 'baz value'12class MonkeyPatcherTest(TestCase):13 """14 Tests for 'MonkeyPatcher' monkey-patching class.15 """16 def setUp(self):17 super(MonkeyPatcherTest, self).setUp()18 self.test_object = TestObj()19 self.original_object = TestObj()20 self.monkey_patcher = MonkeyPatcher()21 def test_empty(self):22 # A monkey patcher without patches doesn't change a thing.23 self.monkey_patcher.patch()24 # We can't assert that all state is unchanged, but at least we can25 # check our test object.26 self.assertEquals(self.original_object.foo, self.test_object.foo)27 self.assertEquals(self.original_object.bar, self.test_object.bar)28 self.assertEquals(self.original_object.baz, self.test_object.baz)29 def test_construct_with_patches(self):30 # Constructing a 'MonkeyPatcher' with patches adds all of the given31 # patches to the patch list.32 patcher = MonkeyPatcher((self.test_object, 'foo', 'haha'),33 (self.test_object, 'bar', 'hehe'))34 patcher.patch()35 self.assertEquals('haha', self.test_object.foo)36 self.assertEquals('hehe', self.test_object.bar)37 self.assertEquals(self.original_object.baz, self.test_object.baz)38 def test_patch_existing(self):39 # Patching an attribute that exists sets it to the value defined in the40 # patch.41 self.monkey_patcher.add_patch(self.test_object, 'foo', 'haha')42 self.monkey_patcher.patch()43 self.assertEquals(self.test_object.foo, 'haha')44 def test_patch_non_existing(self):45 # Patching a non-existing attribute sets it to the value defined in46 # the patch.47 self.monkey_patcher.add_patch(self.test_object, 'doesntexist', 'value')48 self.monkey_patcher.patch()49 self.assertEquals(self.test_object.doesntexist, 'value')50 def test_restore_non_existing(self):51 # Restoring a value that didn't exist before the patch deletes the52 # value.53 self.monkey_patcher.add_patch(self.test_object, 'doesntexist', 'value')54 self.monkey_patcher.patch()55 self.monkey_patcher.restore()56 marker = object()57 self.assertIs(marker, getattr(self.test_object, 'doesntexist', marker))58 def test_patch_already_patched(self):59 # Adding a patch for an object and attribute that already have a patch60 # overrides the existing patch.61 self.monkey_patcher.add_patch(self.test_object, 'foo', 'blah')62 self.monkey_patcher.add_patch(self.test_object, 'foo', 'BLAH')63 self.monkey_patcher.patch()64 self.assertEquals(self.test_object.foo, 'BLAH')65 self.monkey_patcher.restore()66 self.assertEquals(self.test_object.foo, self.original_object.foo)67 def test_restore_twice_is_a_no_op(self):68 # Restoring an already-restored monkey patch is a no-op.69 self.monkey_patcher.add_patch(self.test_object, 'foo', 'blah')70 self.monkey_patcher.patch()71 self.monkey_patcher.restore()72 self.assertEquals(self.test_object.foo, self.original_object.foo)73 self.monkey_patcher.restore()74 self.assertEquals(self.test_object.foo, self.original_object.foo)75 def test_run_with_patches_decoration(self):76 # run_with_patches runs the given callable, passing in all arguments77 # and keyword arguments, and returns the return value of the callable.78 log = []79 def f(a, b, c=None):80 log.append((a, b, c))81 return 'foo'82 result = self.monkey_patcher.run_with_patches(f, 1, 2, c=10)83 self.assertEquals('foo', result)84 self.assertEquals([(1, 2, 10)], log)85 def test_repeated_run_with_patches(self):86 # We can call the same function with run_with_patches more than87 # once. All patches apply for each call.88 def f():89 return (self.test_object.foo, self.test_object.bar,90 self.test_object.baz)91 self.monkey_patcher.add_patch(self.test_object, 'foo', 'haha')92 result = self.monkey_patcher.run_with_patches(f)93 self.assertEquals(94 ('haha', self.original_object.bar, self.original_object.baz),95 result)96 result = self.monkey_patcher.run_with_patches(f)97 self.assertEquals(98 ('haha', self.original_object.bar, self.original_object.baz),99 result)100 def test_run_with_patches_restores(self):101 # run_with_patches restores the original values after the function has102 # executed.103 self.monkey_patcher.add_patch(self.test_object, 'foo', 'haha')104 self.assertEquals(self.original_object.foo, self.test_object.foo)105 self.monkey_patcher.run_with_patches(lambda: None)106 self.assertEquals(self.original_object.foo, self.test_object.foo)107 def test_run_with_patches_restores_on_exception(self):108 # run_with_patches restores the original values even when the function109 # raises an exception.110 def _():111 self.assertEquals(self.test_object.foo, 'haha')112 self.assertEquals(self.test_object.bar, 'blahblah')113 raise RuntimeError("Something went wrong!")114 self.monkey_patcher.add_patch(self.test_object, 'foo', 'haha')115 self.monkey_patcher.add_patch(self.test_object, 'bar', 'blahblah')116 self.assertThat(lambda:self.monkey_patcher.run_with_patches(_),117 Raises(MatchesException(RuntimeError("Something went wrong!"))))118 self.assertEquals(self.test_object.foo, self.original_object.foo)119 self.assertEquals(self.test_object.bar, self.original_object.bar)120class TestPatchHelper(TestCase):121 def test_patch_patches(self):122 # patch(obj, name, value) sets obj.name to value.123 test_object = TestObj()124 patch(test_object, 'foo', 42)125 self.assertEqual(42, test_object.foo)126 def test_patch_returns_cleanup(self):127 # patch(obj, name, value) returns a nullary callable that restores obj128 # to its original state when run.129 test_object = TestObj()130 original = test_object.foo131 cleanup = patch(test_object, 'foo', 42)132 cleanup()133 self.assertEqual(original, test_object.foo)134def test_suite():135 from unittest import TestLoader...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Ddt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful