Coder Social home page Coder Social logo

ncunit's Introduction

ncunit

Minimal (nano) C unit testing library.

This library defines a model to implement unit tests, as well as some helper functions that might be useful when doing so, like byte-by-byte comparisons of random memory (see equals_ptr) and check of access to memory (see mem_access).

You could have written this library. Now you don't have to.

For further documentation, including the helper functions defined in the library, see the header file ncunit.h

Usage

A test is defined as a function of the form:

char * test();

This function can be named whatever, and must return NULL if the test has been passed successfully. Assertions may be used to make the test fail given the desired circumstances.

Inside tests, assertions may be included. Assertions are of the form:

assert(int cond, char * msg, ...);

where «cond» is the condition to check (the assertion will fail iff cond == 0), and «msg, ...» is the formatted message (printf style) to show when the assertion fails. Assertions make the test they are in return the error message when the condition is false. Because of this, assertions may only be used inside tests, not anywhere in the code.

To execute a test, simply call the function execute_test:

execute_test(char * (*test)(), void (*before)(), void (*after)(), char * name);

where «test» is the function representing the test, «name» is the name of the test to be executed, «before» is a function to be run before the test and «after» a function to be run after the test (if any of these two last arguments is NULL, they will be ignored)

The results of the different tests will be shown through STDERR as follows:

[nunit] $$ test_1: OK
[nunit] XX test_2: ERROR: Sample error message
...

This output makes it easy to automate the tests using simple shell scripts.

The library can also be compiled so that headers are printed in the output (see Installation). In this case, the ouput will be as shown below:

[nunit] >> Test: test_1
[nunit] $$ test_1: OK
[nunit] >> Test: test_2
[nunit] XX test_2: ERROR: Sample error message
...

Installation

To install:

sudo make install

To uninstall:

sudo make uninstall

If anything fails, first try to clean and recompile, then reinstall:

make clean; make
sudo make install

To make the library show headers in the output, before installing run make as follows:

make CFLAGS='-DNC_HEADERS'

Examples

Examples are included in the «examples» folder. To compile them, run:

make example

There is an example script that could be used in a makefile to automate the tests.

The source of the examples is in the «examples/src» folder.

ncunit's People

Watchers

James Cloos avatar danoloan avatar

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.