本文整理汇总了Python中unittest2.mock方法的典型用法代码示例。如果您正苦于以下问题:Python unittest2.mock方法的具体用法?Python unittest2.mock怎么用?Python unittest2.mock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest2
的用法示例。
在下文中一共展示了unittest2.mock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def setUp(self):
# disable logging while testing
logging.disable(logging.CRITICAL)
self.patched = {}
if hasattr(self, 'patch_these'):
for patch_this in self.patch_these:
namespace = patch_this[0] if isinstance(patch_this, (list, set)) else patch_this
patcher = mock.patch(namespace)
mocked = patcher.start()
mocked.reset_mock()
self.patched[namespace] = mocked
if isinstance(patch_this, (list, set)) and len(patch_this) > 0:
retval = patch_this[1]
if callable(retval):
retval = retval()
mocked.return_value = retval
示例2: test_mocked
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def test_mocked(self):
poly_oi = ia.PolygonsOnImage(
[ia.Polygon([(1, 1), (8, 1), (8, 9), (1, 9)]),
ia.Polygon([(1, 1), (15, 1), (15, 9), (1, 9)])],
shape=(10, 11, 3))
mock_sub = mock.Mock()
mock_sub.return_value = "foo"
poly_oi.items[0].subdivide_ = mock_sub
poly_oi.items[1].subdivide_ = mock_sub
poly_oi_sub = self._func(poly_oi, 2)
assert mock_sub.call_count == 2
assert mock_sub.call_args_list[0][0][0] == 2
assert mock_sub.call_args_list[1][0][0] == 2
assert poly_oi_sub.items == ["foo", "foo"]
示例3: test_without_seed_start_simulate_py36_or_lower
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def test_without_seed_start_simulate_py36_or_lower(self,
mock_cp,
mock_ia_seed,
mock_time,
mock_vi):
def version_info(index):
return 3 if index == 0 else 6
mock_vi.__getitem__.side_effect = version_info
mock_time.return_value = 1
mock_cp.return_value = mock.MagicMock()
mock_cp.return_value.name = "foo"
augseq = mock.MagicMock()
multicore._Pool_initialize_worker(augseq, None)
assert mock_time.call_count == 1
assert mock_ia_seed.call_count == 1
assert augseq.seed_.call_count == 1
seed_global = mock_ia_seed.call_args_list[0][0][0]
seed_local = augseq.seed_.call_args_list[0][0][0]
assert seed_global != seed_local
示例4: test_n_colors_tuple
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def test_n_colors_tuple(self):
aug = self.augmenter(n_colors=(2, 1000))
mock_quantize_func = mock.MagicMock(
return_value=np.zeros((4, 4, 3), dtype=np.uint8))
n_images = 10
fname = self.quantization_func_name
with mock.patch(fname, mock_quantize_func):
image = np.zeros((4, 4, 3), dtype=np.uint8)
_ = aug.augment_images([image] * n_images)
# call i, args, argument 1
n_colors = [mock_quantize_func.call_args_list[i][0][1]
for i in sm.xrange(n_images)]
assert all([2 <= n_colors_i <= 1000 for n_colors_i in n_colors])
assert len(set(n_colors)) > 1
示例5: test_from_colorspace
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def test_from_colorspace(self):
def _noop(img):
return img
aug = self.augmenter(from_colorspace="BGR")
mock_change_colorspace = mock.MagicMock()
mock_change_colorspace.return_value = mock_change_colorspace
mock_change_colorspace.augment_image.side_effect = _noop
mock_change_colorspace._draw_samples.return_value = (None, ["foo"])
fname = "imgaug.augmenters.color.ChangeColorspace"
with mock.patch(fname, mock_change_colorspace):
_ = aug.augment_image(np.zeros((4, 4, 3), dtype=np.uint8))
# call 0, kwargs, argument 'from_colorspace'
assert (
mock_change_colorspace.call_args_list[0][1]["from_colorspace"]
== "BGR")
# call 1, kwargs, argument 'from_colorspace' (inverse transform)
assert (
mock_change_colorspace.call_args_list[1][1]["from_colorspace"]
== "foo")
示例6: _test_image_with_n_channels
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def _test_image_with_n_channels(cls, nb_channels):
image = np.zeros((10, 20), dtype=np.uint8)
if nb_channels is not None:
image = image[..., np.newaxis]
image = np.tile(image, (1, 1, nb_channels))
sampler = iaa.RegularGridPointsSampler(1, 1)
aug = iaa.Voronoi(sampler)
mock_segment_voronoi = mock.MagicMock()
if nb_channels is None:
mock_segment_voronoi.return_value = image[..., np.newaxis]
else:
mock_segment_voronoi.return_value = image
fname = "imgaug.augmenters.segmentation.segment_voronoi"
with mock.patch(fname, mock_segment_voronoi):
image_aug = aug(image=image)
assert image_aug.shape == image.shape
示例7: test_backends_called
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def test_backends_called(self):
def side_effect_cv2(image, ksize, sigmaX, sigmaY, borderType):
return image + 1
def side_effect_scipy(image, sigma, mode):
return image + 1
mock_GaussianBlur = mock.Mock(side_effect=side_effect_cv2)
mock_gaussian_filter = mock.Mock(side_effect=side_effect_scipy)
image = np.arange(4*4).astype(np.uint8).reshape((4, 4))
with mock.patch('cv2.GaussianBlur', mock_GaussianBlur):
_observed = iaa.blur_gaussian_(
np.copy(image), sigma=1.0, eps=0, backend="cv2")
assert mock_GaussianBlur.call_count == 1
with mock.patch('scipy.ndimage.gaussian_filter', mock_gaussian_filter):
_observed = iaa.blur_gaussian_(
np.copy(image), sigma=1.0, eps=0, backend="scipy")
assert mock_gaussian_filter.call_count == 1
示例8: test_draw_samples
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def test_draw_samples(self):
aug = iaa.MeanShiftBlur(
spatial_radius=[1.0, 2.0, 3.0],
color_radius=(1.0, 2.0)
)
batch = mock.Mock()
batch.nb_rows = 100
samples = aug._draw_samples(batch, iarandom.RNG(0))
assert np.all(
np.isclose(samples[0], 1.0)
| np.isclose(samples[0], 2.0)
| np.isclose(samples[0], 3.0)
)
assert np.all((1.0 <= samples[1]) | (samples[1] <= 2.0))
示例9: test_basic_functionality_3d
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def test_basic_functionality_3d(self):
# image with three channels
img = [
[99, 100, 101],
[99, 100, 101],
[99, 100, 101]
]
img = np.uint8(img)
img3d = np.tile(img[:, :, np.newaxis], (1, 1, 3))
img3d[..., 1] += 1
img3d[..., 2] += 2
aug = iaa.AllChannelsCLAHE(clip_limit=20, tile_grid_size_px=17)
mock_clahe = ArgCopyingMagicMock()
mock_clahe.apply.return_value = img
with mock.patch('cv2.createCLAHE') as mock_createCLAHE:
mock_createCLAHE.return_value = mock_clahe
_ = aug.augment_image(img3d)
clist = mock_clahe.apply.call_args_list
assert np.array_equal(clist[0][0][0], img3d[..., 0])
assert np.array_equal(clist[1][0][0], img3d[..., 1])
assert np.array_equal(clist[2][0][0], img3d[..., 2])
示例10: test_unit_sized_kernels
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def test_unit_sized_kernels(self):
img = np.zeros((1, 1), dtype=np.uint8)
tile_grid_sizes = [0, 0, 0, 1, 1, 1, 3, 3, 3]
tile_grid_min_sizes = [0, 1, 3, 0, 1, 3, 0, 1, 3]
nb_calls_expected = [0, 0, 1, 0, 0, 1, 1, 1, 1]
gen = zip(tile_grid_sizes, tile_grid_min_sizes, nb_calls_expected)
for tile_grid_size_px, tile_grid_size_px_min, nb_calls_exp_i in gen:
with self.subTest(tile_grid_size_px=tile_grid_size_px,
tile_grid_size_px_min=tile_grid_size_px_min,
nb_calls_expected_i=nb_calls_exp_i):
aug = iaa.AllChannelsCLAHE(
clip_limit=20,
tile_grid_size_px=tile_grid_size_px,
tile_grid_size_px_min=tile_grid_size_px_min)
mock_clahe = mock.Mock()
mock_clahe.apply.return_value = img
mock_createCLAHE = mock.MagicMock(return_value=mock_clahe)
with mock.patch('cv2.createCLAHE', mock_createCLAHE):
_ = aug.augment_image(img)
assert mock_createCLAHE.call_count == nb_calls_exp_i
示例11: test_prefetches_at_second_call
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def test_prefetches_at_second_call(self):
other_param = mock.Mock()
other_param.draw_samples.return_value = np.zeros((100,), dtype=np.uint8)
param = iap.AutoPrefetcher(other_param, 10)
rng = iarandom.RNG(0)
_samples = param.draw_samples((1,), rng)
_samples = param.draw_samples((1,), rng)
# rng is currently not identical in call args,
# because draw_samples() creates a new one with same state
assert other_param.draw_samples.call_count == 2
assert other_param.draw_samples.call_args_list[0][0][0] == (1,)
assert other_param.draw_samples.call_args_list[0][0][1].equals(rng)
assert other_param.draw_samples.call_args_list[1][0][0] == (10,)
assert other_param.draw_samples.call_args_list[1][0][1].equals(rng)
# (100,) because that's what the mock always returns
assert param.samples.shape == (100,)
assert param.index == 1
assert param.last_rng_idx == rng._idx
示例12: test_standard_exponential_np117_mocked
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def test_standard_exponential_np117_mocked(self):
fname = "standard_exponential"
arr = np.zeros((1,), dtype="float16")
args = []
kwargs = {"size": (1,), "dtype": "float16", "method": "foo",
"out": arr}
mock_gen = mock.MagicMock()
getattr(mock_gen, fname).return_value = "foo"
rng = iarandom.RNG(0)
rng.generator = mock_gen
rng._is_new_rng_style = True
result = getattr(rng, fname)(*args, **kwargs)
assert result == "foo"
getattr(mock_gen, fname).assert_called_once_with(*args, **kwargs)
示例13: test_standard_gamma_np117_mocked
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def test_standard_gamma_np117_mocked(self):
fname = "standard_gamma"
arr = np.zeros((1,), dtype="float16")
args = []
kwargs = {"shape": 1.0, "size": (1,), "dtype": "float16", "out": arr}
mock_gen = mock.MagicMock()
getattr(mock_gen, fname).return_value = "foo"
rng = iarandom.RNG(0)
rng.generator = mock_gen
rng._is_new_rng_style = True
result = getattr(rng, fname)(*args, **kwargs)
assert result == "foo"
getattr(mock_gen, fname).assert_called_once_with(*args, **kwargs)
示例14: test_standard_normal_np117_mocked
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def test_standard_normal_np117_mocked(self):
fname = "standard_normal"
arr = np.zeros((1,), dtype="float16")
args = []
kwargs = {"size": (1,), "dtype": "float16", "out": arr}
mock_gen = mock.MagicMock()
getattr(mock_gen, fname).return_value = "foo"
rng = iarandom.RNG(0)
rng.generator = mock_gen
rng._is_new_rng_style = True
result = getattr(rng, fname)(*args, **kwargs)
assert result == "foo"
getattr(mock_gen, fname).assert_called_once_with(*args, **kwargs)
示例15: test_run
# 需要导入模块: import unittest2 [as 别名]
# 或者: from unittest2 import mock [as 别名]
def test_run(self):
self.runner.tests = {"A": None, "B": None, "C": None}
self.runner.tcpdump_running = True
analyse = mock.Mock()
analyse.return_value = 1
with mock.patch('__main__.__builtins__.open',
self._mock_open(read_data="A,B,C\r\n0,2,1\r\n2,0,1\r\n2,1,0\r\n")):
with mock.patch('tlsfuzzer.timing_runner.TimingRunner.sniff'):
with mock.patch('tlsfuzzer.timing_runner.TimingRunner.extract') as extract:
with mock.patch('tlsfuzzer.timing_runner.TimingRunner.analyse', analyse):
with mock.patch('tlsfuzzer.timing_runner.Thread'):
with mock.patch('tlsfuzzer.timing_runner.time.sleep'):
with mock.patch('tlsfuzzer.timing_runner.Runner') as runner:
ret = self.runner.run()
self.assertEqual(runner.call_count, WARM_UP + 9)
extract.assert_called_once()
analyse.assert_called_once()
self.assertEqual(ret, 1)