Faker C++: Generate Realistic Fake Data for Testing and Development
Tags: cpp, fakedata, datageneration
Published: 2024-07-04 12:51:28

---
title: Faker C++: Generate Realistic Fake Data for Testing and Development
published: true
description: Presentation of Faker C++ project and how to use it minimally
tags: cpp,fakedata,datageneration
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iajnxpk85g82p8lx7fk3.png
# Use a ratio of 100:42 for best results.
# published_at: 2024-07-02 17:33 +0000
---

Hey there, C++ developers! 👋 Are you tired of creating mock data for your tests and prototypes? Say hello to Faker C++, a powerful library designed to generate realistic fake data for C++ applications. Let's dive into what makes this library special and how it can supercharge your development workflow.

## What is Faker C++?

[Faker C++](https://github.com/cieslarmichal/faker-cxx) is a C++ library inspired by the popular [Faker.js](https://fakerjs.dev/), aimed at providing developers with a robust tool for generating fake (but realistic) data. Whether you're building test suites, populating databases, or creating demos, Faker C++ has got you covered. It's open source and under MIT license.

## Key Features

* 📚 Realistic Data Generation: Generate various types of data including names, addresses, emails, dates, and more.
* 🛠 Modular Design: Choose from a wide range of modules like Internet, Location, String, Date, and more to generate specific types of data.
* 🚀 Easy Integration: Seamlessly integrate with CMake, and it supports major compilers like MSVC, GCC, Clang, and Apple Clang.

## Quick Example

Here's a taste of what you can do with Faker C++:

```cpp
// main.cpp
#include <cstdlib>
#include <iostream>

#include "faker-cxx/Internet.h"
#include "faker-cxx/Location.h"
#include "faker-cxx/String.h"

int main()
{
    const auto id = faker::String::uuid();
    const auto email = faker::Internet::email();
    const auto password = faker::Internet::password();
    const auto city = faker::Location::city();
    const auto streetAddress = faker::Location::streetAddress();

    std::cout << id << std::endl;
    std::cout << email << std::endl;
    std::cout << password << std::endl;
    std::cout << city << std::endl;
    std::cout << streetAddress << std::endl;

    return EXIT_SUCCESS;
}
```

## Consuming and using Faker C++

Let's see how to install and link to faker library.

### Installing with Conan 2.x

Faker C++ can be easily installed and integrated into your projects using Conan. Here's how you can do it:

First, make sure you have Conan 2.x installed. If not, you can install it using pip:

```shell
pip install conan
```

To install faker-cxx, run the following command:

```shell
conan install -r conancenter --requires="faker-cxx/[*]" --build=missing
```

This command will fetch the latest version of faker-cxx and build it if necessary.

If you want to use faker-cxx in your CMake project, you can create a `conanfile.txt` in your project root with the following content:

```ini
[requires]
faker-cxx/[*]

[generators]
CMakeDeps
CMakeToolchain

[layout]
cmake_layout
```

Using this conanfile.txt, Conan will provide faker-cxx CMake files under the folder `build/Release/generators/`

### Consuming Faker C++

Once you've set up Faker C++ and compiled your code, you can run your example to generate and display fake data.

First, let's configure the `CMakeLists.txt`, in order to link:

```cmakelists
cmake_minimum_required(VERSION 3.15)
project(example)

find_package(faker-cxx REQUIRED)
add_executable(example main.cpp)
target_link_libraries(example faker-cxx::faker-cxx)
target_compile_features(example cxx_std_20)
```

Now, we configure and build our example using Faker:

```shell
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=build/Release/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
cmake --build build
```

Now we can run our example using fake data:

```shell
build/example

11e9c8cc-04f8-49b2-ae28-afd6c5724e6d
CurtisShields@gmail.com
w(*Ba.u3!N@;Pz8
Shawnee
341 Meredith Hill
```

This output showcases a UUID, email address, password, city, and street address, all generated randomly by Faker C++. You can use similar functions to generate various other types of fake data as per your project’s requirements.

## Supported Modules

Faker C++ comes with a plethora of modules, each designed to generate specific types of data:

* 🖥️ Internet: emails, usernames, passwords, IP addresses.
* 🏢 Location: countries, cities, zip codes, street addresses.
* 🎨 String: UUIDs, alphanumeric, numeric, hexadecimal strings.
* 📖 Date: past and future dates.
* 🐼 Animal: bears, birds, cats, dogs.

And many more including modules for Commerce, Company, Finance, Food, Music, Sport, Vehicle, etc.

## Community and Contributions

Faker C++ is an open-source project and welcomes contributions! Whether you're fixing bugs, adding new features, or improving documentation, your help is appreciated. Check out our [Contributing](https://github.com/cieslarmichal/faker-cxx/blob/main/CONTRIBUTING.md) Guide for more details. Join our [Discord server](https://discord.gg/h2ur8H6mK6) to connect with other contributors.

## Conclusion

Faker C++ aims to be the go-to library for generating fake data in C++ projects. It's designed to make your testing and development process smoother and more efficient. Give it a try in your next project and let us know what you think!
Check out the full documentation to explore all the capabilities of Faker C++. Happy coding! 🚀