SCADwright provides a few tools for writing automated tests against your designs.
tree_hash computes a short hex hash of a shape's AST (ignoring source locations). If any dimension, transform, or boolean op changes, the hash changes. Use it to pin a part's geometry in a unit test:
from scadwright import tree_hash
def test_widget_geometry_pinned():
w = MyWidget(width=40, height=20)
assert tree_hash(w) == "a1b2c3d4e5f6..."When you intentionally change the geometry, run the test, copy the new hash from the failure message, and update the assertion.
from scadwright.asserts import assert_fits_in, assert_contains, assert_no_collision, assert_bbox_equalassert_fits_in(part, [200, 200, 50])-- part's bounding box fits within the given volume.assert_contains(outer, inner)-- outer's bbox fully contains inner's bbox.assert_no_collision(a, b)-- bounding boxes don't overlap.assert_bbox_equal(part, ((0, 0, 0), (10, 10, 10)))-- bbox matches exactly (within tolerance).
These check bounding boxes, not mesh geometry. They're fast (no rendering required) and catch the most common errors: a part that grew too large, parts that overlap, or a part that shifted unexpectedly.
Call the variant method to get its scene. What you get back is what scadwright build writes, so a test can assert on the real output:
from scadwright import emit_str
def test_print_variant_builds():
design = MyDesign()
node = design.print()
scad = emit_str(node)
assert "difference" in scad # or whatever you expect
def test_display_variant_builds():
design = MyDesign()
node = design.display()
assert tree_hash(node) == "..."Scenes from different variants don't interfere, so one test can compare them:
def test_both_variants_fit_the_bed():
design = MyDesign()
assert_fits_in(design.print(), [220, 220, 250])
assert_fits_in(design.display(), [400, 400, 400])Assert on the emitted text when the thing you care about is a setting rather than a shape. A tree_hash tells you the geometry changed, not what it changed to, and the camera doesn't move it at all:
def test_final_variant_is_smooth():
assert "$fn = 128" in emit_str(MyDesign().final())For regression testing the generated SCAD output, compare against a saved "golden" file:
from pathlib import Path
from scadwright import emit_str
def test_widget_scad_output(tmp_path):
golden = Path("tests/golden/widget.scad")
actual = emit_str(MyWidget(width=40))
if golden.exists():
assert actual == golden.read_text()
else:
golden.write_text(actual) # first run: create the golden fileSCADwright's own test suite uses this pattern extensively. When you make an intentional change, regenerate the golden files and review the diff.