Medical Imaging Interaction Toolkit  2026.06.00
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkTestingMacros_h
14 #define mitkTestingMacros_h
15 
16 #include <exception>
17 #include <iostream>
18 #include <string>
19 
20 #include <itkMacro.h>
21 #include <mitkException.h>
23 #include <mitkTestCaller.h>
24 #include <mitkTestManager.h>
25 
26 #include <cppunit/extensions/HelperMacros.h>
27 #include <cppunit/ui/text/TestRunner.h>
28 
29 namespace mitk
30 {
32  class TestFailedException : public std::exception
33  {
34  public:
36  };
37 }
38 
44 #define MITK_TEST_OUTPUT_NO_ENDL(x) std::cout x;
45 
51 #define MITK_TEST_OUTPUT(x) MITK_TEST_OUTPUT_NO_ENDL(x << "\n")
52 
59 #define MITK_TEST_BEGIN(testName) \
60  std::string mitkTestName(#testName); \
61  mitk::TestManager::GetInstance()->Initialize(); \
62  try \
63  {
69 #define MITK_TEST_FAILED_MSG(MSG) \
70  MITK_TEST_OUTPUT(MSG) \
71  throw mitk::TestFailedException();
72 
78 #define MITK_TEST_END() \
79  } \
80  catch (const mitk::TestFailedException &) \
81  { \
82  MITK_TEST_OUTPUT(<< "Further test execution skipped.") \
83  mitk::TestManager::GetInstance()->TestFailed(); \
84  } \
85  catch (const std::exception &ex) \
86  { \
87  MITK_TEST_OUTPUT(<< "std::exception occurred " << ex.what()) \
88  mitk::TestManager::GetInstance()->TestFailed(); \
89  } \
90  if (mitk::TestManager::GetInstance()->NumberOfFailedTests() > 0) \
91  { \
92  MITK_TEST_OUTPUT(<< mitkTestName << ": [DONE FAILED] , subtests passed: " \
93  << mitk::TestManager::GetInstance()->NumberOfPassedTests() \
94  << " failed: " \
95  << mitk::TestManager::GetInstance()->NumberOfFailedTests()) \
96  return EXIT_FAILURE; \
97  } \
98  else \
99  { \
100  MITK_TEST_OUTPUT(<< mitkTestName << ": " << mitk::TestManager::GetInstance()->NumberOfPassedTests() \
101  << " tests [DONE PASSED]") \
102  return EXIT_SUCCESS; \
103  }
104 
105 #define MITK_TEST_CONDITION(COND, MSG) \
106  MITK_TEST_OUTPUT_NO_ENDL(<< MSG) \
107  if (!(COND)) \
108  { \
109  mitk::TestManager::GetInstance()->TestFailed(); \
110  MITK_TEST_OUTPUT(<< " [FAILED]\n" \
111  << "In " \
112  << __FILE__ \
113  << ", line " \
114  << __LINE__ \
115  << ": " #COND " : [FAILED]") \
116  } \
117  else \
118  { \
119  MITK_TEST_OUTPUT(<< " [PASSED]") \
120  mitk::TestManager::GetInstance()->TestPassed(); \
121  }
122 
123 #define MITK_TEST_CONDITION_REQUIRED(COND, MSG) \
124  MITK_TEST_OUTPUT_NO_ENDL(<< MSG) \
125  if (!(COND)) \
126  { \
127  MITK_TEST_FAILED_MSG(<< " [FAILED]\n" \
128  << " +--> in " \
129  << __FILE__ \
130  << ", line " \
131  << __LINE__ \
132  << ", expression is false: \"" #COND "\"") \
133  } \
134  else \
135  { \
136  MITK_TEST_OUTPUT(<< " [PASSED]") \
137  mitk::TestManager::GetInstance()->TestPassed(); \
138  }
139 
159 #define MITK_TEST_FOR_EXCEPTION_BEGIN(EXCEPTIONCLASS) \
160  try \
161  {
162 #define MITK_TEST_FOR_EXCEPTION_END(EXCEPTIONCLASS) \
163  mitk::TestManager::GetInstance()->TestFailed(); \
164  MITK_TEST_OUTPUT(<< "Expected an '" << #EXCEPTIONCLASS << "' exception. [FAILED]") \
165  } \
166  catch (const EXCEPTIONCLASS &) \
167  { \
168  MITK_TEST_OUTPUT(<< "Caught an expected '" << #EXCEPTIONCLASS << "' exception. [PASSED]") \
169  mitk::TestManager::GetInstance()->TestPassed(); \
170  }
171 
178 #define MITK_TEST_FOR_EXCEPTION(EXCEPTIONCLASS, STATEMENT) \
179  MITK_TEST_FOR_EXCEPTION_BEGIN(EXCEPTIONCLASS) \
180  STATEMENT; \
181  MITK_TEST_FOR_EXCEPTION_END(EXCEPTIONCLASS)
182 
197 #define MITK_ASSERT_EQUAL(EXPECTED, ACTUAL, MSG) \
198  if (((EXPECTED).IsNull()) || ((ACTUAL).IsNull())) \
199  { \
200  mitkThrow() << "mitk::Equal does not work with nullptr pointer input."; \
201  } \
202  CPPUNIT_ASSERT_MESSAGE(MSG, mitk::Equal(*(EXPECTED), *(ACTUAL), mitk::eps, true))
203 
218 #define MITK_TEST_NOT_EQUAL(OBJ1, OBJ2, MSG) \
219  CPPUNIT_ASSERT_MESSAGE(MSG, !mitk::Equal(*(OBJ1), *(OBJ2), mitk::eps, true))
220 
236 #define MITK_ASSERT_NOT_EQUAL(OBJ1, OBJ2, MSG) \
237  if (((OBJ1).IsNull()) || ((OBJ2).IsNull())) \
238  { \
239  mitkThrow() << "mitk::Equal does not work with nullptr pointer input."; \
240  } \
241  CPPUNIT_ASSERT_MESSAGE(MSG, !mitk::Equal(*(OBJ1), *(OBJ2), mitk::eps, true))
242 
251 #define MITK_TEST_SUITE_REGISTRATION(TESTSUITE_NAME) \
252  int TESTSUITE_NAME##Test(int /*argc*/, char * /*argv*/ []) \
253  { \
254  int result = 0; \
255  try \
256  { \
257  CppUnit::TextUi::TestRunner runner; \
258  runner.addTest(TESTSUITE_NAME##TestSuite::suite()); \
259  result = runner.run() ? 0 : 1; \
260  } \
261  catch (const mitk::TestNotRunException& e) \
262  { \
263  MITK_WARN << "Test not run: " << e.GetDescription(); \
264  result = 77; \
265  } \
266  return result; \
267  }
268 
279 #define MITK_TEST(TESTMETHOD) CPPUNIT_TEST(TESTMETHOD)
280 
295 #define MITK_PARAMETERIZED_TEST(TESTMETHOD, ARGS) \
296  \
297  { \
298  std::string testName = #TESTMETHOD; \
299  for (std::size_t i = 0; i < ARGS.size(); ++i) \
300  { \
301  testName += "_" + ARGS[i]; \
302  } \
303  CPPUNIT_TEST_SUITE_ADD_TEST((new mitk::TestCaller<TestFixtureType>( \
304  context.getTestNameFor(testName), &TestFixtureType::TESTMETHOD, context.makeFixture(), args))); \
305  }
306 
323 #define MITK_PARAMETERIZED_CMD_LINE_TEST(TESTMETHOD) \
324  CPPUNIT_TEST_SUITE_ADD_TEST((new mitk::TestCaller<TestFixtureType>( \
325  context.getTestNameFor(#TESTMETHOD), &TestFixtureType::TESTMETHOD, context.makeFixture())));
326 
343 #define MITK_PARAMETERIZED_TEST_1(TESTMETHOD, arg1) \
344  \
345  { \
346  std::vector<std::string> args; \
347  args.push_back(arg1); \
348  MITK_PARAMETERIZED_TEST(TESTMETHOD, args) \
349  }
350 
368 #define MITK_PARAMETERIZED_TEST_2(TESTMETHOD, arg1, arg2) \
369  \
370  { \
371  std::vector<std::string> args; \
372  args.push_back(arg1); \
373  args.push_back(arg2); \
374  MITK_PARAMETERIZED_TEST(TESTMETHOD, args) \
375  }
376 #endif
Indicate a failed test.
Find image slices visible on a given plane.