6 releases

0.1.0 Sep 6, 2022
0.1.0-dev.4 Aug 19, 2022
0.1.0-dev.3 Aug 16, 2022
0.1.0-dev.1 Aug 15, 2022

#442 in Testing

Download history 454/week @ 2024-03-14 636/week @ 2024-03-21 319/week @ 2024-03-28 604/week @ 2024-04-04 647/week @ 2024-04-11 599/week @ 2024-04-18 893/week @ 2024-04-25 607/week @ 2024-05-02 436/week @ 2024-05-09 728/week @ 2024-05-16 554/week @ 2024-05-23 619/week @ 2024-05-30 602/week @ 2024-06-06 537/week @ 2024-06-13 669/week @ 2024-06-20 409/week @ 2024-06-27

2,333 downloads per month
Used in 8 crates (3 directly)

MIT license

15KB
176 lines

Isotest enables a very specific Rust unit testing pattern.

Say you have some complex data type which is used in a lot of places. You'd like to write functions that operate on that complex data, but these functions only need to know about some small subset of that data type. It could be convenient to write those functions generically over a trait which contains methods that work with that subset of data, rather than taking the full data type.

There a few key benefits to doing this. Using a trait hides irrelevant details, keeping the concern of the function limited to only what it needs to know. This is a good application of the principle of least privilege.

Using a trait also makes writing tests easier. If we used the full data type, we would need to create test fixtures with a lot of extraneous arbitrary data which won't even affect the function under test. By using a trait, we can write a much simpler test-only data structure which implements the same trait, and use that instead of the complex "real" data type in our tests. This keeps tests simpler and avoids the need to generate lots of arbitrary throw-away data.

Additionally, when debugging a failing test, it's a lot easier to get debug output on just the data we care about, and not have all of the noise of the real data structure included in the debug output.

The only concern with this approach is that we're not testing the "real" data, and if there is any problem with our test data type or its implementation of the common trait, then we may not be testing what we think we are testing. This is where isotest comes in.

isotest helps ensure that our implementation of the common trait is correct by letting the user define functions to convert to and from the test data type, and then providing a macro with a simple API to run tests for both types. The user can write tests in terms of the simpler test data, but then that test gets run for both test and real data, to ensure that assumptions implicit in the trait implementation are not faulty. Thus, you get the benefit of simplifying your test writing while still testing your logic with real data.

Dependencies

~135KB