Skip to content

Commit

Permalink
fix: setup teardown class to method
Browse files Browse the repository at this point in the history
  • Loading branch information
terryyz committed May 7, 2024
1 parent 6850246 commit 66625c5
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 55 deletions.
17 changes: 7 additions & 10 deletions data/raw/f_615_xiaoheng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 2 additions & 4 deletions data/raw/f_618_xiaoheng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 2 additions & 4 deletions data/raw/f_623_xiaoheng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
18 changes: 8 additions & 10 deletions data/raw/f_663_xiaoheng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 4 additions & 6 deletions data/raw/f_665_xiaoheng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 4 additions & 6 deletions data/raw/f_682_simon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions data/raw/f_687_xiaoheng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions data/raw/f_688_xiaoheng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit 66625c5

Please sign in to comment.