本文整理汇总了Python中testfixtures.Replacer.replace方法的典型用法代码示例。如果您正苦于以下问题:Python Replacer.replace方法的具体用法?Python Replacer.replace怎么用?Python Replacer.replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类testfixtures.Replacer
的用法示例。
在下文中一共展示了Replacer.replace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestCommandTaskWithMockPopen
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
class TestCommandTaskWithMockPopen(MockLoggerMixin, unittest.TestCase):
""" Run command tasks with a mocked popen """
def setUp(self):
self.global_config = BaseGlobalConfig()
self.project_config = BaseProjectConfig(
self.global_config, config={"noyaml": True}
)
self.task_config = TaskConfig()
self._task_log_handler.reset()
self.task_log = self._task_log_handler.messages
self.Popen = MockPopen()
self.r = Replacer()
self.r.replace("cumulusci.tasks.command.subprocess.Popen", self.Popen)
self.addCleanup(self.r.restore)
def test_functional_mock_command(self):
""" Functional test that runs a command with mocked
popen results and checks the log.
"""
self.task_config.config["options"] = {"command": "ls -la"}
self.Popen.set_command("ls -la", stdout=b"testing testing 123", stderr=b"e")
task = Command(self.project_config, self.task_config)
task()
self.assertTrue(any("testing testing" in s for s in self.task_log["info"]))
示例2: setUp
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
def setUp(self):
self.dir = TempDirectory()
self.addCleanup(self.dir.cleanup)
self.Popen = MockPopen()
r = Replacer()
r.replace('archivist.helpers.Popen', self.Popen)
self.addCleanup(r.restore)
示例3: test_cleanup_properly
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
def test_cleanup_properly(self):
r = Replacer()
try:
m = Mock()
d = mkdtemp()
m.return_value = d
r.replace('testfixtures.tempdirectory.mkdtemp',m)
self.failUnless(os.path.exists(d))
self.assertFalse(m.called)
@tempdir()
def test_method(d):
d.write('something', b'stuff')
d.check('something', )
self.assertFalse(m.called)
compare(os.listdir(d),[])
test_method()
self.assertTrue(m.called)
self.failIf(os.path.exists(d))
finally:
r.restore()
if os.path.exists(d):
# only runs if the test fails!
rmtree(d) # pragma: no cover
示例4: test_replacer_del
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
def test_replacer_del(self):
r = Replacer()
r.replace('testfixtures.tests.sample1.left_behind',
object(), strict=False)
with catch_warnings(record=True) as w:
del r
self.assertTrue(len(w), 1)
compare(str(w[0].message),
"Replacer deleted without being restored, ""originals left:"
" {'testfixtures.tests.sample1.left_behind': <not_there>}")
示例5: SourceMixin
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
class SourceMixin(object):
def setUp(self):
def get_source(level=''):
return 'default_source'+str(level)
self.r = Replacer()
self.r.replace('configurator._api.get_source', get_source)
self.r.replace('configurator.section.get_source', get_source)
def tearDown(self):
self.r.restore()
示例6: SavedSearchFormTests
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
class SavedSearchFormTests(TestCase):
def setUp(self):
super(SavedSearchFormTests, self).setUp()
self.user = UserFactory()
self.data = {'url': 'http://www.my.jobs/jobs',
'feed': 'http://www.my.jobs/jobs/feed/rss?',
'email': '[email protected]',
'frequency': 'D',
'label': 'All jobs from www.my.jobs',
'sort_by': 'Relevance'}
self.r = Replacer()
self.r.replace('urllib2.urlopen', return_file)
def tearDown(self):
self.r.restore()
def test_successful_form(self):
form = SavedSearchForm(user=self.user,data=self.data)
self.assertTrue(form.is_valid())
def test_invalid_url(self):
self.data['url'] = 'http://google.com'
form = SavedSearchForm(user=self.user,data=self.data)
self.assertFalse(form.is_valid())
self.assertEqual(form.errors['url'][0], 'This URL is not valid.')
def test_day_of_week(self):
self.data['frequency'] = 'W'
form = SavedSearchForm(user=self.user,data=self.data)
self.assertFalse(form.is_valid())
self.assertEqual(form.errors['day_of_week'][0], 'This field is required.')
self.data['day_of_week'] = '1'
form = SavedSearchForm(user=self.user,data=self.data)
self.assertTrue(form.is_valid())
def test_day_of_month(self):
self.data['frequency'] = 'M'
form = SavedSearchForm(user=self.user,data=self.data)
self.assertFalse(form.is_valid())
self.assertEqual(form.errors['day_of_month'][0], 'This field is required.')
self.data['day_of_month'] = '1'
form = SavedSearchForm(user=self.user,data=self.data)
self.assertTrue(form.is_valid())
def test_duplicate_url(self):
original = SavedSearchFactory(user=self.user)
form = SavedSearchForm(user=self.user,data=self.data)
self.assertFalse(form.is_valid())
self.assertEqual(form.errors['url'][0], 'URL must be unique.')
示例7: test_remove_called_twice
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
def test_remove_called_twice(self):
from testfixtures.tests import sample1
def test_z(): pass
r = Replacer()
r.replace('testfixtures.tests.sample1.z',test_z)
r.restore()
assert sample1.z() == 'original z'
r.restore()
assert sample1.z() == 'original z'
示例8: test_import_and_obtain_with_lists
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
def test_import_and_obtain_with_lists(self):
t = test_datetime(None)
t.add(2002, 1, 1, 1, 0, 0)
t.add(2002, 1, 1, 2, 0, 0)
from testfixtures import Replacer
r = Replacer()
r.replace('testfixtures.tests.sample1.now', t.now)
try:
compare(sample1.str_now_2(), '2002-01-01 01:00:00')
compare(sample1.str_now_2(), '2002-01-01 02:00:00')
finally:
r.restore()
示例9: setUp
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
def setUp(self):
r = Replacer()
self.addCleanup(r.restore)
self.mock = Mock()
self.mock.input.return_value = 'u'
self.mock.getpass.return_value = 'p'
r.replace('workfront.get_api_key.input', self.mock.input)
r.replace('getpass.getpass', self.mock.getpass)
r.replace('logging.basicConfig', self.mock.logging)
r.replace('sys.argv', ['x'])
self.server = MockOpen(
'https://api-cl01.attask-ondemand.com/attask/api/unsupported'
)
r.replace('workfront.six.moves.urllib.request.urlopen', self.server)
示例10: test_import_and_obtain_with_lists
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
def test_import_and_obtain_with_lists(self):
t = test_date(None)
t.add(2002, 1, 1)
t.add(2002, 1, 2)
from testfixtures import Replacer
r = Replacer()
r.replace('testfixtures.tests.sample1.today', t.today)
try:
compare(sample1.str_today_2(), '2002-01-01')
compare(sample1.str_today_2(), '2002-01-02')
finally:
r.restore()
示例11: PopenCommandsTest
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
class PopenCommandsTest(unittest.TestCase):
def setUp(self):
self.Popen = MockPopen()
self.r = Replacer()
self.r.replace('swabbie.utils.command.subprocess.Popen', self.Popen)
self.addCleanup(self.r.restore)
def test_call_cmd(self):
self.Popen.set_command('cmd1', stdout='foo')
command_result = Command._call_cmd('cmd1')
expected_command_result = CommandResult(output='foo', return_code=0)
self.assertEqual(command_result.output, expected_command_result.output)
self.assertEqual(command_result.return_code, expected_command_result.return_code)
self.assertFalse(command_result.err)
示例12: test_function
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
def test_function(self):
from testfixtures.tests import sample1
assert sample1.z() == 'original z'
def test_z():
return 'replacement z'
r = Replacer()
r.replace('testfixtures.tests.sample1.z',test_z)
assert sample1.z() == 'replacement z'
r.restore()
assert sample1.z() == 'original z'
示例13: test_method
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
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'
示例14: test_class_method
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
def test_class_method(self):
from testfixtures.tests import sample1
c = sample1.X
assert sample1.X.aMethod() is c
def rMethod(cls):
return cls, 1
r = Replacer()
r.replace('testfixtures.tests.sample1.X.aMethod',rMethod)
sample1.X.aMethod()
assert sample1.X.aMethod() == (c, 1)
r.restore()
sample1.X.aMethod()
assert sample1.X.aMethod() is c
示例15: test_class
# 需要导入模块: from testfixtures import Replacer [as 别名]
# 或者: from testfixtures.Replacer import replace [as 别名]
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'