Giter Club home page Giter Club logo

eztest's Introduction

EzTest

Release License: MIT

An easy to use unit testing framework written in, and created for, the C language.

Table of contents

Introduction

A simple test
TEST(Math, AddingTwoOnTwoShouldEqualFour)
{
    const int expected = 4;
    const int actual = math_add(2, 2);
    
    ASSERT_ARE_EQUAL(expected, actual); // or ASSERT_EQ(expected, actual); using the shorthand.
}

The asserts are created using the power of the C11 macro _Generic. As seen in the above example, this means that the user doesn't have to provide any prefix or suffix to represent the data type.

A full test (Setup and Teardown)

EzTest also provides the option of setup and teardown functions. The setup function is a function that runs before every test within a test suite. The teardown function has similar behaviour, but runs after the test. A test utilizing setup and teardown functions is defined by using the TEST_FULL(suite, name) macro. A setup and teardown function must also be defined for the test suite when using the full test macro. This is done with the SETUP(suite) and TEARDOWN(suite) macros.

SETUP(Math)
{
    // Code to run before every test in the Math suite.
}

TEST_FULL(Math, AddingTwoOnTwoShouldEqualFour)
{
    // Test code placed here.
}

TEARDOWN(Math)
{
    // This code runs after every test in the Math suite.
}

See the next section for information on how to get started with EzTest.

Getting started

EzTest was created to make unit testing quick and easy in C. The fastest way to get started with EzTest is by using the template. The template provides a default project layout for C projects using EzTest. After getting the template or creating your own project structure, you are ready to start.

1. Write tests using the test macros

Before having access to the test macros you need to include the eztest header file: #include "eztest.h". There are two macros for creating tests: TEST(suite, test) and TEST_FULL(suite, test). The first is for simple unit tests while the second is for tests with a setup and teardown function. Both of these macros take the same arguments.

Argument 1: Suite

This is the name of the test suite that the test will be associated with. The name must follow the rules for variable names in C and must be unique through out your tests. The suite name can be used to group multiple tests providing a better overview for the tester.

Argument 2: Test

This is the name of the test. The name must follow the rules for variable names in C and must be unique within the test suite.

Example usage of both TEST(suite, test) and TEST_FULL(suite, test) can be found in the usage section.

2. Build/ Compile

Option I: CMake/Template

The perhaps simplest way to build your tests is to use CMake. If you are using CMake and/or the template, then follow the build instructions given here.

Option II: Manual build

To build the EzTest runner, compile using a C11 compatible C compiler by providing the runner.c source file along with your test files.
Example compile: $ gcc -o ezrunner runner.c <test-files>

3. Run

Run the executable to run your tests. The test runner can take multiple optional arguments to customize your test experience. Learn more about the test runner here.

Asserts

Macro Shorthand Description Details
ASSERT_IS_NULL Tests whether the provided pointer is null. Documentation
ASSERT_IS_NOT_NULL Tests whether the provided pointer is non-null. Documentation
ASSERT_IS_TRUE Tests whether the condition is true. Documentation
ASSERT_IS_FALSE Tests whether the condition is false. Documentation
ASSERT_ARE_SAME Tests whether the two pointers refer to the same memory location. Documentation
ASSERT_ARE_NOT_SAME Tests whether the two pointers refer to different memory locations. Documentation
ASSERT_IS_NAN Tests whether the provided float is NaN. Documentation
ASSERT_ARE_EQUAL ASSERT_EQ Tests whether the two values are equal. Documentation
ASSERT_ARE_EQUAL_PRECISION ASSERT_EQ_PRECISION Tests whether two floating point numbers are equal using a user provided epsilon. Documentation
ASSERT_ARE_EQUAL_MEM ASSERT_EQ_MEM Tests whether the two values are equal by comparing each byte at the given memory locations. Documentation
ASSERT_ARE_EQUAL_CMP ASSERT_EQ_CMP Tests whether the two values are equal by using the passed comparator function. Documentation
ASSERT_ARE_NOT_EQUAL ASSERT_NE Tests whether the two values are different. Documentation
ASSERT_ARE_NOT_EQUAL_PRECISION ASSERT_NE_PRECISION Tests whether two floating point numbers are different using a user provided epsilon. Documentation
ASSERT_ARE_NOT_EQUAL_MEM ASSERT_NE_MEM Tests whether the two values are different by comparing each byte at the given memory locations. Documentation
ASSERT_ARE_NOT_EQUAL_CMP ASSERT_NE_CMP Tests whether the two values are different by using the passed comparator function. Documentation
ASSERT_GREATER ASSERT_GT Tests whether the first value is greater than the second value.
ASSERT_GREATER_PRECISION ASSERT_GT_PRECISION Tests whether the first floating point value is greater than the second floating point value using a user provided epsilon.
ASSERT_GREATER_MEM ASSERT_GT_MEM Tests whether the first n bytes at the first memory location is greater than the n first bytes at the second memory location.
ASSERT_GREATER_CMP ASSERT_GT_CMP Tests whether the first value is greater than the second value by using the passed comparator function.
ASSERT_GREATER_EQUAL ASSERT_GE Tests whether the first value is greater than or equal to the second value.
ASSERT_GREATER_EQUAL_PRECISION ASSERT_GE_PRECISION Tests whether the first floating point value is greater than or equal to the second floating point value using a user provided epsilon.
ASSERT_GREATER_EQUAL_MEM ASSERT_GE_MEM Tests whether the first n bytes at the first memory location is greater than or equal to the n first bytes at the second memory location.
ASSERT_GREATER_EQUAL_CMP ASSERT_GE_CMP Tests whether the first value is greater than or equal to the second value by using the passed comparator function.
ASSERT_LESS ASSERT_LT Tests whether the first value is lesser than the second value.
ASSERT_LESS_PRECISION ASSERT_LT_PRECISION Tests whether the first floating point value is lesser than the second floating point value using a user provided epsilon.
ASSERT_LESS_MEM ASSERT_LT_MEM Tests whether the first n bytes at the first memory location is lesser than the n first bytes at the second memory location.
ASSERT_LESS_CMP ASSERT_LT_CMP Tests whether the first value is lesser than the second value by using the passed comparator function.
ASSERT_LESS_EQUAL ASSERT_LE Tests whether the first value is lesser than or equal to the second value.
ASSERT_LESS_EQUAL_PRECISION ASSERT_LE_PRECISION Tests whether the first floating point value is lesser than or equal to the second floating point value using a user provided epsilon.
ASSERT_LESS_EQUAL_MEM ASSERT_LE_MEM Tests whether the first n bytes at the first memory location is lesser than or equal to the n first bytes at the second memory location.
ASSERT_LESS_EQUAL_CMP ASSERT_LE_CMP Tests whether the first value is lesser than or equal to the second value by using the passed comparator function.

Runner

The runner is the program that executes the tests.

Exit code

The exit code is EXIT_SUCCESS (0) if all tests passed and EXIT_FAILURE (non-zero) if one or more tests failed.

Options

Short Long               Description
-v --version Prints the version number.
-h --help Prints help/ usage information.
-c --no-color Don't use any color when printing.
-t --timer Display execution time for each test.
-q --quiet No output.
-s --skip Skips all tests in the passed list of test suits. The argument for this option should be a comma separated list of case-sensitive test suit names that you want to skip.
-f --SIGSEGV Segmentation fault is handled like other test failures.

Contribute

We welcome all contributions, please see the contribution guidelines.

License

This project is licensed under the MIT License - see LICENSE.md for details.

eztest's People

Contributors

havardt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

eztest's Issues

Timer option should provide overall time

Is your feature request related to a problem? Please describe.
Currently, the timer option displays the time for each unit test. It does not display the overall (combined) time for all unit tests.

Describe the solution you'd like
The total time should be displayed at the end along with the report.

[FEATURE REQUEST] Skip tests

Is your feature request related to a problem? Please describe.
A user does not always want to run all tests.

Describe the solution you'd like
The user should be able to supply suits or tests that should not run. This can be done through command line arguments so the user does not have to change their code and recompile in order to skip tests.

[FEATURE REQUEST] Failed tests only mode

Is your feature request related to a problem? Please describe.
When many tests are run the output can be overwhelming.

Describe the solution you'd like
I would like a mode/option that only prints test output if the test fails.

New asserts: Greater/ less, Less equal, greater equal

4 new equality asserts should be added:

  • Greater: test if the first value is greater than the second value.
  • Less (Test if the first value is less than the second)
  • Greater or equal: test if the first value is greater than or equal to the second value.
  • Less or equal: test if the first value is less than or equal to the second value.

Docs further detailing usage of each assert

Each asserts should have further detailed docs. These docs should at minimum include:

  • Description
  • Parameter list with expected data types and description for each parameter.
  • Example

Short-hand asserts

Currently all assert macros follow a naming convention of clearly defining use and meaning, however it may be favorable to also have short-hands for each macro.

Example:

ASSERT_ARE_NOT_EQUAL could have a short-hand macro with the name ASSERT_NE.
ASSERT_ARE_EQUAL could have a short-hand macro with the name ASSERT_EQ.

[FEATURE REQUEST] Report only mode

Is your feature request related to a problem? Please describe.
When running a lot of tests, the output can be overwhelming.

Describe the solution you'd like
I would like a option/ mode that only prints out a final report giving me an overview of the test results.

[BUG] Base tests runs when multiple test files

Describe the bug
When running tests with multiple test files, the base test used as a reference also runs.

To Reproduce
Steps to reproduce the behavior:

  1. Use multiple files for tests, each with #include eztest/core/eztest.h
  2. Build and run

Expected behavior
The base/ reference test should never run.

[FEATURE REQUEST] Asserts with custom comparator

Is your feature request related to a problem? Please describe.
Testing structs can be a hassle when having to assert each individual part.

Describe the solution you'd like
An assert macro that takes a comparator function as parameter.

POSIX reserved

Currently, there is a large usage of underscores to indicate that the function should not be used by the user. This is a problem as functions starting with an underscore are reserved.

Proposed fix: replace '_' with 'eztest'

The downside here is that it is not as clear to the user what functions should be accessed and which should not be accessed. With this said, it is a change that must be done.

[FEATURE REQUEST] Quiet mode

Is your feature request related to a problem? Please describe.
The human readable output is not always wanted and/ or needed.

Describe the solution you'd like
I would like a quiet/ silent mode that makes sure nothing is printed to the screen. Rather the exit status should represent all tests passed (returns 0) or at least one test failed (returns 1).

[FEATURE REQUEST] Add test timer

Describe the solution you'd like
I would like for each test to be timed. The time of the test should be displayed along with the test result.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.