本文整理汇总了Python中testfixtures.Replacer类的典型用法代码示例。如果您正苦于以下问题:Python Replacer类的具体用法?Python Replacer怎么用?Python Replacer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Replacer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cleanup_properly
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
示例2: TestCommandTaskWithMockPopen
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"]))
示例3: setUp
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)
示例4: test_replacer_del
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: SavedSearchFormTests
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.')
示例6: test_import_and_obtain_with_lists
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()
示例7: test_import_and_obtain_with_lists
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()
示例8: PopenCommandsTest
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)
示例9: test_function
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'
示例10: test_method
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'
示例11: setUp
def setUp(self):
super(MySearchViewTests, self).setUp()
self.client = TestClient()
self.user = UserFactory()
self.client.login_user(self.user)
self.new_form_data = {
'url': 'www.my.jobs/jobs',
'feed': 'http://www.my.jobs/jobsfeed/rss?',
'label': 'Jobs Label',
'email': self.user.email,
'frequency': 'D',
'is_active': 'True',
'sort_by': 'Relevance',
}
self.new_digest_data = {
'is_active': 'True',
'user': self.user,
'email': self.user.email,
'frequency': 'M',
'day_of_month': 1,
}
self.new_form = forms.SavedSearchForm(user=self.user,
data=self.new_form_data)
self.r = Replacer()
self.r.replace('urllib2.urlopen', return_file)
示例12: setUp
def setUp(self):
self.redirect_guid = JOB['guid']
self.redirect = RedirectFactory(buid=JOB['buid'],
guid='{%s}' %
uuid.UUID(self.redirect_guid))
self.password = 'secret'
self.user = User.objects.create(email='[email protected]')
self.user.set_password(self.password)
self.user.save()
self.contact = CompanyEmail.objects.create(
buid=self.redirect.buid,
email=self.user.email)
self.email = self.user.email.replace('@', '%40')
self.auth = {
'bad': [
'',
'Basic %s' % base64.b64encode('bad%40email:wrong_pass')],
'good':
'Basic %s' % base64.b64encode('%s:%s' % (self.user.email.\
replace('@', '%40'),
self.password))}
self.post_dict = {'to': '[email protected]',
'from': '[email protected]',
'text': 'This address does not contain a valid guid',
'html': '',
'subject': 'Bad Email',
'attachments': 0}
self.r = Replacer()
self.r.replace('pysolr.Solr.search', mock_search)
示例13: setUp
def setUp(self):
self.r = Replacer()
# test values for author info, requiring less setup
self.r.replace('opencore.views.people.get_author_info',
lambda id,request: {
'title' : 'Author title',
'country' : 'Author country',
'organization' : 'author org',
'url' : 'author-url',
'photo_url' : 'author-photo-url',
})
self.r.replace('opencore.views.forms.get_current_request',
lambda :self.request)
self.r.replace('opencore.views.forms.authenticated_userid',
lambda request:'auth_user_id')
testing.cleanUp()
sessions = DummySessions()
context = DummyProfile(sessions=sessions, **profile_data)
context.title = 'title'
context.__name__='admin'
context.users = DummyUsers()
context.users.add('admin','admin','password',())
self.context = context
request = testing.DummyRequest()
request.api = get_template_api(context, request)
request.context = context
self.request = request
示例14: setUp
def setUp(self):
super(SavedSearchHelperTests, self).setUp()
self.user = UserFactory()
self.valid_url = 'http://www.my.jobs/search?location=chicago&q=nurse'
self.r = Replacer()
self.r.replace('urllib2.urlopen', return_file)
示例15: setUp
def setUp(self):
self.replacer = Replacer()
self.mock_a = MagicMock()
self.mock_b = MagicMock()
self.replacer.replace('test_hello.RealClassA', self.mock_a)
self.replacer.replace('test_hello.RealClassB', self.mock_b)