I had posted some notes on the MySQL test framework in my earlier post at http://blogs.sun.com/amitsaha/entry/the_mysql_test_framework
'mysql_client_test.c' which lives in the 'tests/' directory uses the MySQL C API to write various client side tests to be executed on the server. In this post, I will show how you can add your own test(s) to this file.
Why would you want to do that?
Adding your tests to this file enables you take advantage of the existing test framework to run your tests.
How does 'mysql_client_test.c' execute the tests?
In the main( ) function, this code snippet selects the tests to be run:
for ( ; *argv ; argv++)
18054 {
18055 for (fptr= my_tests; fptr->name; fptr++)
18056 {
18057 if (!strcmp(fptr->name, *argv))
18058 {
18059 (*fptr->function)();
18060 break;
18061 }
18062 }
18063 if (!fptr->name)
18064 {
18065 fprintf(stderr, "\n\nGiven test not found: '%s'\n", *argv);
18066 fprintf(stderr, "See legal test names with %s -T\n\nAborting!\n",
18067 my_progname);
18068 client_disconnect();
18069 free_defaults(defaults_argv);
18070 exit(1);
18071 }
where 'fptr' is defined as:
struct my_tests_st *fptr
Map-like data structure
'my_tests_st' implements a map-like data structure which 'maps' test names to the function pointers which points to functions which contain codes for carrying out the tests. It is defined as:
struct my_tests_st
{
const char *name;
void (*function)();
};
For eg.
static struct my_tests_st my_tests[]= {
17681 {"testse_create_table", testse_create_table }, /* Entry for the new test method introduced - Amit.Saha@sun.com*/
17682 { "disable_general_log", disable_general_log },
.
.
'mysql_client_test.c' can operate in 2 modes:
- Run all the tests which are defined in the file (when no arguments is specified)
- Run only the test which is specified in the argument list supplied to it. When the test is specified by its name, the structure above is looked up to find the appropriate function pointer and subsequently the function is called
- Write your test case in a new function and append it to 'mysql_client_test.c'
- Append the method signature to the file somewhere towards the beginning of the file (after the #include statements)
- Add the appropriate mapping in the static struct my_tests_st my_tests[] either at the end or beginning
- Do a 'make' in the tests/ sub-directory
- To run only the new test case, you just added:
$./mysql_client_test function_name
Demo
Say, I want to create a new test case which will test whether I can create a table in my new custom storage engine- TESTSE.
So I will append the function to 'mysql_client_test.c':
/* Test to create a table using the TestSE */
void testse_create_table()
{
myheader("testse_create_table");
char QUERY[]="CREATE TABLE demo2(car CHAR(15) NOT NULL, year INTEGER NOT NULL) ENGINE=TESTSE";
printf("\n Client connected to MySQL Server %s \n", mysql_get_server_info(mysql));
printf("\n Connection Description %s\n", mysql_get_host_info(mysql));
if(mysql_query(mysql, QUERY)!=0){
printf("\n Query %s failed", QUERY);
}
else
{
printf("Query succeeded\n");
}
}
Now add the following to static struct my_tests_st my_tests
{"testse_create_table", testse_create_table },
- Add a function signature for your function- void testse_create_table() somewhere before the place where the above mapping occurs
- Do a 'make'
- Now execute your test by doing:
$ ./mysql_client_test --no-defaults --socket=/tmp/mysql_9090.sock --user=root testse_create_table
Based on your system settings you may have to supply different options and values. The various options available can be seen by:
$ ./mysql_client_test --help
The test output should be something like:
##################################### client_connect ##################################### Establishing a connection to '' ...OK Connected to MySQL server version: 5.1.24-rc (50124) Creating a test database 'client_test_db' ...OK ##################################### 1 of (1/1): testse_create_table ##################################### Client connected to MySQL Server 5.1.24-rc Connection Description Localhost via UNIX socket Query succeeded ##################################### client_disconnect ##################################### dropping the test database 'client_test_db' ...OK closing the connection ...OK All '1' tests were successful (in '1' iterations) Total execution time: 0 SECS


