1 hour ago · Tech · hide · 0 comments

I’ve been revisiting some of my small Rust utilities this week, and I wanted to improve the test suite. There’s a lot of repetition, and I wanted to switch to parametrised tests, a pattern I use a lot in Python and Go. (These are also called “table-driven tests”.) Parameterised tests allow you to write the test logic once, then run it with many different inputs and expected results. This reduces repetition, highlights variation, and simplifies adding new cases. For example, in my thumbnail creator, I want to create a thumbnail with various image formats, and check the thumbnail has the correct filename and dimensions. Rather than copy-pasting test functions, I wanted to declare test cases with named fields, like this: animated_gif: { img_path: "src/tests/animated_squares.gif", target: TargetDimension::MaxWidth(16), expected_filename: "animated_squares.mp4", expected_dims: (16, 16), }, Supporting this syntax required me to create a custom Rust macro. Existing approaches I found three…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.