tape-testing / tape

tap-producing test harness for node and browsers
MIT License
5.77k stars 307 forks source link

How to test a function multiple times, with a range of inputs? #518

Closed cagross closed 4 years ago

cagross commented 4 years ago

Hello. I've written my first test using Tape--in my test I call one of my functions, pass to it a sample 'test' object, and assert the return value is a number. The test passes as expected. I'd now like to create several more 'test' objects, and ensure my Tape test is executed over and over, once for each object. Is there a built-in way to do this in Tape, e.g. a special 'batch' test or something? Or would I be responsible for writing the code to do that (e.g. saving all 'test' objects to a single array, then loop over the array and carry out my test on each array element)?

Thanks.

ljharb commented 4 years ago

Indeed, you’d make an array of test cases, iterate that array, and assert on each item.

For tape to try to support this natively would either require adding complexity to every assertion type, or, providing a single utility that was just a shallow wrapper around array map, which is already in the language.

Raynos commented 4 years ago

Once utility that’s useful when doing tests on a large array is collapsing all the assertions into one TAP output line.

https://github.com/raynos/collapsed-assert

I use this small library with loops quite often to avoid seeing one thousand TAP output lines on a test.

ljharb commented 4 years ago

TIL, that's really nice.

cagross commented 4 years ago

Sorry for the late reply, but thanks to both. I'll start with a simple array as @ljharb suggested, and consider the collapse utility suggested by @Raynos. We can consider this resolved.