Primitive pawn unit testing tools

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1229
Gender:
United States of America

Primitive pawn unit testing tools

Post by Archive-Poster »

Unit testing tool

I am new in pawn, so when i was making of curl module , I had a need for a unit testing tool. This tool was inspired by cutest and check. Actually this tool is uncompleted, there is no way to test async code, there is not different outstreams and options (so please contribute).

How does it works
so first of all you have to include <utest.inc>
define list the unit tests by using macro TEST_LIST .It has to be unique, structure is

Code: Select all

TEST_LIST = {
  { test_name, test_description },
  ...
  TEST_LIST_END /* this is required */
};
you have to create each "test_name", by using macro START_TEST and END_TEST

Code: Select all

START_TEST(test_name) {
    new Handle:curl = curl_init()
    ASSERT_TRUE(INVALID_HANDLE != curl)
    ASSERT_TRUE(curl_close(curl))
} END_TEST
so START_TEST(some_name) define function like

Code: Select all

public __some_name(__test[_TestT]) {
where __test[__TesT] is current instance of test
__test collects assert's results

then you can do asserts
RUN all test by using macro UTEST_RUN(option)

Example:

Code: Select all

#include <amxmodx>
#include <utest>

stock my_max(a, b)
{
    return a > b ? a : b
}

stock my_min(a, b)
{
    return a > b ? b : a
}

TEST_LIST = {
    { "test1", "test max function" },
    { "test2", "test min function" },
    { "test3", "TODO" },
    TEST_LIST_END
};

START_TEST(test1) {
    ASSERT_INT_EQ(my_max(1, 3), 3)
    ASSERT_TRUE(my_max(1, 1) == 1)
    ASSERT_FALSE(my_max(-1, 1) == -1)
} END_TEST

START_TEST(test2) {
    ASSERT_INT_EQ(my_min(1, 3), 1)
    ASSERT_TRUE(my_min(1, 1) == 1)
    ASSERT_FALSE(my_min(-1, 1) == 1)
    ASSERT_TRUE_MSG(true, "expected true !!")
} END_TEST

START_TEST(test3) {
    SKIP_TEST("IMPLEMENT ME!")
} END_TEST

public run_test()
{
    UTEST_RUN(UT_VERBOSE)
}

public plugin_init()
{
    register_plugin("some_unit_test", "1.0", "alldroll")
    set_task(2.0, "run_test")
} 
Output:

Code: Select all

======UTEST RUN=======
>>> Running test suite [test1]: test max function
+ [test1] OK #1
+ [test1] OK #2
+ [test1] OK #3
>>> Running test suite [test2]: test min function
+ [test2] OK #1
+ [test2] OK #2
+ [test2] OK #3
+ [test2] OK #4
>>> Running test suite [test3]: TODO
>>> Skip test suite test3 (IMPLEMENT ME!)
=======SUMMARY:========
Count of all unit tests: 3
Count of ok tests: 2
Count of failed tests: 0
Count of skipped tests: 1
SUCCESS: all unit tests have passed
==========END==========

Who is online

Users browsing this forum: No registered users and 0 guests