Tutorial: Adding tests
In our previous tutorials, we built a binary main
target and a library
target that uses it, even incorporating a shared logging_lib
. Now, let's
ensure our library
target works as expected by adding unit tests. Writing
tests helps us catch bugs early and refactor with confidence.
Our goal is to learn how to define and run Rust unit tests within the Buck2.
What We'll Do:
- Create a dedicated directory for our library's tests.
- Write a simple unit test for the greet function in
greeter_lib
. - Update
greeter_lib/BUCK
to define a test target usingrust_test
. - Run the tests using Buck2 and see the results.
Prerequisites
- You should follow the previous tutorial, we will start from the state as the previous tutorial ends.
Step 1: Create a dedicated directory for our tests
- Navigate to your
greeter_lib
directory: - Create the
tests
directory:
mkdir tests
Your greeter_lib
structure should now look like this:
greeter_lib
├── BUCK
├── src
│ └── lib.rs