From 66625c5ea32dc3015318b8ed57d606c2f2f92db0 Mon Sep 17 00:00:00 2001 From: Terry Zhuo Date: Tue, 7 May 2024 22:32:50 +1000 Subject: [PATCH] fix: setup teardown class to method --- data/raw/f_615_xiaoheng.py | 17 +++++++---------- data/raw/f_618_xiaoheng.py | 6 ++---- data/raw/f_623_xiaoheng.py | 6 ++---- data/raw/f_663_xiaoheng.py | 18 ++++++++---------- data/raw/f_665_xiaoheng.py | 10 ++++------ data/raw/f_682_simon.py | 10 ++++------ data/raw/f_687_xiaoheng.py | 10 ++++------ data/raw/f_688_xiaoheng.py | 16 +++++++--------- 8 files changed, 38 insertions(+), 55 deletions(-) diff --git a/data/raw/f_615_xiaoheng.py b/data/raw/f_615_xiaoheng.py index dda95510..fc158692 100644 --- a/data/raw/f_615_xiaoheng.py +++ b/data/raw/f_615_xiaoheng.py @@ -44,23 +44,20 @@ def f_615(log_file_path: str, keywords: list): import shutil class TestCases(unittest.TestCase): - @classmethod - def setUpClass(cls): + def setUp(self): # Setup code to create a test log file - cls.test_file_path = "test_log_file.log" - with open(cls.test_file_path, 'w') as f: + self.test_file_path = "test_log_file.log" + with open(self.test_file_path, 'w') as f: f.write("ERROR 11:30:10 This is an error message\n") f.write("WARNING 11:35:10 This is a warning message\n") - @classmethod - def tearDownClass(cls): + def tearDown(self): # Cleanup the test directory and all its contents - shutil.rmtree(cls.test_dir) + shutil.rmtree(self.test_dir) - @classmethod - def tearDownClass(cls): + def tearDown(self): # Cleanup the test log file - os.remove(cls.test_file_path) + os.remove(self.test_file_path) def test_nonexistent_file(self): with self.assertRaises(FileNotFoundError): diff --git a/data/raw/f_618_xiaoheng.py b/data/raw/f_618_xiaoheng.py index 1f7a66e4..ef8214dd 100644 --- a/data/raw/f_618_xiaoheng.py +++ b/data/raw/f_618_xiaoheng.py @@ -49,14 +49,12 @@ def f_618(path_to_append=PATH_TO_APPEND, json_file=JSON_FILE): PATH_TO_TEMP_JSON = tempfile.mktemp(suffix='.json') class TestCases(unittest.TestCase): - @classmethod - def setUpClass(cls): + def setUp(self): # Create a temporary JSON file for tests that rely on the default JSON file with open(PATH_TO_TEMP_JSON, 'w') as f: json.dump({'initial_key': 'initial_value'}, f) - @classmethod - def tearDownClass(cls): + def tearDown(self): # Clean up the temporary JSON file after all tests have run os.remove(PATH_TO_TEMP_JSON) diff --git a/data/raw/f_623_xiaoheng.py b/data/raw/f_623_xiaoheng.py index c30c051b..f528ddbd 100644 --- a/data/raw/f_623_xiaoheng.py +++ b/data/raw/f_623_xiaoheng.py @@ -57,13 +57,11 @@ def run_tests(): runner.run(suite) class TestCases(unittest.TestCase): - @classmethod - def setUpClass(cls): + def setUp(self): """Create the directory for test files.""" os.makedirs(BASE_PATH, exist_ok=True) - @classmethod - def tearDownClass(cls): + def tearDown(self): """Remove all created test files and the directory after all tests.""" for filename in os.listdir(BASE_PATH): os.remove(os.path.join(BASE_PATH, filename)) diff --git a/data/raw/f_663_xiaoheng.py b/data/raw/f_663_xiaoheng.py index 72ebc3cd..8b615e27 100644 --- a/data/raw/f_663_xiaoheng.py +++ b/data/raw/f_663_xiaoheng.py @@ -44,16 +44,14 @@ def create_test_files(base_path): f.write("A" * 10**6) # 1MB file class TestCases(unittest.TestCase): - @classmethod - def setUpClass(cls): - cls.base_path = "f_663_data_xiaoheng" - create_test_files(cls.base_path) - - @classmethod - def tearDownClass(cls): - for item in os.listdir(cls.base_path): - os.remove(os.path.join(cls.base_path, item)) - os.rmdir(cls.base_path) + def setUp(self): + self.base_path = "f_663_data_xiaoheng" + create_test_files(self.base_path) + + def tearDown(self): + for item in os.listdir(self.base_path): + os.remove(os.path.join(self.base_path, item)) + os.rmdir(self.base_path) def test_file_properties(self): file_path = os.path.join(self.base_path, "large_file.txt") diff --git a/data/raw/f_665_xiaoheng.py b/data/raw/f_665_xiaoheng.py index 11dbad0c..b5f692b5 100644 --- a/data/raw/f_665_xiaoheng.py +++ b/data/raw/f_665_xiaoheng.py @@ -70,18 +70,16 @@ def run_tests(): TEST_FILES_DIR = './test_files' class TestCases(unittest.TestCase): - @classmethod - def setUpClass(cls): + def setUp(self): # Create a directory for test files if it doesn't exist os.makedirs(TEST_FILES_DIR, exist_ok=True) # Create some sample files - cls.sample_files = ['test1.txt', 'test2.txt', 'image1.jpg', 'image2.jpg'] - for file in cls.sample_files: + self.sample_files = ['test1.txt', 'test2.txt', 'image1.jpg', 'image2.jpg'] + for file in self.sample_files: with open(os.path.join(TEST_FILES_DIR, file), 'w') as f: f.write("Sample content for " + file) - @classmethod - def tearDownClass(cls): + def tearDown(self): # Remove the test directory after tests shutil.rmtree(TEST_FILES_DIR) diff --git a/data/raw/f_682_simon.py b/data/raw/f_682_simon.py index 63c945c4..25d89849 100644 --- a/data/raw/f_682_simon.py +++ b/data/raw/f_682_simon.py @@ -97,13 +97,11 @@ def run_tests(): class TestCases(unittest.TestCase): - @classmethod - def setUpClass(cls): - cls.temp_folder = tempfile.mkdtemp() + def setUp(self): + self.temp_folder = tempfile.mkdtemp() - @classmethod - def tearDownClass(cls): - shutil.rmtree(cls.temp_folder) + def tearDown(self): + shutil.rmtree(self.temp_folder) def test_case_1(self): result = f_682(['a', 'b', 'a', 'c', 'a'], 'a', self.temp_folder) diff --git a/data/raw/f_687_xiaoheng.py b/data/raw/f_687_xiaoheng.py index 4f3f3e9e..5f2456c6 100644 --- a/data/raw/f_687_xiaoheng.py +++ b/data/raw/f_687_xiaoheng.py @@ -60,15 +60,13 @@ def f_687(filename, dest_dir): import shutil class TestCases(unittest.TestCase): - @classmethod - def setUpClass(cls): + def setUp(self): # Create a temporary directory for the tests - cls.test_dir = tempfile.mkdtemp() + self.test_dir = tempfile.mkdtemp() - @classmethod - def tearDownClass(cls): + def tearDown(self): # Remove the temporary directory after the tests - shutil.rmtree(cls.test_dir) + shutil.rmtree(self.test_dir) def setUp(self): # Create a test file in the temporary directory diff --git a/data/raw/f_688_xiaoheng.py b/data/raw/f_688_xiaoheng.py index c23b7f65..2ec11748 100644 --- a/data/raw/f_688_xiaoheng.py +++ b/data/raw/f_688_xiaoheng.py @@ -56,18 +56,16 @@ def f_688(filename, data): class TestCases(unittest.TestCase): - @classmethod - def setUpClass(cls): + def setUp(self): """Create the test file with initial data.""" - cls.filename = 'data.json' - cls.data = {'key': 'value'} - with open(cls.filename, 'w') as file: - json.dump(cls.data, file) + self.filename = 'data.json' + self.data = {'key': 'value'} + with open(self.filename, 'w') as file: + json.dump(self.data, file) - @classmethod - def tearDownClass(cls): + def tearDown(self): """Remove the test file after all tests.""" - os.remove(cls.filename) + os.remove(self.filename) def test_empty_dict(self): """Test with an empty dictionary to ensure it writes and verifies correctly."""