This directory contains the comprehensive pytest-based test suite for benpy 2.1.0.
The test suite validates:
- Basic module imports and structure
- API compatibility and backward compatibility
- Memory management and leak detection
- Example problems from the bensolve distribution
- Problem dimension consistency
- Solution properties and data access
pytestpytest tests/test_import.py
pytest tests/test_api.py
pytest tests/test_memory.py
pytest tests/test_examples.py# Run only API tests
pytest -m api
# Run only memory tests
pytest -m memory
# Run only example-based tests
pytest -m examples
# Skip slow tests
pytest -m "not slow"pytest --cov=benpy --cov-report=htmlPytest configuration and shared fixtures:
cleanup_memory: Auto-cleanup after each testsimple_2d_problem: Basic 2D test probleminfeasible_problem: Infeasible problem fixtureunbounded_problem: Unbounded problem fixturecone_problem: Problem with custom ordering conemax_problem: Maximization problem fixturepartially_unbounded_problem: Partially unbounded fixture
Basic import and module structure tests:
- Module can be imported
- Version information available
- Core classes accessible (vlpProblem, vlpSolution, _cVlpProblem, _cVlpSolution)
- Core functions available (solve, solve_direct)
- Dependencies present (numpy, scipy, prettytable)
API compatibility and functionality tests:
- In-memory interface: from_arrays, solve_direct
- Matrix recovery: constraint_matrix, objective_matrix
- Dimension validation: Mismatch detection
- Sparse matrices: scipy.sparse support
- Bounds handling: Various bound combinations
- Ordering cones: Y and Z generators
- Duality parameters: c vector specification
- Structure access: Problem and solution properties
- Backward compatibility: Traditional vlpProblem interface
- Optimization directions: Minimization and maximization
- Data types: float32, float64, integer conversion
Memory management and leak detection tests:
- Deallocation: dealloc called correctly
- Multiple allocations: No leaks when creating many objects
- Ordering cones: Memory handling with Y generators
- Duality parameters: Memory handling with c vectors
- Property access: Safe solution property access
- File-based problems: from_file memory management
- Reference counting: Python refcount correctness
- Object reuse: Reusing problem objects safely
- NULL initialization: Safe initialization and cleanup
Tests using bensolve example problems:
- Example 01: Simple 2-objective MOLP
- Example 02: Infeasible problem (expected to fail)
- Example 03: Upper image with no vertex
- Example 04: Totally unbounded problem
- Example 05: VLP with custom ordering cone (3 objectives)
- Example 06: Maximization with dual cone
- Example 08: Partially unbounded problem
- Example 11: 5-objective problem (31 constraints)
- Consistency checks: All solvable examples run
- Dimension validation: Internal consistency of all examples
- Solution properties: Expected structure and data
Python definitions of bensolve example problems:
- Converted from MATLAB (.m) files
- Functions:
get_example01()throughget_example11() - Helper functions:
get_all_examples(): All examples as dictionaryget_solvable_examples(): Names of solvable examplesget_infeasible_examples(): Names of infeasible examplesget_unbounded_examples(): Names of unbounded examples
Tests are organized using pytest markers:
@pytest.mark.api: API compatibility tests@pytest.mark.memory: Memory leak detection tests@pytest.mark.examples: Example problem tests@pytest.mark.integration: Full solve operations@pytest.mark.slow: Tests that take longer to run
| Example | Description | Type | Status |
|---|---|---|---|
| example01 | Simple 2-objective MOLP | Minimization | Solvable |
| example02 | Infeasible problem | Minimization | Infeasible |
| example03 | No vertex in upper image | Minimization | Solvable |
| example04 | Totally unbounded | Minimization | Unbounded |
| example05 | Custom ordering cone (q=3) | Minimization | Solvable |
| example06 | Maximization with dual cone | Maximization | Solvable |
| example08 | Partially unbounded | Minimization | Unbounded |
| example11 | 5 objectives, 31 constraints | Minimization | Unbounded |
def test_my_feature(simple_2d_problem):
"""Test using the simple 2D problem fixture."""
sol = benpy.solve_direct(
simple_2d_problem['B'],
simple_2d_problem['P'],
a=simple_2d_problem.get('a'),
l=simple_2d_problem.get('l'),
opt_dir=simple_2d_problem['opt_dir']
)
assert sol is not None@pytest.mark.slow
@pytest.mark.examples
def test_large_problem():
"""Test a large problem (marked as slow)."""
# ... test code ...from problems import get_example05
def test_with_example():
"""Test using a predefined example."""
prob_data = get_example05()
sol = benpy.solve_direct(
prob_data['B'],
prob_data['P'],
# ... other parameters ...
)
assert sol is not NoneThese tests are designed to run in CI environments:
- GitHub Actions workflow (when configured)
- Multiple Python versions (3.8, 3.9, 3.10, 3.11, 3.12)
- Multiple platforms (Linux, macOS, Windows)
Target coverage areas:
- ✓ Module imports and structure
- ✓ API functions and classes
- ✓ Memory management
- ✓ Example problems
- ✓ Dimension validation
- ✓ Error handling
- ✓ Backward compatibility
If tests fail with import errors:
# Install benpy in development mode
pip install -e .Memory tests may fail if:
- benpy is not properly compiled
- There are actual memory leaks
- System memory is constrained
Some tests are marked as slow. Skip them:
pytest -m "not slow"When adding new features to benpy:
- Add corresponding tests
- Use appropriate markers
- Update this README if adding new test files
- Ensure all tests pass before submitting PR
- bensolve documentation: http://www.bensolve.org/
- benpy documentation:
/doc/InMemoryInterface.md - Example notebooks:
/notebooks/ - MATLAB examples:
/src/bensolve-2.1.0/ex/