@@ -304,12 +304,12 @@ def test_output_format_options(
304304 assert output_path .exists ()
305305
306306 # Verify the file was saved with correct PIL format
307- saved_image = Image .open (output_path )
308- expected_pil_formats = {"png" : "PNG" , "jpg" : "JPEG" , "webp" : "WEBP" }
309- expected_format = expected_pil_formats [fmt ]
310- assert (
311- saved_image .format == expected_format
312- ), f"Expected { expected_format } , got { saved_image .format } "
307+ with Image .open (output_path ) as saved_image :
308+ expected_pil_formats = {"png" : "PNG" , "jpg" : "JPEG" , "webp" : "WEBP" }
309+ expected_format = expected_pil_formats [fmt ]
310+ assert (
311+ saved_image .format == expected_format
312+ ), f"Expected { expected_format } , got { saved_image .format } "
313313
314314 @patch ("src.withoutbg.cli.WithoutBG" )
315315 def test_jpeg_quality_setting (
@@ -604,8 +604,8 @@ def test_rgba_to_jpg_conversion(self, mock_withoutbg_class, temp_dir):
604604 assert output_path .exists ()
605605
606606 # Verify the saved image is RGB (not RGBA)
607- saved_image = Image .open (output_path )
608- assert saved_image .mode == "RGB"
607+ with Image .open (output_path ) as saved_image :
608+ assert saved_image .mode == "RGB"
609609
610610 def test_directory_as_input_without_batch_flag (self , temp_dir ):
611611 """Test providing directory as input without --batch flag."""
@@ -650,9 +650,9 @@ def create_test_image(path, size=(100, 100), color=(255, 0, 0), mode="RGB"):
650650 assert image_path .exists ()
651651
652652 # Verify image properties
653- image = Image .open (image_path )
654- assert image .size == (100 , 100 )
655- assert image .mode == "RGB"
653+ with Image .open (image_path ) as image :
654+ assert image .size == (100 , 100 )
655+ assert image .mode == "RGB"
656656
657657 def test_create_corrupted_file (self , temp_dir ):
658658 """Test utility for creating corrupted image files."""
@@ -679,30 +679,29 @@ def verify_output_properties(
679679 """Verify properties of output image file."""
680680 assert output_path .exists (), f"Output file { output_path } does not exist"
681681
682- image = Image .open (output_path )
683-
684- if expected_format :
685- assert image .format .lower () == expected_format .lower ()
686- if expected_mode :
687- assert image .mode == expected_mode
688- if expected_size :
689- assert image .size == expected_size
690-
691- return image
682+ with Image .open (output_path ) as image :
683+ if expected_format :
684+ assert image .format .lower () == expected_format .lower ()
685+ if expected_mode :
686+ assert image .mode == expected_mode
687+ if expected_size :
688+ assert image .size == expected_size
689+
690+ return image .size # Return size instead of image object
692691
693692 # Create test output file
694693 test_image = Image .new ("RGBA" , (200 , 150 ), color = (255 , 0 , 0 , 128 ))
695694 output_path = temp_dir / "output.png"
696695 test_image .save (output_path )
697696
698697 # Test verification
699- verified_image = verify_output_properties (
698+ verified_size = verify_output_properties (
700699 output_path ,
701700 expected_format = "PNG" ,
702701 expected_mode = "RGBA" ,
703702 expected_size = (200 , 150 ),
704703 )
705- assert verified_image . size == (200 , 150 )
704+ assert verified_size == (200 , 150 )
706705
707706
708707class TestCLIPerformance :
0 commit comments