Menu

[Rust Guide] 11.1. Writing and Running Tests
🖼️

[Rust Guide] 11.1. Writing and Running Tests

DEV Community·SomeB1oody·7 months ago
#NKR9vmxy
#rust#programming#learning#software#test
Reading 0:00
15s threshold

If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.

11.1.1 What Is Testing

In Rust, a test is a function used to verify whether non-test code behaves as expected.

A test function usually performs three actions:

Arrange data/state

Act on the code under test

Assert the result

In some languages, these three actions are called the 3A steps.

11.1.2 Anatomy of a Test Function

A test function is still just a function; the difference is that it must be annotated with the test attribute.

An attribute is metadata for Rust code. It does not change the logic of the code it decorates; it only adds decoration, or annotation. In fact, we already used this in 5.2. Struct Usage Example - Printing Debug Information.

Adding #[test] to a function turns it into a test function.

11.1.3 Running Tests

Putting aside what is inside the test function for now, how do we run it after writing it?…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.