Testing¶
numgrids uses pytest as its test runner. All tests live in
the tests/ directory at the repository root.
Running the Full Test Suite¶
python -m pytest tests
To see verbose output with individual test names:
python -m pytest tests -v
Running Specific Tests¶
Run a single test file:
python -m pytest tests/test_diff.py
Run a single test class or function:
python -m pytest tests/test_diff.py::TestDiffSomeClass
python -m pytest tests/test_diff.py::TestDiffSomeClass::test_specific_case
Test Organization¶
The test suite is organized with roughly one test file per module:
Test file |
What it covers |
|---|---|
|
Grid creation and properties |
|
Differentiation operators |
|
Boundary conditions (Dirichlet, Neumann, Robin) |
|
Axis types (uniform, logarithmic, etc.) |
|
Curvilinear grids (spherical, cylindrical, polar) |
|
Adaptive mesh refinement |
|
Numerical integration |
|
Interpolation |
|
Save / load functionality |
|
Public API surface |
|
Plotting utilities |
Writing New Tests¶
When adding or modifying functionality, always include corresponding tests.
Test structure¶
Tests are written using Python’s unittest.TestCase base class:
import unittest
import numpy as np
from numpy.testing import assert_array_almost_equal
from numgrids import Grid
class TestMyFeature(unittest.TestCase):
def test_something(self):
grid = Grid((-1, 1, 50))
# ... set up the problem ...
result = compute_something(grid)
expected = analytical_solution(grid)
assert_array_almost_equal(result, expected, decimal=6)
Numerical assertions¶
Because numgrids deals with numerical computations, most assertions compare arrays
with a finite tolerance. Prefer helpers from numpy.testing:
numpy.testing.assert_array_almost_equal– element-wise comparison up to a number of decimal places.numpy.testing.assert_allclose– relative and absolute tolerance comparison.
Mathematical correctness¶
Tests should verify results against known analytical solutions whenever possible. For example, a test for a second-order derivative operator might compare its output to the exact second derivative of a polynomial on the same grid. This ensures that the numerical discretization converges to the correct mathematical result.