Testing Rust with fake data generation

PumpkinSeed
2 min readAug 25, 2023

--

Using fakeit package in Rust

Usually when you start a new project or you have an existing project that you have just started to test properly, you write loads of code for generating data for those tests. Each project has its own sub-modules which are test helpers. Tests use them to generate test data which is RELEVANT. I think we can agree that a random 6-character string is not a relevant first name. You need relevant data to test features which work in real world scenarios.

I just created a library which solves this problem. I primarily focused on simplicity. It is important to have a simple, easy to use solution which can easily be added to your project.

https://github.com/PumpkinSeed/fakeit

The following snippet shows an example of how we can fill a struct with relevant data and use it.

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct User {
id: String,
username: Option<String>,
phone: Option<String>,
email: Option<String>,
beer: Option<String>,
role: Option<String>,
billing_address: Option<Address>,
shipping_address: Option<Address>,
}

#[derive(Debug, Serialize, Deserialize)]
struct Address {
first_name: Option<String>,
last_name: Option<String>,
company: Option<String>,
line1: Option<String>,
line2: Option<String>,
city: Option<String>,
state: Option<String>,
country: Option<String>,
}

fn main() {
let user = User{
id: fakeit::unique::uuid_v4(),
username: Some(fakeit::internet::username()),
phone: Some(fakeit::contact::phone_formatted()),
email: Some(fakeit::contact::email()),
beer: Some(fakeit::beer::name()),
role: Some(fakeit::job::title()),
billing_address: Some(Address{
first_name: Some(fakeit::name::first()),
last_name: Some(fakeit::name::last()),
company: Some(fakeit::company::company()),
line1: Some(fakeit::address::street()),
line2: None,
city: Some(fakeit::address::city()),
state: Some(fakeit::address::state()),
country: Some(fakeit::address::country()),
}),
shipping_address: Some(Address{
first_name: Some(fakeit::name::first()),
last_name: Some(fakeit::name::last()),
company: Some(fakeit::company::company()),
line1: Some(fakeit::address::street()),
line2: None,
city: Some(fakeit::address::city()),
state: Some(fakeit::address::state()),
country: Some(fakeit::address::country()),
}),
};
let j = serde_json::to_string(&user).unwrap();
println!("{}", j);
}

The output looks like this:

{
"id":"707e7049-4e55-4708-a53c-2d4466eacdc6",
"username":"Vandervort6104",
"phone":"(577)017-5275",
"email":"reyeswisozk@dibbert.io",
"beer":"Stone Imperial Russian Stout",
"role":"Coordinator",
"billing_address":{
"first_name":"Saige",
"last_name":"Gottlieb",
"company":"Gleichner, Flatley and Sipes",
"line1":"08456 South Washington bury",
"line2":null,
"city":"Melisaburgh",
"state":"Montana",
"country":"Sudan"
},
"shipping_address":{
"first_name":"Bessie",
"last_name":"Muller",
"company":"Ruecker-Schaden",
"line1":"607 New South Dakota land",
"line2":null,
"city":"Rockyfort",
"state":"Wyoming",
"country":"Saint Kitts and Nevis"
}
}

If you have any feedbacks in any part of the project (additional features, code structure, potential bugs, improvements) it would be nice if you could share it with me (via Github issues).

I would also appreciate if you could throw a Star on the project. Open-source projects don’t pay creators. It’s a small gesture from you, but a big help for the creators.

--

--