Skip to content

roshanlam/pareeksha

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pareeksha

A lightweight C library for writing and running unit tests. It basically provides a simple and flexible testing framework to do things like define and execute tests, perform assertions, and report test results.

Current Features

  • Easy test definition: Define test functions and use assertion macros to verify expected behavior.
  • Setup and teardown functions: Execute setup and teardown operations before and after each test.
  • Test result reporting: Print test results with pass/fail status and error messages.

Usage

  1. Include the pareeksha.h header file in your test files.
#include "pareeksha.h"
  1. Write your test functions using the provided assertion macros EXPECT and ASSERT. For example
void test_addition() {
    int result = 2 + 2;
    EXPECT(result == 4, "Addition test");
    EXPECT(result != 5, "Addition test - Result should not be 5");
}
  1. Optionally, define setup and teardown functions to be executed before and after each test:
void setup() {
    // Perform setup operations here
}

void teardown() {
    // Perform teardown operations here
}
  1. Register your test functions, setup, and teardown functions using the add_test function:
add_test("Addition", test_addition, setup, teardown);
  1. Use the run_tests function to execute all the registered tests
    int failures = run_tests();
  1. Review the test results and the total number of failures.
printf("Total failures: %d\n", failures);

Assertion Macros

The Testing Framework provides the following assertion macros to check conditions and report test failures:

EXPECT(condition, message)
  • Checks if the given condition is true. If the condition is false, the test will continue execution and report a failure.
ASSERT(condition, message)
  • Checks if the given condition is true. If the condition is false, the test will immediately stop execution and report a failure.

Example

Here's a simple example of how to use Pareeksha:

#include "pareeksha.h"

// Example test functions
void test_addition() {
    int result = 2 + 2;
    EXPECT(result == 4, "Addition test");
    EXPECT(result != 5, "Addition test - Result should not be 5");
}

void test_subtraction() {
    int result = 5 - 3;
    ASSERT(result == 2, "Subtraction test");
    ASSERT(result != 10, "Subtraction test - Result should not be 10");
}

// Example setup and teardown functions
void setup() {
    printf("Running setup...\n");
    // Perform setup operations here
}

void teardown() {
    printf("Running teardown...\n");
    // Perform teardown operations here
}

int main() {
  const char *log_file_path = "test_results.json";

  add_test("Addition", test_addition, NULL, NULL);
  add_test("Subtraction", test_subtraction, setup, teardown);

  int failures = run_tests(log_file_path);
  printf("Total failures: %d\n", failures);

  return failures > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

License

This project is licensed under the MIT License.

About

A simple and easy to use testing framework for c

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages