Medical Imaging Interaction Toolkit  2024.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 
60 #define MITK_TEST_BEGIN(testName) \
61  std::string mitkTestName(#testName); \
62  mitk::TestManager::GetInstance()->Initialize(); \
63  try \
64  {
65 
71 #define MITK_TEST_FAILED_MSG(MSG) \
72  MITK_TEST_OUTPUT(MSG) \
73  throw mitk::TestFailedException();
74 
81 #define MITK_TEST_END() \
82  } \
83  catch (const mitk::TestFailedException &) \
84  { \
85  MITK_TEST_OUTPUT(<< "Further test execution skipped.") \
86  mitk::TestManager::GetInstance()->TestFailed(); \
87  } \
88  catch (const std::exception &ex) \
89  { \
90  MITK_TEST_OUTPUT(<< "std::exception occurred " << ex.what()) \
91  mitk::TestManager::GetInstance()->TestFailed(); \
92  } \
93  if (mitk::TestManager::GetInstance()->NumberOfFailedTests() > 0) \
94  { \
95  MITK_TEST_OUTPUT(<< mitkTestName << ": [DONE FAILED] , subtests passed: " \
96  << mitk::TestManager::GetInstance()->NumberOfPassedTests() \
97  << " failed: " \
98  << mitk::TestManager::GetInstance()->NumberOfFailedTests()) \
99  return EXIT_FAILURE; \
100  } \
101  else \
102  { \
103  MITK_TEST_OUTPUT(<< mitkTestName << ": " << mitk::TestManager::GetInstance()->NumberOfPassedTests() \
104  << " tests [DONE PASSED]") \
105  return EXIT_SUCCESS; \
106  }
107 
111 #define MITK_TEST_CONDITION(COND, MSG) \
112  MITK_TEST_OUTPUT_NO_ENDL(<< MSG) \
113  if (!(COND)) \
114  { \
115  mitk::TestManager::GetInstance()->TestFailed(); \
116  MITK_TEST_OUTPUT(<< " [FAILED]\n" \
117  << "In " \
118  << __FILE__ \
119  << ", line " \
120  << __LINE__ \
121  << ": " #COND " : [FAILED]") \
122  } \
123  else \
124  { \
125  MITK_TEST_OUTPUT(<< " [PASSED]") \
126  mitk::TestManager::GetInstance()->TestPassed(); \
127  }
128 
132 #define MITK_TEST_CONDITION_REQUIRED(COND, MSG) \
133  MITK_TEST_OUTPUT_NO_ENDL(<< MSG) \
134  if (!(COND)) \
135  { \
136  MITK_TEST_FAILED_MSG(<< " [FAILED]\n" \
137  << " +--> in " \
138  << __FILE__ \
139  << ", line " \
140  << __LINE__ \
141  << ", expression is false: \"" #COND "\"") \
142  } \
143  else \
144  { \
145  MITK_TEST_OUTPUT(<< " [PASSED]") \
146  mitk::TestManager::GetInstance()->TestPassed(); \
147  }
148 
169 #define MITK_TEST_FOR_EXCEPTION_BEGIN(EXCEPTIONCLASS) \
170  try \
171  {
172 
175 #define MITK_TEST_FOR_EXCEPTION_END(EXCEPTIONCLASS) \
176  mitk::TestManager::GetInstance()->TestFailed(); \
177  MITK_TEST_OUTPUT(<< "Expected an '" << #EXCEPTIONCLASS << "' exception. [FAILED]") \
178  } \
179  catch (const EXCEPTIONCLASS &) \
180  { \
181  MITK_TEST_OUTPUT(<< "Caught an expected '" << #EXCEPTIONCLASS << "' exception. [PASSED]") \
182  mitk::TestManager::GetInstance()->TestPassed(); \
183  }
184 
192 #define MITK_TEST_FOR_EXCEPTION(EXCEPTIONCLASS, STATEMENT) \
193  MITK_TEST_FOR_EXCEPTION_BEGIN(EXCEPTIONCLASS) \
194  STATEMENT; \
195  MITK_TEST_FOR_EXCEPTION_END(EXCEPTIONCLASS)
196 
211 #define MITK_ASSERT_EQUAL(EXPECTED, ACTUAL, MSG) \
212  if (((EXPECTED).IsNull()) || ((ACTUAL).IsNull())) \
213  { \
214  mitkThrow() << "mitk::Equal does not work with nullptr pointer input."; \
215  } \
216  CPPUNIT_ASSERT_MESSAGE(MSG, mitk::Equal(*(EXPECTED), *(ACTUAL), mitk::eps, true))
217 
234 #define MITK_TEST_NOT_EQUAL(OBJ1, OBJ2, MSG) \
235  CPPUNIT_ASSERT_MESSAGE(MSG, !mitk::Equal(*(OBJ1), *(OBJ2), mitk::eps, true))
236 
252 #define MITK_ASSERT_NOT_EQUAL(OBJ1, OBJ2, MSG) \
253  if (((OBJ1).IsNull()) || ((OBJ2).IsNull())) \
254  { \
255  mitkThrow() << "mitk::Equal does not work with nullptr pointer input."; \
256  } \
257  CPPUNIT_ASSERT_MESSAGE(MSG, !mitk::Equal(*(OBJ1), *(OBJ2), mitk::eps, true))
258 
267 #define MITK_TEST_SUITE_REGISTRATION(TESTSUITE_NAME) \
268  int TESTSUITE_NAME##Test(int /*argc*/, char * /*argv*/ []) \
269  { \
270  int result = 0; \
271  try \
272  { \
273  CppUnit::TextUi::TestRunner runner; \
274  runner.addTest(TESTSUITE_NAME##TestSuite::suite()); \
275  result = runner.run() ? 0 : 1; \
276  } \
277  catch (const mitk::TestNotRunException& e) \
278  { \
279  MITK_WARN << "Test not run: " << e.GetDescription(); \
280  result = 77; \
281  } \
282  return result; \
283  }
284 
295 #define MITK_TEST(TESTMETHOD) CPPUNIT_TEST(TESTMETHOD)
296 
311 #define MITK_PARAMETERIZED_TEST(TESTMETHOD, ARGS) \
312  \
313  { \
314  std::string testName = #TESTMETHOD; \
315  for (std::size_t i = 0; i < ARGS.size(); ++i) \
316  { \
317  testName += "_" + ARGS[i]; \
318  } \
319  CPPUNIT_TEST_SUITE_ADD_TEST((new mitk::TestCaller<TestFixtureType>( \
320  context.getTestNameFor(testName), &TestFixtureType::TESTMETHOD, context.makeFixture(), args))); \
321  }
322 
339 #define MITK_PARAMETERIZED_CMD_LINE_TEST(TESTMETHOD) \
340  CPPUNIT_TEST_SUITE_ADD_TEST((new mitk::TestCaller<TestFixtureType>( \
341  context.getTestNameFor(#TESTMETHOD), &TestFixtureType::TESTMETHOD, context.makeFixture())));
342 
359 #define MITK_PARAMETERIZED_TEST_1(TESTMETHOD, arg1) \
360  \
361  { \
362  std::vector<std::string> args; \
363  args.push_back(arg1); \
364  MITK_PARAMETERIZED_TEST(TESTMETHOD, args) \
365  }
366 
384 #define MITK_PARAMETERIZED_TEST_2(TESTMETHOD, arg1, arg2) \
385  \
386  { \
387  std::vector<std::string> args; \
388  args.push_back(arg1); \
389  args.push_back(arg2); \
390  MITK_PARAMETERIZED_TEST(TESTMETHOD, args) \
391  }
392 #endif
mitkException.h
mitk::TestFailedException::TestFailedException
TestFailedException()
Definition: mitkTestingMacros.h:35
mitkTestManager.h
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitkTestCaller.h
mitk::TestFailedException
Indicate a failed test.
Definition: mitkTestingMacros.h:32
mitkTestNotRunException.h