KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
test_case.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Pooyan Dadvand
11 //
12 //
13 
14 #pragma once
15 
16 // System includes
17 #include <string>
18 #include <iostream>
19 
20 // External includes
21 
22 // Project includes
23 #include "testing/tester.h"
25 
26 namespace Kratos::Testing
27 {
28 namespace Internals
29 {
30  template <typename TestType> class RegisterThisTest {
31  public:
32  explicit RegisterThisTest(bool IsDisabled=false)
33  {
34  TestType* p_test = new TestType;
35  if (IsDisabled)
36  p_test->Disable();
37  Tester::AddTestCase(p_test);
38  }
39  };
40 
41 }
42 
45 
48 
50 
52 class KRATOS_API(KRATOS_CORE) TestCase
53 {
54 public:
57 
61 
63  TestCase() = delete;
64 
66  TestCase(TestCase const& rOther) = delete;
67 
69  TestCase(std::string const& Name);
70 
72  virtual ~TestCase();
73 
77 
79  TestCase& operator=(TestCase const& rOther) = delete;
80 
84 
85  virtual void Reset();
86 
87  virtual void ResetResult();
88 
89  virtual void Setup();
90 
91  virtual void Run();
92 
93  virtual void Profile();
94 
95  virtual void TearDown();
96 
97  virtual void Enable();
98 
99  virtual void Disable();
100 
101  virtual void Select();
102 
103  virtual void UnSelect();
104 
108 
109  const std::string& Name() const;
110 
111  const TestCaseResult& GetResult() const;
112 
113  void SetResult(TestCaseResult const& TheResult);
114 
115  void SetResultOutput(std::string const& TheResultOutput);
116 
120 
121  virtual bool IsEnabled() const;
122 
123  virtual bool IsDisabled() const;
124 
125  virtual bool IsSelected() const;
126 
127  virtual bool IsDistributedTest() const;
128 
132 
134  virtual std::string Info() const;
135 
137  virtual void PrintInfo(std::ostream& rOStream) const;
138 
140  virtual void PrintData(std::ostream& rOStream) const;
141 
143 private:
146 
150 
151  const std::string mName;
152 
153  bool mIsEnabled;
154 
155  bool mIsSelected;
156 
157  TestCaseResult mResult;
158 
162 
163  virtual void TestFunction() = 0;
164 
166 }; // Class TestCase
167 
169 
172 
174 inline std::ostream& operator << (std::ostream& rOStream,
175  const TestCase& rThis)
176 {
177  rThis.PrintInfo(rOStream);
178  rOStream << std::endl;
179  rThis.PrintData(rOStream);
180 
181  return rOStream;
182 }
186 
187 
188 #define KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName) \
189  Test##TestCaseName
190 
191 #define KRATOS_TESTING_CONVERT_TO_STRING(Name) #Name
192 
193 #define KRATOS_TESTING_TEST_CASE_CLASS_BODY(TestCaseName,ParentName) \
194  public:\
195  KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName)() : ParentName(KRATOS_TESTING_CONVERT_TO_STRING(Test##TestCaseName)) {}\
196  private: \
197  void TestFunction() override; \
198  static const Internals::RegisterThisTest< KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName) > mDummy;
199 
200 #define KRATOS_TESTING_TEST_CASE_CLASS(TestCaseName,ParentName) \
201 class KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName) : public ParentName \
202  {\
203 KRATOS_TESTING_TEST_CASE_CLASS_BODY(TestCaseName,ParentName) \
204 };
205 
206 
207 // This macro creates a sub class of TestCase for given TestCaseName prepending a Test to it
208 // For example if the TestCaseName is "ModelPartConstruction" then the result is:
209 // class TestModelPartConstruction : public TestCase
210 // {
211 // public:
212 // TestModelPartConstruction() : TestCase("TestModelPartConstruction") {}
213 // private:
214 // void TestFunction() override;
215 // static Internals::RegisterThisTest<TestModelPartConstruction> mDummy;
216 // };
217 // Kratos::Testing::Internals::RegisterThisTest<TestModelPartConstruction>
218 // TestModelPartConstruction::mDummy;
219 // void TestModelPartConstruction::TestFunction()
220 //
221 #define KRATOS_TEST_CASE(TestCaseName) \
222 KRATOS_TESTING_TEST_CASE_CLASS(TestCaseName, TestCase) \
223 const Kratos::Testing::Internals::RegisterThisTest< KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName) > \
224  KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName)::mDummy; \
225 \
226 void KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName)::TestFunction()
227 
228 #define KRATOS_DISABLED_TEST_CASE(TestCaseName) \
229 KRATOS_TESTING_TEST_CASE_CLASS(TestCaseName, TestCase) \
230 const Kratos::Testing::Internals::RegisterThisTest< KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName) > \
231  KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName)::mDummy(true); \
232 \
233 void KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName)::TestFunction()
234 
235 // This macro creates a fixture sub class of TestCase for given FixtureName
236 // For example if the FixtureName is "ModelPartFixture" then the result is:
237 // class ModelPartFixture : public TestCase
238 // {
239 // public:
240 // ModelPartFixture(std::string const& Name) : TestCase(Name) {}
241 // private:
242 // void Setup() override;
243 // void TearDown() override;
244 // };
245 //
246 #define KRATOS_TEST_FIXTURE(TestFixtureName) \
247 class TestFixtureName : public TestCase \
248  {\
249  public:\
250  TestFixtureName(std::string const& Name) : TestCase(Name) {}\
251  private: \
252  void Setup() override; \
253  void TearDown() override; \
254 };
255 
256 #define KRATOS_TEST_FIXTURE_SETUP(TestFixtureName) \
257 void TestFixtureName::Setup()
258 
259 #define KRATOS_TEST_FIXTURE_TEAR_DOWN(TestFixtureName) \
260 void TestFixtureName::TearDown()
261 
262 #define KRATOS_TEST_CASE_WITH_FIXTURE(TestCaseName,TestFixtureName) \
263 KRATOS_TESTING_TEST_CASE_CLASS(TestCaseName, TestFixtureName) \
264 const Kratos::Testing::Internals::RegisterThisTest< KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName) > \
265  KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName)::mDummy; \
266 \
267 void KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName)::TestFunction()
268 
269 #define KRATOS_DISABLED_TEST_CASE_WITH_FIXTURE(TestCaseName,TestFixtureName) \
270 KRATOS_TESTING_TEST_CASE_CLASS(TestCaseName, TestFixtureName) \
271 const Kratos::Testing::Internals::RegisterThisTest< KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName) > \
272  KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName)::mDummy(true); \
273 \
274 void KRATOS_TESTING_CREATE_CLASS_NAME(TestCaseName)::TestFunction()
275 
277 
279 } // namespace Kratos::Testing.
std::string Info() const override
Turn back information as a string.
Definition: periodic_interface_process.hpp:93
RegisterThisTest(bool IsDisabled=false)
Definition: test_case.h:32
The test case base class.
Definition: test_case.h:53
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: test_case.cpp:178
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: test_case.cpp:183
TestCase()=delete
TestCase cannot be created without a name.
TestCase(TestCase const &rOther)=delete
The TestCase cannot be copied to avoid duplications.
TestCase & operator=(TestCase const &rOther)=delete
Preventing the assignment of the tests.
The test case base class.
Definition: test_case_result.h:37
static void AddTestCase(TestCase *pHeapAllocatedTestCase)
Definition: tester.cpp:134
std::ostream & operator<<(std::ostream &rOStream, const TestCase &rThis)
output stream function
Definition: test_case.h:174
Definition: distributed_test_case.cpp:24
def Run()
Definition: convection_diffusion_benchmarks.py:9