This directory contains comprehensive unit tests for the Nexus Python API.
The test suite is organized into the following modules:
test_basic.py- Basic tests for framework initialization and runtime discoverytest_runtime.py- Tests for Runtime and Device classestest_buffer.py- Tests for Buffer class and memory operationstest_schedule.py- Tests for Schedule and Command classestest_properties.py- Tests for the Properties system and device informationtest_integration.py- Integration tests for complete workflowsrun_tests.py- Test runner script
-
Build the Nexus framework with Python bindings:
cd /path/to/nexus mkdir build && cd build cmake .. -DNEXUS_BUILD_PYTHON_MODULE=ON make
-
Install required Python dependencies:
pip install numpy
cd test/python
python run_tests.py# Run tests with verbose output
python run_tests.py --verbose
# Run a specific test class
python run_tests.py --pattern TestBasic
# Run a specific test method
python run_tests.py --pattern TestBasic.test_runtime_discovery
# List all available tests
python run_tests.py --list# Run basic tests
python -m unittest test_basic
# Run buffer tests
python -m unittest test_buffer
# Run with verbose output
python -m unittest test_basic -vThe tests are designed to work even when the Nexus framework is not fully implemented:
- Tests are skipped if the
nexusmodule is not available - Tests handle missing functionality gracefully
- Error conditions are tested and expected
The test suite covers:
- Basic Functionality: Runtime discovery, device enumeration, framework initialization
- Memory Management: Buffer creation, copying, and cleanup
- Kernel Execution: Schedule and command creation, argument setting
- Properties System: Device information, property access
- Error Handling: Invalid operations, resource limits
- Integration: Complete workflows, multi-device operations
- Performance: Large buffers, many operations
Tests work across different platforms:
- Linux: CUDA, HIP, CPU backends
- macOS: Metal, CPU backends
- Windows: CPU backend
- Basic Tests: Framework initialization and runtime discovery
- Runtime Tests: Runtime and device management
- Buffer Tests: Memory buffer operations
- Schedule Tests: Command scheduling and execution
- Properties Tests: Device information and properties
- Workflow Tests: Complete vector addition and computation workflows
- Multi-device Tests: Operations across multiple devices
- Memory Management: Resource allocation and cleanup
- Error Handling: Invalid operations and edge cases
- Performance Tests: Large-scale operations and stress testing
The Nexus Python API follows this structure:
import nexus
# Get available runtimes
runtimes = nexus.get_runtimes()
# Get devices from a runtime
for runtime in runtimes:
devices = runtime.get_devices()
for device in devices:
# Work with device
buffer = device.create_buffer(data)
schedule = device.create_schedule()Tests use a helper function to find the first runtime with a valid device:
def get_first_runtime_with_device():
"""Helper function to get the first runtime that has at least one device."""
runtimes = nexus.get_runtimes()
for runtime in runtimes:
devices = runtime.get_devices()
if len(devices) > 0:
return runtime, devices[0]
return None, None- All tests should run and pass
- Kernel execution tests may fail if no kernel libraries are available
- Performance tests may be limited by available memory
- Tests are skipped with appropriate warnings
- No errors are thrown
- Test runner reports skipped tests
- Tests for implemented features pass
- Tests for missing features are skipped or handle errors gracefully
- Property system tests may be limited if not fully implemented
To add new tests:
- Create a new test file following the naming convention
test_*.py - Use the
@unittest.skipIf(nexus is None, "nexus module not available")decorator - Add proper error handling for missing functionality
- Update
run_tests.pyto include the new test module
Example test structure:
#!/usr/bin/env python3
"""
Unit tests for new feature.
"""
import unittest
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
try:
import nexus
except ImportError:
print("Warning: nexus module not found. Tests will be skipped.")
nexus = None
def get_first_runtime_with_device():
"""Helper function to get the first runtime that has at least one device."""
runtimes = nexus.get_runtimes()
for runtime in runtimes:
devices = runtime.get_devices()
if len(devices) > 0:
return runtime, devices[0]
return None, None
@unittest.skipIf(nexus is None, "nexus module not available")
class TestNewFeature(unittest.TestCase):
"""Test cases for new feature."""
def setUp(self):
"""Set up test fixtures."""
self.runtime, self.device = get_first_runtime_with_device()
self.runtimes = nexus.get_runtimes()
def test_new_feature(self):
"""Test new feature functionality."""
if self.device is not None:
# Test implementation
pass
if __name__ == '__main__':
unittest.main()If you get import errors:
- Ensure Nexus is built with Python bindings
- Check that the Python path includes the Nexus module
- Verify that
libnexus.so(or equivalent) is in the Python module directory
If numpy is not available:
pip install numpyThe tests have been updated to use the current Nexus API:
- Use
nexus.get_runtimes()instead ofnexus.get_system() - Access devices through runtime objects
- Use the helper function
get_first_runtime_with_device()for consistent device access
When running tests, you may see:
- PASS: Test completed successfully
- SKIP: Test skipped due to missing dependencies or functionality
- FAIL: Test failed due to implementation issues
- ERROR: Test encountered an unexpected error
Tests are designed to be informative about what functionality is available and what might be missing in the current implementation.