Code Monkey home page Code Monkey logo

ctest's Introduction

ctest

Build Status Build status Documentation Status

About

This is a small testing framework inspired by gtest for use in C 89/90 projects. The library provides facilities for running and filtering tests.

Test Functions

In order to test your code with ctest, you write functions that have expectations or assertions about how your code should behave. For example, a test for a global function fibonacci might look like this:

void test_fib() {
	CTEST_ASSERT_INT_EQ(fibonacci(1), 1);
	CTEST_ASSERT_INT_EQ(fibonacci(5), 5);
	CTEST_ASSERT_INT_EQ(fibonacci(10), 55);
}

In order to run the test, it must be added to the test system:

int main(int argc, char** argv) {
	ctest_init();

	CTEST_ADD_TEST(test_fib);

	return ctest_run();
}

Test Fixtures

You can also make use of fixtures to reduce amount of setup and teardown code that would be shared by numerous tests. A fixture has a unique name and consists of a structure to hold the test data along with a setup and teardown method. The system will handle calling the correct setup and teardown method when running each test belonging to the fixture.

typedef struct {
	list* test_list;
} list_test_data;

void list_test_setup(list_test_data* fixture) {
	fixture->test_list = list_create();
}

void list_test_teardown(list_test_data* fixture) {
	list_destroy(fixture->test_list);
}

CTEST_FIXTURE(list_test, list_test_data, list_test_setup, list_test_teardown)

A test using a fixture receives a pointer to the fixture's structure when it is ran:

void test_list_push(list_test_data* fixture) {
	list_push_back(fixture->test_list, 42);

	CTEST_ASSERT_INT_EQ(list_size(fixture->test_list), 1)
}

Fixture tests are added in a similar way to test functions:

int main(int argc, char** argv) {
	ctest_init();

	CTEST_ADD_TEST_F(list_test, test_list_push);

	return ctest_run();
}

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.