单元测试和改进
This commit is contained in:
146
tests/run_tests.py
Normal file
146
tests/run_tests.py
Normal file
@@ -0,0 +1,146 @@
|
||||
"""
|
||||
Test runner script for HeurAMS.
|
||||
|
||||
This script runs all unit tests and provides a summary report.
|
||||
"""
|
||||
import sys
|
||||
import pytest
|
||||
import os
|
||||
|
||||
|
||||
def run_tests():
|
||||
"""
|
||||
Run all unit tests and return the result.
|
||||
"""
|
||||
print("=" * 60)
|
||||
print("HEURAMS TEST SUITE")
|
||||
print("=" * 60)
|
||||
|
||||
# Add the src directory to Python path
|
||||
src_dir = os.path.join(os.path.dirname(__file__), "..", "src")
|
||||
sys.path.insert(0, src_dir)
|
||||
|
||||
# Run tests with verbose output
|
||||
test_args = [
|
||||
"-v", # Verbose output
|
||||
"--tb=short", # Short traceback format
|
||||
"--color=yes", # Color output
|
||||
"tests/" # Test directory
|
||||
]
|
||||
|
||||
print(f"Running tests from: {os.path.abspath('tests')}")
|
||||
print(f"Python path includes: {src_dir}")
|
||||
print()
|
||||
|
||||
# Run pytest
|
||||
exit_code = pytest.main(test_args)
|
||||
|
||||
print("=" * 60)
|
||||
if exit_code == 0:
|
||||
print("✅ ALL TESTS PASSED")
|
||||
else:
|
||||
print("❌ SOME TESTS FAILED")
|
||||
print("=" * 60)
|
||||
|
||||
return exit_code
|
||||
|
||||
|
||||
def run_specific_test(test_file=None, test_class=None, test_method=None):
|
||||
"""
|
||||
Run specific tests.
|
||||
|
||||
Args:
|
||||
test_file: Specific test file to run (e.g., "test_particles.py")
|
||||
test_class: Specific test class to run (e.g., "TestAtom")
|
||||
test_method: Specific test method to run (e.g., "test_atom_creation")
|
||||
"""
|
||||
# Add the src directory to Python path
|
||||
src_dir = os.path.join(os.path.dirname(__file__), "..", "src")
|
||||
sys.path.insert(0, src_dir)
|
||||
|
||||
test_args = [
|
||||
"-v", # Verbose output
|
||||
"--tb=short", # Short traceback format
|
||||
"--color=yes", # Color output
|
||||
]
|
||||
|
||||
# Build test path
|
||||
test_path = "tests/"
|
||||
if test_file:
|
||||
test_path = f"tests/{test_file}"
|
||||
if test_class:
|
||||
test_path += f"::{test_class}"
|
||||
if test_method:
|
||||
test_path += f"::{test_method}"
|
||||
|
||||
test_args.append(test_path)
|
||||
|
||||
print(f"Running specific test: {test_path}")
|
||||
print()
|
||||
|
||||
exit_code = pytest.main(test_args)
|
||||
return exit_code
|
||||
|
||||
|
||||
def run_examples():
|
||||
"""
|
||||
Run the examples to demonstrate functionality.
|
||||
"""
|
||||
# Add the src directory to Python path
|
||||
src_dir = os.path.join(os.path.dirname(__file__), "..", "src")
|
||||
sys.path.insert(0, src_dir)
|
||||
|
||||
try:
|
||||
from tests.examples import run_all_examples
|
||||
run_all_examples()
|
||||
return 0
|
||||
except Exception as e:
|
||||
print(f"Error running examples: {e}")
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description="HeurAMS Test Runner")
|
||||
parser.add_argument(
|
||||
"--all",
|
||||
action="store_true",
|
||||
help="Run all tests (default)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--file",
|
||||
type=str,
|
||||
help="Run specific test file (e.g., test_particles.py)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--class",
|
||||
dest="test_class",
|
||||
type=str,
|
||||
help="Run specific test class (requires --file)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--method",
|
||||
type=str,
|
||||
help="Run specific test method (requires --class)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--examples",
|
||||
action="store_true",
|
||||
help="Run examples instead of tests"
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.examples:
|
||||
exit_code = run_examples()
|
||||
elif args.file:
|
||||
exit_code = run_specific_test(
|
||||
test_file=args.file,
|
||||
test_class=args.test_class,
|
||||
test_method=args.method
|
||||
)
|
||||
else:
|
||||
exit_code = run_tests()
|
||||
|
||||
sys.exit(exit_code)
|
||||
Reference in New Issue
Block a user