🔍 Code Extractor

function create_test_file

Maturity: 37

Creates a temporary test file with specified content and filename in a temporary directory.

File:
/tf/active/vicechatdev/SPFCsync/test_upload_modalities.py
Lines:
27 - 35
Complexity:
simple

Purpose

This utility function is designed for testing purposes, particularly for file upload operations. It creates a temporary file in a system-generated temporary directory, writes the provided content to it, and returns the full file path. This is useful for unit tests, integration tests, or any scenario where temporary test files are needed without polluting the main file system.

Source Code

def create_test_file(content="Test file content", filename="test_upload.txt"):
    """Create a temporary test file"""
    temp_dir = tempfile.mkdtemp()
    file_path = os.path.join(temp_dir, filename)
    
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(content)
    
    return file_path

Parameters

Name Type Default Kind
content - 'Test file content' positional_or_keyword
filename - 'test_upload.txt' positional_or_keyword

Parameter Details

content: String content to write into the test file. Defaults to 'Test file content'. Can be any string value including multi-line text. The content is written with UTF-8 encoding.

filename: Name of the file to create in the temporary directory. Defaults to 'test_upload.txt'. Should include the file extension. Can be any valid filename string.

Return Value

Returns a string representing the absolute file path to the created temporary test file. The path includes the temporary directory path and the specified filename (e.g., '/tmp/tmpXXXXXX/test_upload.txt' on Unix systems). The file exists on disk and contains the specified content when the function returns.

Dependencies

  • tempfile
  • os

Required Imports

import tempfile
import os

Usage Example

import os
import tempfile

def create_test_file(content="Test file content", filename="test_upload.txt"):
    """Create a temporary test file"""
    temp_dir = tempfile.mkdtemp()
    file_path = os.path.join(temp_dir, filename)
    
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(content)
    
    return file_path

# Example 1: Create a test file with default content
file_path = create_test_file()
print(f"Created test file at: {file_path}")

# Example 2: Create a test file with custom content
custom_content = "This is my custom test content\nWith multiple lines"
file_path = create_test_file(content=custom_content, filename="my_test.txt")
print(f"Created custom test file at: {file_path}")

# Example 3: Use in a test scenario
test_data = "Sample data for upload testing"
test_file = create_test_file(content=test_data, filename="upload_test.csv")
# ... perform upload test ...
# Clean up after test
import shutil
shutil.rmtree(os.path.dirname(test_file))

Best Practices

  • Remember to clean up the temporary directory after use. The function creates a new temporary directory each time it's called, which is not automatically deleted. Use shutil.rmtree(os.path.dirname(file_path)) to remove the directory and its contents.
  • The function uses UTF-8 encoding for file writing. Ensure your test content is compatible with UTF-8 encoding.
  • Each call to this function creates a new temporary directory, so multiple calls will create multiple directories. Consider reusing the same directory for multiple test files if needed.
  • The temporary directory location depends on the system's temporary directory settings (typically /tmp on Unix, C:\Temp on Windows).
  • This function is intended for testing purposes only and should not be used in production code for creating permanent files.
  • Consider using context managers or pytest fixtures to ensure proper cleanup of temporary files in test suites.

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_filecloud_operations 55.9% similar

    Tests FileCloud basic operations by creating a test folder to verify connectivity and authentication with a FileCloud server.

    From: /tf/active/vicechatdev/SPFCsync/test_connections.py
  • function test_upload_modalities 49.4% similar

    Integration test function that validates FileCloud upload functionality by testing both new file creation and existing file update scenarios.

    From: /tf/active/vicechatdev/SPFCsync/test_upload_modalities.py
  • function ensure_dir_exists 49.3% similar

    Creates a directory and all necessary parent directories if they don't already exist, with logging support.

    From: /tf/active/vicechatdev/msg_to_eml.py
  • function file_cleanup 49.0% similar

    Removes files older than 60 seconds from the './static/files/' directory.

    From: /tf/active/vicechatdev/datacapture_integrated.py
  • function create_folder 47.9% similar

    Creates a nested folder structure on a FileCloud server by traversing a path and creating missing directories.

    From: /tf/active/vicechatdev/filecloud_wuxi_sync.py
← Back to Browse