xtest is a C++ testing framework inspired by googletest.

Contributors Forks Stargazers Issues LICENSE LinkedIn


Logo

xtest

C++ testing framework inspired by googletest
Explore the docs »

Wiki · Report Bug · Request Feature

Contents
  1. xtest
  2. Usage
  3. Contribution
  4. License
  5. Developers
  6. Acknowledgments

xtest

xtest is a C++ testing framework inspired by gtest. This is just a minimal implementation of a C++ unit testing framework which gives all the required facilities for unit testing a software without going template crazy and causing compilation issues.

Consider installing xtest from the most stable branch stable.

Commence

To download and install library xtest in your system follow the following steps.

Prerequisites

You must have a g++ 9.3.0 or greater version with a cmake version 3.16.3 or higher installed in your system.

Ubuntu

Step 1: Install gcc

sudo apt update
sudo apt install build-essential

If this is the first time you are installing g++ then you may also want to install the manual pages about using GNU/Linux for development.

sudo apt install manpages-dev

Verify that the compiler is installed.

g++ --version

and it should print,

g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

In the above output ubuntu1~20.04 is my version, yours may be different.

Step 2: Install cmake

  1. Uninstall the default cmake version provided by Ubuntu's package manager and configuration by using:

    sudo apt remove --purge --auto-remove cmake

    OR:

    sudo apt purge --auto-remove cmake

    This step is crucial if you have a old version of cmake installed in your system.

  2. Prepare for installation.

    sudo apt update
    sudo apt install -y software-properties-common lsb-release
    sudo apt clean all
  3. Obtain a copy of kitware's signing key.

    wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null
  4. Add kitware's repository to your sources list for Ubuntu Focal Fossa (20.04), Ubuntu Bionic Beaver (18.04) and Ubuntu Xenial Xerus (16.04).

    sudo apt-add-repository "deb https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main"
  5. As an optional step it is recommended that we also install the kitware-archive-keyring package to ensure that Kitware's keyring stays up to date as they rotate their keys.

    sudo apt update
    sudo apt install kitware-archive-keyring
    sudo rm /etc/apt/trusted.gpg.d/kitware.gpg
  6. Finally we can update and install the cmake package.

    sudo apt update
    sudo apt install cmake

Installation

Clone the repository.

git clone https://github.com/joshiayush/xtest.git

Build xtest

Next step is to build xtest as a shared library using our build system cmake.

cmake -B build/ -S .
cd build
make install

This will build xtest library as libxtest.so file inside the build directory. Now as the next step you have to create a symbolic link of libxtest.so in /usr/lib/ directory,

sudo ln -s /build/libxtest.so /usr/lib/libxtest.so

Now create symbolic link for the header files inside /usr/include/C++/9 inside xtest,

sudo mkdir /usr/include/C++/9/xtest
sudo ln -s /include/** /usr/include/C++/9/xtest

Now you are all set.

Build samples

To build samples,

cmake -B build/ -S . -DBUILD_SAMPLES:BOOL=ON
cd build
make install

Now run the samples,

./samples/factorial/factorial  # Runs the factorial unit test samples binary
./samples/isprime/isprime      # Runs the isprime unit test samples binary

Usage

Testing a custom function that returns the sqaure of a given number.

#include <xtest/xtest.hh>

double square(int n) {
  return n*n;
}

TEST(TestSquare, TestWithPositiveNumber) {
  EXPECT_EQ(square(10), 100);
}

int32_t main(int32_t argc, char** argv) {
  ::xtest::InitXTest(&argc, argv);
  return RUN_ALL_TESTS();
}

Now compile the file and link the libxtest.so file with it:

g++ -lxtest square_test.cc -o square_test

For more information on usage see our wiki.

Contribution

Contributions are what makes the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement", "bug", or "documentation". Don't forget to give the project a star! Thanks again!

Project xtest is hosted on GitHub. If you want to contribute changes please make sure to read the CONTRIBUTING.md file. You can also contribute changes to the CONTRIBUTING.md file itself.

License

Distributed under the BSD 3-Clause License. See LICENSE for more information. Please do not use project xtest if you have any issue with the BSD 3-Clause License.

Developers

Acknowledgments

A big thanks goes to the following resources who have helped in the development of project xtest.

Similar Resources

Kernel-mode C++ unit testing framework in BDD-style

There is a lack of unit testing frameworks that work in OS kernel. This library closes that gap and is targeted for windows driver developers.

Dec 28, 2022

The fastest feature-rich C++11/14/17/20 single-header testing framework

The fastest feature-rich C++11/14/17/20 single-header testing framework

master branch dev branch doctest is a new C++ testing framework but is by far the fastest both in compile times (by orders of magnitude) and runtime c

Jan 5, 2023

Header-only C++11 library for property-based testing.

autocheck Header-only C++11 library for QuickCheck (and later, SmallCheck) testing. Please consult the wiki for documentation. Install conan remote ad

Dec 22, 2022

Practical mutation testing tool for C and C++

Mull Mull is a tool for Mutation Testing based on LLVM/Clang with a strong focus on C and C++ languages. For installation and usage please refer to th

Dec 30, 2022

proftest is a C application for testing the quality of different operating system APIs for profiling.

proftest is a C application for testing the quality of different operating system APIs for profiling.

Jul 23, 2021

A micro unit-testing library for C/C++

µ-test A micro unit testing framework for C/C++ to get you up and running with unit-testing ASAP (even without libc). Usage Simply include the C and h

Dec 8, 2021

A tool to help in testing client/server robustness in the presence of malformed data.

Tool to assist in testing robustness of network-attached services in the presence of malformed data.

Aug 27, 2022

Bayesian A/B testing calculations for C++

BayesTest C++ Bayesian A/B testing calculations for C++ Based on this post by Evan Miller Also available in Rust Installation Include the header in yo

Nov 14, 2022

5G core testing solution

CoreScope CoreScope combines gNodeB and UE components without any radio transmission. It behaves like a UE and exposes an IP interface, but to the cor

Oct 21, 2022
Comments
  • FIX #5 -- Introduce new macro `XTEST_TESTING_DISABLED` to disable tests when building `xtest`'s tests together with the library

    FIX #5 -- Introduce new macro `XTEST_TESTING_DISABLED` to disable tests when building `xtest`'s tests together with the library

    This macro is needed as out tests uses output redirection so to capture the output in a custom buffer to later check it with the expected output. As we know ColoredPrintf() is the only gateway to our data to the console which internally uses function ShouldUseColor() that is bound to return false if any redirection happens.

    This commit also fixes the problem with flag --color which was not working before because of the old macro XTEST_TESTING_ENABLED which we always forgot to define before building the library with samples.

    There are two ways to define macro XTEST_TESTING_DISABLED:

    • Explicitly provide XTEST_TESTING_DISABLED option over the command line while building the library.
    • Removing BUILD_TESTS option from the command line while building the library.

    Fixes #5

    Signed-off-by: Ayush Joshi [email protected]

  • FIX #3 -- Prioritize flag parsing

    FIX #3 -- Prioritize flag parsing

    This commit prioritizes flag parsing so that flags don't mess up with function ParseFlagValue and yield wrong output.

    Also, fix function ParseFlagValue() by assigning flag_end a value of

    flag + (preffix_len + std::strlen(flag_name));
    

    not

    flag + (preffix_len + std::strlen(flag_name)) + 1;
    

    which produces wrong result because the if clause after it expects flag_end to be '=' in order to execute expression

    return ++flag_end;
    

    Fixes https://github.com/joshiayush/xtest/issues/3

    Signed-off-by: Ayush Joshi [email protected]

  • `ParseFlagValue()` returns `true` for non-value arguments when value arguments are used

    `ParseFlagValue()` returns `true` for non-value arguments when value arguments are used

    Description

    Currently while debugging, a bug has been come out where different flags are messing up with the algorithm. For example, flag nameshuffle is messing up with the algorithm when flag --color=no is used; ParseFlagValue() returns true for flag name shuffle when --color=no is used this is because of the statement *flag_end == '\0' in ParseFlagValue(). This is a bug in ParseFlagValue() and needs to be fixed.

    Solution

    A solution is to write a custom algorithm that will find the flag value in the given flag while also taking into account if the value is defined optional or not.

    https://github.com/joshiayush/xtest/blob/5a06d3981c2c3c1e7697af549b7d86b1ab997bd6/xtest/xtest.cc#L483-L501

    Replacing this fairly simple algorithm with a more robust one might help in solving this bug. I'll upload a more robust algorithm once I'll get time for it.

  • Capture output stream (stdout/stderr)

    Capture output stream (stdout/stderr)

    At the moment, we don't have any API that can also capture the output streams (stdout/stderr), so even to test our own xtest API we have to use the redirector API that we have inside of our tests which is not completely efficient. So, what I have decided to do is to implement a CapturedStream API similar to the one that googletest uses.

    Problems with RedirectorContext:

    • We have to explicitly pass the type of the stream we want to capture.
    • We have to manually replace the stream file with our own context buffer.
    • We also have to restore the stream manually.

    What googletest have for us:

    • Simplicity; (following example is taken from the cjson library).

      testing::internal::CaptureStdout();
      PrintSstream(&sstream, FALSE);
      std::string stdout_output = testing::internal::GetCapturedStdout();
      
Related tags
C++ Unit Testing Easier: A Header-only C++ unit testing framework

CUTE C++ Unit Testing Easier: A Header-only C++ unit testing framework usually available as part of the Cevelop C++ IDE (http://cevelop.com) Dependenc

Dec 26, 2022
The C Unit Testing Library on GitHub is a library designed for easy unit testing in C

The C Unit Testing Library on GitHub is a library designed for easy unit testing in C. It was written by Brennan Hurst for the purpose of providing a J-Unit-like testing framework within C for personal projects.

Oct 11, 2021
The fastest feature-rich C++11/14/17/20 single-header testing framework
The fastest feature-rich C++11/14/17/20 single-header testing framework

master branch Windows All dev branch Windows All doctest is a new C++ testing framework but is by far the fastest both in compile times (by orders of

Jan 4, 2023
C++ xUnit-like testing framework without macros

tst C++ testing framework. Installation, documentation, tutorials See WiKi. Features xUnit-like concepts minimal use of preprocessor macros declarativ

Sep 26, 2022
c++ testing framework

iutest iutest - iris unit test framework Welcome to the iutest iutest is framework for writing C++ tests. Features An XUnit test framework. Header onl

Sep 12, 2022
UT: C++20 μ(micro)/Unit Testing Framework
UT: C++20 μ(micro)/Unit Testing Framework

"If you liked it then you "should have put a"_test on it", Beyonce rule UT / μt | Motivation | Quick Start | Overview | Tutorial | Examples | User Gui

Jan 3, 2023
A complete unit testing framework in a header

liblittletest A complete unit testing framework in a header liblittletest is an easy to use all-in-an-header testing framework; all you have to do in

Nov 11, 2021
Modern c++17 unit testing framework on Microsoft Windows, Apple macOS, Linux, iOS and android.
Modern c++17 unit testing framework on Microsoft Windows, Apple macOS, Linux, iOS and android.

tunit Modern c++17 unit testing framework on Windows, macOS, Linux, iOS and android. Continuous Integration build status Operating system Status Windo

Apr 5, 2022
Simple C testing framework

MrTest Simple C testing framework Usage Copy the mrtest.c and mrtest.h file into your project. In order to use the mrtest main: create a .c file that

Jul 20, 2022
A minimal testing framework for C/C++
A minimal testing framework for C/C++

mtest About mtest is a minimal testing framework for C++. Requirements Windows or UNIX-like host A compiler supporting C++11 Usage To include mtest in

Jan 10, 2022