-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Description
Currently, most tests in test/test_transforms.py rely on unittest.TestCase
. Now that we support pytest
, we want to remove the use of the unittest
module.
For a similar issue see #3956
Instructions
There are many tests in this file, so I bundled them in multiple related groups below. If you're interested in working on this issue, please comment below with "I'm working on <group X>
, <group Y>
, etc..." so that others don't pick the same tests as you do. Feel free to pick as many groups as you wish, but please don't submit more than 2 groups per PR in order to keep the reviews manageable. Before picking a group, make sure it wasn't picked by another contributor first. Thanks!!
How to port a test to pytest
Porting a test from unittest
to pytest is usually fairly straightforward. For a typical example, see https://github.com/pytorch/vision/pull/3907/files:
- take the test method out of the
Tester(unittest.TestCase)
class and just declare it as a function - Replace
@unittest.skipIf
withpytest.mark.skipif(cond, reason=...)
- remove any use of
self.assertXYZ
.- Typically
assertEqual(a, b)
can be replaced byassert a == b
when a and b are pure python objects (scalars, tuples, lists), and otherwise we can rely onassert_equal
which is already used in the file. self.assertRaises
should be replaced with thepytest.raises(Exp, match=...):
context manager, as done in https://github.com/pytorch/vision/pull/3907/files. Same for warnings withpytest.warns
self.assertTrue
should be replaced with a plainassert
- Typically
- When a function uses for loops to tests multiple parameter values, one should use
pytest.mark.parametrize
instead, as done e.g. in https://github.com/pytorch/vision/pull/3907/files. - It may make sense to keep related tests within a single class. For example here, the tests in group A could be grouped into a
TestToPILImage
class, the tests in group N could be inTestPad
, etc. Not all groups need a dedicated class though, it's on a case-by-case basis.
CC @ShrillShrestha and @zhiqwang in case you're interested in this too :)
Groups:
-
Groups A: Port Group A test_transforms.py to pytest #3959
test_1_channel_ndarray_to_pil_image
test_1_channel_tensor_to_pil_image
test_2_channel_ndarray_to_pil_image
test_2_channel_tensor_to_pil_image
test_2d_ndarray_to_pil_image
test_2d_tensor_to_pil_image
test_3_channel_ndarray_to_pil_image
test_3_channel_tensor_to_pil_image
test_4_channel_ndarray_to_pil_image
test_4_channel_tensor_to_pil_image
test_ndarray_bad_types_to_pil_image
test_tensor_bad_types_to_pil_image
-
Group B port test_accimage_xxx in test_transforms to pytest #3985
test_accimage_crop
test_accimage_pil_to_tensor
test_accimage_resize
test_accimage_to_tensor
-
Group C Port affine transform tests in test_tranforms to pytest #3984
test_affine
test_random_affine
-
Group D Group G,Group D #3981
test_autoaugment
test_center_crop
test_center_crop_2
test_color_jitter
-
Group E Group E test_transforms.py port to pytest #4026
test_convert_image_dtype_float_to_float
test_convert_image_dtype_float_to_int
test_convert_image_dtype_int_to_float
test_convert_image_dtype_int_to_int
test_convert_image_dtype_int_to_int_consistency
-
Group F Port to_tensor tests in test_transforms to pytest #3966
test_pil_to_tensor
test_to_tensor
test_to_tensor_with_other_default_dtypes
-
Group G Group G,Group D #3981
test_five_crop
test_ten_crop
test_max_value
test_linear_transformation
-
Group H Port group H test_transforms.py to pytest #3964
test_random_apply
test_random_choice
test_random_order
-
Group I Port some test_random_xxx in test_transforms to pytest #3972
test_random_crop
test_random_erasing
test_random_rotation
test_randomperspective
test_randomperspective_fill
-
Group J Port test resize in test_transforms to pytest #3952
test_randomresized_params
test_resize
-
Group K port tests in test_transforms to pytest #3957
test_rotate
test_rotate_fill
test_gaussian_blur_asserts
test_lambda
-
Group L Refactor test random horizontal flip #3971
test_random_horizontal_flip
test_random_vertical_flip
test_normalize
test_normalize_3d_tensor
test_normalize_different_dtype
-
Group M Port grayscale tests in test_tranforms to pytest #3962
test_to_grayscale
test_random_grayscale
-
Group N Port test pad in test_transforms to pytest #3954
test_pad
test_pad_raises_with_invalid_pad_sequence_len
test_pad_with_mode_F_images
test_pad_with_non_constant_padding_modes
test_pad_with_tuple_of_pad_values
-
Group O Port Group O test to pytest ref #3945 #3955
test_random_invert
test_random_posterize
test_random_solarize
test_random_adjust_sharpness
test_random_autocontrast
test_random_equalize
-
Group P port adjusts in test_transforms to pytest #3950
test_adjust_brightness
test_adjust_contrast
test_adjust_gamma
test_adjust_hue
test_adjust_saturation
test_adjust_sharpness
test_adjusts_L_mode
cc @pmeier