-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
86 lines (66 loc) · 1.88 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""conftest"""
import os
import tempfile
import shutil
import pytest
SAMPLE_CODE = """
def sample_function(x):
return x * 2
class SampleClass:
def __init__(self, value):
self.value = value
def get_value(self):
return self.value
sample_variable = 42
def using_function():
result = sample_function(10)
obj = SampleClass(result)
return obj.get_value() + sample_variable
"""
@pytest.fixture
def sample_file():
"""Create a temporary file with sample code for testing."""
with tempfile.NamedTemporaryFile(suffix=".py", delete=False) as f:
f.write(SAMPLE_CODE.encode("utf-8"))
file_path = f.name
yield file_path
# Cleanup after tests
os.unlink(file_path)
@pytest.fixture
def multi_file_package():
"""Create a temporary package with multiple Python files for testing."""
# Create a temporary directory
package_dir = tempfile.mkdtemp()
# Create main.py
main_content = """
from utils import helper_function
from models import DataModel
def main_function():
model = DataModel("test")
result = helper_function(model)
return result
"""
with open(os.path.join(package_dir, "main.py"), "w", encoding="utf-8") as f:
f.write(main_content)
# Create utils.py
utils_content = """
def helper_function(data):
return process_data(data)
def process_data(data):
return data.get_value() * 2
"""
with open(os.path.join(package_dir, "utils.py"), "w", encoding="utf-8") as f:
f.write(utils_content)
# Create models.py
models_content = """
class DataModel:
def __init__(self, value):
self.value = value
def get_value(self):
return len(self.value)
"""
with open(os.path.join(package_dir, "models.py"), "w", encoding="utf-8") as f:
f.write(models_content)
yield package_dir
# Cleanup
shutil.rmtree(package_dir)