Added SetUp/TearDown functions for test suites.
authorMarkus Kauppila
Mon, 11 Jul 2011 17:55:35 +0300
changeset 57542eead798346a
parent 5753 5e8c5282cac0
child 5755 35d4ae5dea90
Added SetUp/TearDown functions for test suites.
test/test-automation/SDL_test.c
test/test-automation/SDL_test.h
test/test-automation/runner.c
test/test-automation/testdummy/testdummy.c
     1.1 --- a/test/test-automation/SDL_test.c	Mon Jul 11 17:47:55 2011 +0300
     1.2 +++ b/test/test-automation/SDL_test.c	Mon Jul 11 17:55:35 2011 +0300
     1.3 @@ -1,4 +1,4 @@
     1.4 -/*
     1.5 +	/*
     1.6    Copyright (C) 2011 Markus Kauppila <markus.kauppila@gmail.com>
     1.7  
     1.8    This software is provided 'as-is', without any express or implied
     1.9 @@ -36,7 +36,7 @@
    1.10  int _testAssertsPassed;
    1.11  
    1.12  void
    1.13 -_TestCaseInit()
    1.14 +_InitTestEnvironment() // InitTestEnvironment
    1.15  {
    1.16  	_testReturnValue = 0;
    1.17  	_testAssertsFailed = 0;
    1.18 @@ -44,7 +44,7 @@
    1.19  }
    1.20  
    1.21  int
    1.22 -_TestCaseQuit()
    1.23 +_QuitTestEnvironment()
    1.24  {
    1.25  	AssertSummary(_testAssertsFailed + _testAssertsPassed,
    1.26                    _testAssertsFailed, _testAssertsPassed, time(0));
    1.27 @@ -76,7 +76,6 @@
    1.28  	   AssertWithValues("AssertEquals", 1, buf,
    1.29      		  actual, expected, time(0));
    1.30  
    1.31 -      _testReturnValue = 0;
    1.32        _testAssertsPassed++;
    1.33     }
    1.34  }
    1.35 @@ -98,7 +97,6 @@
    1.36     } else {
    1.37  		Assert("AssertTrue", 1, buf, time(0));
    1.38  
    1.39 -		_testReturnValue = 0;
    1.40  		_testAssertsPassed++;
    1.41     }
    1.42  }
    1.43 @@ -115,7 +113,6 @@
    1.44  
    1.45     Assert("AssertPass", 1, buf, time(0));
    1.46  
    1.47 -   _testReturnValue = 0;
    1.48     _testAssertsPassed++;
    1.49  }
    1.50  
     2.1 --- a/test/test-automation/SDL_test.h	Mon Jul 11 17:47:55 2011 +0300
     2.2 +++ b/test/test-automation/SDL_test.h	Mon Jul 11 17:55:35 2011 +0300
     2.3 @@ -25,6 +25,10 @@
     2.4  
     2.5  #include "logger.h"
     2.6  
     2.7 +#include "common/common.h"
     2.8 +#include "common/images.h"
     2.9 +
    2.10 +
    2.11  extern int _testReturnValue;
    2.12  extern int _testAssertsFailed;
    2.13  extern int _testAssertsPassed;
    2.14 @@ -52,18 +56,18 @@
    2.15  } TestCaseReference;
    2.16  
    2.17  /*!
    2.18 - *  Initialized the test case. Must be called at
    2.19 - *  the beginning of every test case, before doing
    2.20 - *  anything else.
    2.21 + *  Initialized the test environment such as asserts. Must be called at
    2.22 + *  the beginning of every test case, before doing anything else.
    2.23   */
    2.24 -void _TestCaseInit();
    2.25 +void _InitTestEnvironment();
    2.26  
    2.27  /*!
    2.28 - *  Deinitializes and exits the test case
    2.29 + *  Deinitializes the test environment and
    2.30 + *  returns the result of the test (pass or failure)
    2.31   *
    2.32   * \return 0 if test succeeded, otherwise 1
    2.33   */
    2.34 -int _TestCaseQuit();
    2.35 +int _QuitTestEnvironment();
    2.36  
    2.37  /*!
    2.38   *  Assert function. Tests if the expected value equals the actual value, then
    2.39 @@ -74,6 +78,7 @@
    2.40   * \param message Message that will be printed if assert fails
    2.41   */
    2.42  void AssertEquals(const int expected, const int actual, char *message, ...);
    2.43 +
    2.44  /*!
    2.45   *  Assert function. Tests if the given condition is true. True in
    2.46   *  this case means non-zero value. If the condition is true, the
     3.1 --- a/test/test-automation/runner.c	Mon Jul 11 17:47:55 2011 +0300
     3.2 +++ b/test/test-automation/runner.c	Mon Jul 11 17:55:35 2011 +0300
     3.3 @@ -39,9 +39,13 @@
     3.4  //!< Function pointer to a test case function
     3.5  typedef void (*TestCaseFp)(void *arg);
     3.6  //!< Function pointer to a test case init function
     3.7 -typedef void (*TestCaseInitFp)(void);
     3.8 +typedef void (*InitTestInvironmentFp)(void);
     3.9  //!< Function pointer to a test case quit function
    3.10 -typedef int  (*TestCaseQuitFp)(void);
    3.11 +typedef int  (*QuitTestInvironmentFp)(void);
    3.12 +//!< Function pointer to a test case set up function
    3.13 +typedef void (*TestCaseSetUpFp)(void *arg);
    3.14 +//!< Function pointer to a test case tear down function
    3.15 +typedef void  (*TestCaseTearDownFp)(void *arg);
    3.16  
    3.17  
    3.18  //!< Flag for executing tests in-process
    3.19 @@ -105,20 +109,24 @@
    3.20  	long requirements;
    3.21  	long timeout;
    3.22  
    3.23 -	TestCaseInitFp testCaseInit;
    3.24 +	InitTestInvironmentFp initTestEnvironment;
    3.25 +	TestCaseSetUpFp testSetUp;
    3.26  	TestCaseFp testCase;
    3.27 -	TestCaseQuitFp testCaseQuit;
    3.28 +	TestCaseTearDownFp testTearDown;
    3.29 + 	QuitTestInvironmentFp quitTestEnvironment;
    3.30  
    3.31  	struct TestCaseItem *next;
    3.32  } TestCase;
    3.33  
    3.34  
    3.35 -
    3.36  /*! Some function prototypes. Add the rest of functions and move to runner.h */
    3.37  TestCaseFp LoadTestCaseFunction(void *suite, char *testName);
    3.38 -TestCaseInitFp LoadTestCaseInitFunction(void *suite);
    3.39 -TestCaseQuitFp LoadTestCaseQuitFunction(void *suite);
    3.40 +InitTestInvironmentFp LoadInitTestInvironmentFunction(void *suite);
    3.41 +QuitTestInvironmentFp LoadQuitTestInvironmentFunction(void *suite);
    3.42  TestCaseReference **QueryTestCaseReferences(void *library);
    3.43 +TestCaseSetUpFp LoadTestSetUpFunction(void *suite);
    3.44 +TestCaseTearDownFp LoadTestTearDownFunction(void *suite);
    3.45 +
    3.46  
    3.47  /*! Pointers to selected logger implementation */
    3.48  RunStartedFp RunStarted = NULL;
    3.49 @@ -159,18 +167,26 @@
    3.50  			void *suite = suiteReference->library;
    3.51  
    3.52  			// Load test case functions
    3.53 -			TestCaseInitFp testCaseInit = LoadTestCaseInitFunction(suiteReference->library);
    3.54 -			TestCaseQuitFp testCaseQuit = LoadTestCaseQuitFunction(suiteReference->library);
    3.55 -			TestCaseFp testCase = (TestCaseFp) LoadTestCaseFunction(suiteReference->library, testReference->name);
    3.56 +			InitTestInvironmentFp initTestEnvironment = LoadInitTestInvironmentFunction(suiteReference->library);
    3.57 +			QuitTestInvironmentFp quitTestEnvironment = LoadQuitTestInvironmentFunction(suiteReference->library);
    3.58 +
    3.59 +			TestCaseSetUpFp testSetUp = LoadTestSetUpFunction(suiteReference->library);
    3.60 +			TestCaseTearDownFp testTearDown = LoadTestTearDownFunction(suiteReference->library);
    3.61 +
    3.62 +			TestCaseFp testCase = LoadTestCaseFunction(suiteReference->library, testReference->name);
    3.63  
    3.64  			// Do the filtering
    3.65  			if(FilterTestCase(testReference)) {
    3.66  				TestCase *item = SDL_malloc(sizeof(TestCase));
    3.67  				memset(item, 0, sizeof(TestCase));
    3.68  
    3.69 -				item->testCaseInit = testCaseInit;
    3.70 +				item->initTestEnvironment = initTestEnvironment;
    3.71 +				item->quitTestEnvironment = quitTestEnvironment;
    3.72 +
    3.73 +				item->testSetUp = testSetUp;
    3.74 +				item->testTearDown = testTearDown;
    3.75 +
    3.76  				item->testCase = testCase;
    3.77 -				item->testCaseQuit = testCaseQuit;
    3.78  
    3.79  				// copy suite name
    3.80  				int length = SDL_strlen(suiteReference->name) + 1;
    3.81 @@ -287,7 +303,9 @@
    3.82  
    3.83  	Entry *entry = NULL;
    3.84  	if(!directory) {
    3.85 -		perror("Couldn't open test suite directory!");
    3.86 +		fprintf(stderr, "Failed to open test suite directory: %s\n", directoryName);
    3.87 +		perror("Error message");
    3.88 +		exit(1);
    3.89  	}
    3.90  
    3.91  	while(entry = readdir(directory)) {
    3.92 @@ -458,42 +476,82 @@
    3.93  
    3.94  
    3.95  /*!
    3.96 - * Loads function that initialises the test case from the
    3.97 - * given test suite.
    3.98 + * Loads function that sets up a fixture for a test case. Note: if there's
    3.99 + * no SetUp function present in the suite the function will return NULL.
   3.100   *
   3.101   * \param suite Used test suite
   3.102   *
   3.103 - * \return Function pointer (TestCaseInit) which points to loaded init function. NULL if function fails.
   3.104 + * \return Function pointer to test case's set up function
   3.105   */
   3.106 -TestCaseInitFp
   3.107 -LoadTestCaseInitFunction(void *suite) {
   3.108 -	TestCaseInitFp testCaseInit = (TestCaseInitFp) SDL_LoadFunction(suite, "_TestCaseInit");
   3.109 -	if(testCaseInit == NULL) {
   3.110 -		fprintf(stderr, "Loading TestCaseInit function failed, testCaseInit == NULL\n");
   3.111 +TestCaseSetUpFp
   3.112 +LoadTestSetUpFunction(void *suite) {
   3.113 +	TestCaseSetUpFp testSetUp = (TestCaseSetUpFp) SDL_LoadFunction(suite, "SetUp");
   3.114 +	if(testSetUp == NULL) {
   3.115 +		fprintf(stderr, "Loading SetUp function failed, testSetUp == NULL\n");
   3.116  		fprintf(stderr, "%s\n", SDL_GetError());
   3.117  	}
   3.118  
   3.119 -	return testCaseInit;
   3.120 +	return testSetUp;
   3.121  }
   3.122  
   3.123  
   3.124  /*!
   3.125 - * Loads function that deinitialises the executed test case from the
   3.126 - * given test suite.
   3.127 + * Loads function that tears down a fixture for a test case. Note: if there's
   3.128 + * no TearDown function present in the suite the function will return NULL.
   3.129 + *
   3.130 + * \param suite Used test suite
   3.131 + *
   3.132 + * \return Function pointer to test case's tear down function
   3.133 + */
   3.134 +TestCaseTearDownFp
   3.135 +LoadTestTearDownFunction(void *suite) {
   3.136 +	TestCaseTearDownFp testTearDown = (TestCaseTearDownFp) SDL_LoadFunction(suite, "TearDown");
   3.137 +	if(testTearDown == NULL) {
   3.138 +		fprintf(stderr, "Loading TearDown function failed, testTearDown == NULL\n");
   3.139 +		fprintf(stderr, "%s\n", SDL_GetError());
   3.140 +	}
   3.141 +
   3.142 +	return testTearDown;
   3.143 +}
   3.144 +
   3.145 +
   3.146 +/*!
   3.147 + * Loads function that initialises the test environment for
   3.148 + * a test case in the given suite.
   3.149   *
   3.150   * \param suite Used test suite
   3.151   *
   3.152 - * \return Function pointer (TestCaseInit) which points to loaded init function. NULL if function fails.
   3.153 + * \return Function pointer (InitTestInvironmentFp) which points to loaded init function. NULL if function fails.
   3.154   */
   3.155 -TestCaseQuitFp
   3.156 -LoadTestCaseQuitFunction(void *suite) {
   3.157 -	TestCaseQuitFp testCaseQuit = (TestCaseQuitFp) SDL_LoadFunction(suite, "_TestCaseQuit");
   3.158 -	if(testCaseQuit == NULL) {
   3.159 -		fprintf(stderr, "Loading TestCaseQuit function failed, testCaseQuit == NULL\n");
   3.160 +InitTestInvironmentFp
   3.161 +LoadInitTestInvironmentFunction(void *suite) {
   3.162 +	InitTestInvironmentFp testEnvInit = (InitTestInvironmentFp) SDL_LoadFunction(suite, "_InitTestEnvironment");
   3.163 +	if(testEnvInit == NULL) {
   3.164 +		fprintf(stderr, "Loading _InitTestInvironment function failed, testEnvInit == NULL\n");
   3.165  		fprintf(stderr, "%s\n", SDL_GetError());
   3.166  	}
   3.167  
   3.168 -	return testCaseQuit;
   3.169 +	return testEnvInit;
   3.170 +}
   3.171 +
   3.172 +
   3.173 +/*!
   3.174 + * Loads function that deinitialises the test environment (and returns
   3.175 + * the test case's result) created for the test case in the given suite.
   3.176 + *
   3.177 + * \param suite Used test suite
   3.178 + *
   3.179 + * \return Function pointer (QuitTestInvironmentFp) which points to loaded init function. NULL if function fails.
   3.180 + */
   3.181 +QuitTestInvironmentFp
   3.182 +LoadQuitTestInvironmentFunction(void *suite) {
   3.183 +	QuitTestInvironmentFp testEnvQuit = (QuitTestInvironmentFp) SDL_LoadFunction(suite, "_QuitTestEnvironment");
   3.184 +	if(testEnvQuit == NULL) {
   3.185 +		fprintf(stderr, "Loading _QuitTestEnvironment function failed, testEnvQuit == NULL\n");
   3.186 +		fprintf(stderr, "%s\n", SDL_GetError());
   3.187 +	}
   3.188 +
   3.189 +	return testEnvQuit;
   3.190  }
   3.191  
   3.192  
   3.193 @@ -536,19 +594,37 @@
   3.194  ExecuteTest(TestCase *testItem) {
   3.195  	int retVal = 1;
   3.196  	if(execute_inproc) {
   3.197 -		testItem->testCaseInit();
   3.198 +		testItem->initTestEnvironment();
   3.199 +
   3.200 +		if(testItem->testSetUp) {
   3.201 +			testItem->testSetUp(0x0);
   3.202 +		}
   3.203  
   3.204  		testItem->testCase(0x0);
   3.205  
   3.206 -		retVal = testItem->testCaseQuit();
   3.207 +		if(testItem->testTearDown) {
   3.208 +			testItem->testTearDown(0x0);
   3.209 +		}
   3.210 +
   3.211 +		retVal = testItem->quitTestEnvironment();
   3.212  	} else {
   3.213  		int childpid = fork();
   3.214  		if(childpid == 0) {
   3.215 -			testItem->testCaseInit();
   3.216 +			testItem->initTestEnvironment();
   3.217 +
   3.218 +			if(testItem->testSetUp) {
   3.219 +				testItem->testSetUp(0x0);
   3.220 +			}
   3.221  
   3.222  			testItem->testCase(0x0);
   3.223  
   3.224 -			exit(testItem->testCaseQuit());
   3.225 +			// note: if test case is is aborted by some signal
   3.226 +			// then TearDown function won't be called
   3.227 +			if(testItem->testTearDown) {
   3.228 +				testItem->testTearDown(0x0);
   3.229 +			}
   3.230 +
   3.231 +			exit(testItem->quitTestEnvironment());
   3.232  		} else {
   3.233  			int stat_lock = -1;
   3.234  			int child = wait(&stat_lock);
     4.1 --- a/test/test-automation/testdummy/testdummy.c	Mon Jul 11 17:47:55 2011 +0300
     4.2 +++ b/test/test-automation/testdummy/testdummy.c	Mon Jul 11 17:55:35 2011 +0300
     4.3 @@ -21,7 +21,7 @@
     4.4  /*! \file
     4.5   * Dummy test suite for test runner. This can be used as a base for
     4.6   * writing new tests. Dummy suite also works as reference to using
     4.7 - * various asserts and (possible) other utilities.
     4.8 + * various asserts and other (possible) utilities.
     4.9   */
    4.10  
    4.11  #include <stdio.h>
    4.12 @@ -30,8 +30,6 @@
    4.13  
    4.14  #include "../SDL_test.h"
    4.15  
    4.16 -// \todo add some helpful commenting for possible test writers?
    4.17 -
    4.18  /* Test case references */
    4.19  static const TestCaseReference test1 =
    4.20  		(TestCaseReference){ "dummycase1", "description", TEST_ENABLED, 0, 0};
    4.21 @@ -52,6 +50,37 @@
    4.22  	return (TestCaseReference **)testSuite;
    4.23  }
    4.24  
    4.25 +/* Create test fixture */
    4.26 +
    4.27 +/*!
    4.28 + * SetUp function can be used to create a test fixture for test cases.
    4.29 + * The function will be called right before executing the test case.
    4.30 + *
    4.31 + * Note: this function is optional.
    4.32 + *
    4.33 + * \param arg parameters given to test. Usually NULL
    4.34 + */
    4.35 +void
    4.36 +SetUp(void *arg)
    4.37 +{
    4.38 +	// create test fixture,
    4.39 +	// for example, set up static variables used by test cases here
    4.40 +}
    4.41 +
    4.42 +/*!
    4.43 + * TearDown function can be used to destroy a test fixture for test cases.
    4.44 + * The function will be called right after executing the test case.
    4.45 + *
    4.46 + * Note: this function is optional.
    4.47 + *
    4.48 + * \param arg parameters given to test. Usually NULL
    4.49 + */
    4.50 +void
    4.51 +TearDown(void *arg)
    4.52 +{
    4.53 +	// destroy test fixture
    4.54 +}
    4.55 +
    4.56  /* Test case functions */
    4.57  void
    4.58  dummycase1(void *arg)