Stp2Glb/test_wrapper.py
sladro 8218502ee8 chore: import existing STP2GLB project
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
2025-10-22 14:36:07 +08:00

209 lines
6.5 KiB
Python

#!/usr/bin/env python3
"""
测试Python包装器功能
"""
import os
import sys
import subprocess
import tempfile
import shutil
def test_help():
"""测试帮助信息"""
print("测试 --help 参数...")
result = subprocess.run([sys.executable, 'main.py', '--help'],
capture_output=True, text=True)
if result.returncode == 0:
print("✓ 帮助信息显示正常")
return True
else:
print("✗ 帮助信息显示失败")
print(result.stderr)
return False
def test_missing_args():
"""测试缺少参数的情况"""
print("测试缺少参数...")
result = subprocess.run([sys.executable, 'main.py'],
capture_output=True, text=True)
if result.returncode != 0:
print("✓ 正确处理缺少参数的情况")
return True
else:
print("✗ 未能正确处理缺少参数")
return False
def test_invalid_input():
"""测试无效输入文件"""
print("测试无效输入文件...")
result = subprocess.run([sys.executable, 'main.py', 'nonexistent.stp', 'output.glb'],
capture_output=True, text=True)
if result.returncode != 0 and "输入文件不存在" in result.stdout:
print("✓ 正确处理无效输入文件")
return True
else:
print("✗ 未能正确处理无效输入文件")
print(result.stdout)
return False
def test_invalid_output():
"""测试无效输出文件扩展名"""
print("测试无效输出文件扩展名...")
# 创建临时输入文件
with tempfile.NamedTemporaryFile(suffix='.stp', delete=False) as tmp_input:
tmp_input.write(b"dummy content")
tmp_input_path = tmp_input.name
try:
result = subprocess.run([sys.executable, 'main.py', tmp_input_path, 'output.txt'],
capture_output=True, text=True)
if result.returncode != 0 and "输出文件必须是.glb格式" in result.stdout:
print("✓ 正确处理无效输出文件扩展名")
return True
else:
print("✗ 未能正确处理无效输出文件扩展名")
print(result.stdout)
return False
finally:
os.unlink(tmp_input_path)
def test_quality_presets():
"""测试质量预设"""
print("测试质量预设...")
# 创建临时输入文件
with tempfile.NamedTemporaryFile(suffix='.stp', delete=False) as tmp_input:
tmp_input.write(b"dummy content")
tmp_input_path = tmp_input.name
# 创建临时输出目录
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_output_path = os.path.join(tmp_dir, 'output.glb')
try:
# 测试不同质量等级
for quality in ['low', 'medium', 'high', 'ultra']:
print(f" 测试质量等级: {quality}")
result = subprocess.run([
sys.executable, 'main.py',
tmp_input_path, tmp_output_path,
'--quality', quality
], capture_output=True, text=True)
# 应该失败(因为没有找到可执行文件),但参数解析应该成功
if "未找到STP2GLB.exe可执行文件" in result.stdout:
print(f"{quality} 质量等级参数解析正常")
else:
print(f"{quality} 质量等级参数解析异常")
print(result.stdout)
return False
return True
finally:
os.unlink(tmp_input_path)
def test_custom_quality():
"""测试自定义质量参数"""
print("测试自定义质量参数...")
# 创建临时输入文件
with tempfile.NamedTemporaryFile(suffix='.stp', delete=False) as tmp_input:
tmp_input.write(b"dummy content")
tmp_input_path = tmp_input.name
# 创建临时输出目录
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_output_path = os.path.join(tmp_dir, 'output.glb')
try:
# 测试custom质量但没有指定参数
result = subprocess.run([
sys.executable, 'main.py',
tmp_input_path, tmp_output_path,
'--quality', 'custom'
], capture_output=True, text=True)
if result.returncode != 0 and "必须指定--linear-deflection或--angular-deflection" in result.stdout:
print(" ✓ 正确处理custom质量缺少参数")
else:
print(" ✗ 未能正确处理custom质量缺少参数")
print(result.stdout)
return False
# 测试custom质量并指定参数
result = subprocess.run([
sys.executable, 'main.py',
tmp_input_path, tmp_output_path,
'--quality', 'custom',
'--linear-deflection', '0.05',
'--angular-deflection', '0.2'
], capture_output=True, text=True)
# 应该失败(因为没有找到可执行文件),但参数解析应该成功
if "未找到STP2GLB.exe可执行文件" in result.stdout:
print(" ✓ custom质量参数解析正常")
return True
else:
print(" ✗ custom质量参数解析异常")
print(result.stdout)
return False
finally:
os.unlink(tmp_input_path)
def main():
"""主测试函数"""
print("Python包装器功能测试")
print("=" * 40)
# 检查main.py是否存在
if not os.path.exists('main.py'):
print("错误: main.py 文件不存在")
return 1
tests = [
test_help,
test_missing_args,
test_invalid_input,
test_invalid_output,
test_quality_presets,
test_custom_quality,
]
passed = 0
total = len(tests)
for test_func in tests:
try:
if test_func():
passed += 1
print()
except Exception as e:
print(f"✗ 测试异常: {e}")
print()
print("=" * 40)
print(f"测试结果: {passed}/{total} 通过")
if passed == total:
print("✓ 所有测试通过!")
return 0
else:
print("✗ 部分测试失败")
return 1
if __name__ == '__main__':
sys.exit(main())