DEV Community

Cover image for Faker C++: Generate Realistic Fake Data for Testing and Development
Uilian Ries
Uilian Ries

Posted on

Faker C++: Generate Realistic Fake Data for Testing and Development

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++ is a C++ library inspired by the popular Faker.js, 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++:

// 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;
}
Enter fullscreen mode Exit fullscreen mode

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:

pip install conan
Enter fullscreen mode Exit fullscreen mode

To install faker-cxx, run the following command:

conan install -r conancenter --requires="faker-cxx/[*]" --build=missing
Enter fullscreen mode Exit fullscreen mode

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:

[requires]
faker-cxx/[*]

[generators]
CMakeDeps
CMakeToolchain

[layout]
cmake_layout
Enter fullscreen mode Exit fullscreen mode

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:

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)
Enter fullscreen mode Exit fullscreen mode

Now, we configure and build our example using Faker:

cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=build/Release/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
cmake --build build
Enter fullscreen mode Exit fullscreen mode

Now we can run our example using fake data:

build/example

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

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 Guide for more details. Join our Discord server 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! πŸš€

Top comments (0)