Structuring Rust Project for Testability

Structuring Rust Project for Testability

Write better, more testable Rust

In this article, we will see how to structure a Rust project so that it’s easily testable. We will build a simple authentication module that is exposed via REST API while storing the data to PostgreSQL and Redis. We will use actix-web for handling the REST API part, sqlx for interacting with PostgreSQL, and redis-rs for interacting with Redis. We will see how the application is decomposed into smaller components that are testable. The final application source code can be accessed from GitHub.


It’s a well-known fact that incorporating automated testing in a software project leads to better software. Automated tests help to ensure the correctness of the software while also improving its maintainability. So, having automated tests in your software project is a good practice to have.

Unfortunately, incorporating automated tests is not a straightforward thing to do. You’ll need to think through how to structure your project so that it is easily testable. In general, what you need to do is to modularize your software internals into decoupled components that can be tested independently.

I had this issue when I was working on a Rust web project. I tried to look for inspiration from some open source projects. However, the level of testing that they have are not up to my preference. They either have none or simply just do integration tests while skipping unit tests.

The goal of this article is to share with you a solution for the issue above in hope that you (or me in the future) will find it useful. In addition to that, I would be happy to receive any feedback on how you structure your Rust projects so that my Rust “toolbox” expands.


What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Rust Casting, Shadowing, Consts and Static

A mix of topics of casting, shadowing, constants and static variables inside the Rust Programming Language. This Rust programming language tutorial series is aimed at easing your training step by step.

Rust Zürichsee, April: Gerhard Bräunlich - PyO3 and Rust in action

This presentation was the first experiment with livestreaming of the Rust Zürisee meetup group in Switzerland. Gerhard is sharing parts of his story migratin...

"Rust Isn’t Afraid to be Imperfect as Long as we Ship something Useful"

Steve Klabnik is a member of the Rust core team, an active open-source contributor, and author of The Rust Programming Language, Rails 4 in Action, and Designing Hypermedia APIs books. In 2012 and 2016, we invited Steve to speak at the RailsClub (now RubyRussia) conference. Since then, Steve has been working on Rust a lot, did a lot of interesting things and we realized that we should definitely interview him once again!

Prevent Breaking Code Changes in Future Releases using `non exhaustive` enums in Rust

Hello everyone, recently I have come across a feature in Rust, known as non_exhaustive. It was introduced in Rust 1.40.0 . This attribute prevents source code-breaking changes in projects downstream.

Rust vs Go - Which Is More Popular?

Rust vs Go - Which Is More Popular - Go and Rust are two of the hottest compiled programming languages. I develop in Go full-time and love it, and I'm learning more about Rust recently - its an exc