Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkTestingMacros.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef mitkTestingMacros_h
18 #define mitkTestingMacros_h
19 
20 #include <exception>
21 #include <iostream>
22 #include <string>
23 
24 #include <itkMacro.h>
25 #include <mitkException.h>
26 #include <mitkTestCaller.h>
27 #include <mitkTestManager.h>
28 
29 #include <cppunit/extensions/HelperMacros.h>
30 #include <cppunit/ui/text/TestRunner.h>
31 
32 namespace mitk
33 {
35  class TestFailedException : public std::exception
36  {
37  public:
39  };
40 }
41 
47 #define MITK_TEST_OUTPUT_NO_ENDL(x) std::cout x;
48 
54 #define MITK_TEST_OUTPUT(x) MITK_TEST_OUTPUT_NO_ENDL(x << "\n")
55 
63 #define MITK_TEST_BEGIN(testName) \
64  std::string mitkTestName(#testName); \
65  mitk::TestManager::GetInstance()->Initialize(); \
66  try \
67  {
68 
74 #define MITK_TEST_FAILED_MSG(MSG) \
75  MITK_TEST_OUTPUT(MSG) \
76  throw mitk::TestFailedException();
77 
84 #define MITK_TEST_END() \
85  } \
86  catch (const mitk::TestFailedException &) \
87  { \
88  MITK_TEST_OUTPUT(<< "Further test execution skipped.") \
89  mitk::TestManager::GetInstance()->TestFailed(); \
90  } \
91  catch (const std::exception &ex) \
92  { \
93  MITK_TEST_OUTPUT(<< "std::exception occured " << ex.what()) \
94  mitk::TestManager::GetInstance()->TestFailed(); \
95  } \
96  if (mitk::TestManager::GetInstance()->NumberOfFailedTests() > 0) \
97  { \
98  MITK_TEST_OUTPUT(<< mitkTestName << ": [DONE FAILED] , subtests passed: " \
99  << mitk::TestManager::GetInstance()->NumberOfPassedTests() \
100  << " failed: " \
101  << mitk::TestManager::GetInstance()->NumberOfFailedTests()) \
102  return EXIT_FAILURE; \
103  } \
104  else \
105  { \
106  MITK_TEST_OUTPUT(<< mitkTestName << ": " << mitk::TestManager::GetInstance()->NumberOfPassedTests() \
107  << " tests [DONE PASSED]") \
108  return EXIT_SUCCESS; \
109  }
110 
114 #define MITK_TEST_CONDITION(COND, MSG) \
115  MITK_TEST_OUTPUT_NO_ENDL(<< MSG) \
116  if (!(COND)) \
117  { \
118  mitk::TestManager::GetInstance()->TestFailed(); \
119  MITK_TEST_OUTPUT(<< " [FAILED]\n" \
120  << "In " \
121  << __FILE__ \
122  << ", line " \
123  << __LINE__ \
124  << ": " #COND " : [FAILED]") \
125  } \
126  else \
127  { \
128  MITK_TEST_OUTPUT(<< " [PASSED]") \
129  mitk::TestManager::GetInstance()->TestPassed(); \
130  }
131 
135 #define MITK_TEST_CONDITION_REQUIRED(COND, MSG) \
136  MITK_TEST_OUTPUT_NO_ENDL(<< MSG) \
137  if (!(COND)) \
138  { \
139  MITK_TEST_FAILED_MSG(<< " [FAILED]\n" \
140  << " +--> in " \
141  << __FILE__ \
142  << ", line " \
143  << __LINE__ \
144  << ", expression is false: \"" #COND "\"") \
145  } \
146  else \
147  { \
148  MITK_TEST_OUTPUT(<< " [PASSED]") \
149  mitk::TestManager::GetInstance()->TestPassed(); \
150  }
151 
172 #define MITK_TEST_FOR_EXCEPTION_BEGIN(EXCEPTIONCLASS) \
173  try \
174  {
175 
178 #define MITK_TEST_FOR_EXCEPTION_END(EXCEPTIONCLASS) \
179  mitk::TestManager::GetInstance()->TestFailed(); \
180  MITK_TEST_OUTPUT(<< "Expected an '" << #EXCEPTIONCLASS << "' exception. [FAILED]") \
181  } \
182  catch (EXCEPTIONCLASS) \
183  { \
184  MITK_TEST_OUTPUT(<< "Caught an expected '" << #EXCEPTIONCLASS << "' exception. [PASSED]") \
185  mitk::TestManager::GetInstance()->TestPassed(); \
186  }
187 
195 #define MITK_TEST_FOR_EXCEPTION(EXCEPTIONCLASS, STATEMENT) \
196  MITK_TEST_FOR_EXCEPTION_BEGIN(EXCEPTIONCLASS) \
197  STATEMENT; \
198  MITK_TEST_FOR_EXCEPTION_END(EXCEPTIONCLASS)
199 
215 #define MITK_TEST_EQUAL(OBJ1, OBJ2, MSG) \
216  MITK_TEST_CONDITION_REQUIRED(mitk::Equal(OBJ1, OBJ2, mitk::eps, true) == true, MSG)
217 
232 #define MITK_ASSERT_EQUAL(EXPECTED, ACTUAL, MSG) \
233  if (((EXPECTED).IsNull()) || ((ACTUAL).IsNull())) \
234  { \
235  mitkThrow() << "mitk::Equal does not work with NULL pointer input."; \
236  } \
237  CPPUNIT_ASSERT_MESSAGE(MSG, mitk::Equal(*(EXPECTED), *(ACTUAL), mitk::eps, true))
238 
255 #define MITK_TEST_NOT_EQUAL(OBJ1, OBJ2, MSG) \
256  CPPUNIT_ASSERT_MESSAGE(MSG, !mitk::Equal(*(OBJ1), *(OBJ2), mitk::eps, true))
257 
273 #define MITK_ASSERT_NOT_EQUAL(OBJ1, OBJ2, MSG) \
274  if (((OBJ1).IsNull()) || ((OBJ2).IsNull())) \
275  { \
276  mitkThrow() << "mitk::Equal does not work with NULL pointer input."; \
277  } \
278  CPPUNIT_ASSERT_MESSAGE(MSG, !mitk::Equal(*(OBJ1), *(OBJ2), mitk::eps, true))
279 
288 #define MITK_TEST_SUITE_REGISTRATION(TESTSUITE_NAME) \
289  int TESTSUITE_NAME##Test(int /*argc*/, char * /*argv*/ []) \
290  { \
291  CppUnit::TextUi::TestRunner runner; \
292  runner.addTest(TESTSUITE_NAME##TestSuite::suite()); \
293  return runner.run() ? 0 : 1; \
294  }
295 
306 #define MITK_TEST(TESTMETHOD) CPPUNIT_TEST(TESTMETHOD)
307 
322 #define MITK_PARAMETERIZED_TEST(TESTMETHOD, ARGS) \
323  \
324  { \
325  std::string testName = #TESTMETHOD; \
326  for (std::size_t i = 0; i < ARGS.size(); ++i) \
327  { \
328  testName += "_" + ARGS[i]; \
329  } \
330  CPPUNIT_TEST_SUITE_ADD_TEST((new mitk::TestCaller<TestFixtureType>( \
331  context.getTestNameFor(testName), &TestFixtureType::TESTMETHOD, context.makeFixture(), args))); \
332  }
333 
350 #define MITK_PARAMETERIZED_CMD_LINE_TEST(TESTMETHOD) \
351  CPPUNIT_TEST_SUITE_ADD_TEST((new mitk::TestCaller<TestFixtureType>( \
352  context.getTestNameFor(#TESTMETHOD), &TestFixtureType::TESTMETHOD, context.makeFixture())));
353 
370 #define MITK_PARAMETERIZED_TEST_1(TESTMETHOD, arg1) \
371  \
372  { \
373  std::vector<std::string> args; \
374  args.push_back(arg1); \
375  MITK_PARAMETERIZED_TEST(TESTMETHOD, args) \
376  }
377 
394 #define MITK_PARAMETERIZED_TEST_2(TESTMETHOD, arg1, arg2) \
395  \
396  { \
397  std::vector<std::string> args; \
398  args.push_back(arg1); \
399  args.push_back(arg2); \
400  MITK_PARAMETERIZED_TEST(TESTMETHOD, args) \
401  }
402 #endif
DataCollection - Class to facilitate loading/accessing structured data.
Indicate a failed test.