Common Structure of Testsfleeting
- Référence externe : https://doc.rust-lang.org/stable/book/ch11-01-writing-tests.html
common structure of tests
A good structure for all your tests (this is not limited to unit tests) is this one:
- Set up the test data
- Call your method under test
- Assert that the expected results are returned
There’s a nice mnemonic to remember this structure: “Arrange, Act, Assert”. Another one that you can use takes inspiration from BDD . It’s the “given”, “when”, “then” triad, where given reflects the setup, when the method call and then the assertion part.
This pattern can be applied to other, more high-level tests as well. In every nbcase they ensure that your tests remain easy and consistent to read. On top of that tests written with this structure in mind tend to be shorter and more expressive.
The bodies of test functions typically perform these three actions:
- Set up any needed data or state.
- Run the code you want to test.
- Assert the results are what you expect.