Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
UnitTestModule.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* UnitTestModule.cc (C) 2000-2024 */
9/* */
10/* Module for unit tests. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/ScopedPtr.h"
15
16#include "arcane/core/IUnitTest.h"
17#include "arcane/core/ISubDomain.h"
18#include "arcane/core/IApplication.h"
19#include "arcane/core/ITimeLoop.h"
20#include "arcane/core/ITimeLoopMng.h"
21#include "arcane/core/IXmlDocumentHolder.h"
22#include "arcane/core/IIOMng.h"
23#include "arcane/core/ArcaneException.h"
24#include "arcane/core/TimeLoopEntryPointInfo.h"
25#include "arcane/core/Directory.h"
26#include "arcane/core/XmlNode.h"
27#include "arcane/core/IParallelMng.h"
28#include "arcane/core/DomUtils.h"
29
30#include "arcane/std/UnitTest_axl.h"
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
35namespace Arcane
36{
37
38/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
40
44class UnitTestModule
45: public ArcaneUnitTestObject
46{
47 public:
48
49 explicit UnitTestModule(const ModuleBuildInfo& cb);
50
51 public:
52
53 static void staticInitialize(ISubDomain* sd);
54 VersionInfo versionInfo() const override { return VersionInfo(2, 0, 0); }
55
56 public:
57
58 void unitTestBuild() override;
59 void unitTestInit() override;
60 void unitTestDoTest() override;
61 void unitTestExit() override;
62
63 private:
64
66 bool m_success = true;
67
68 private:
69
70 void _checkCreateXmlTestDocument();
71};
72
73/*---------------------------------------------------------------------------*/
74/*---------------------------------------------------------------------------*/
75
76ARCANE_REGISTER_MODULE_UNITTEST(UnitTestModule);
77
78/*---------------------------------------------------------------------------*/
79/*---------------------------------------------------------------------------*/
80
81UnitTestModule::
82UnitTestModule(const ModuleBuildInfo& mb)
84, m_tests_doc(domutils::createXmlDocument())
85{
86 XmlNode doc = m_tests_doc->documentNode();
87 XmlElement root(doc, "unit-tests-results");
88}
89
90/*---------------------------------------------------------------------------*/
91/*---------------------------------------------------------------------------*/
92
93void UnitTestModule::
94staticInitialize(ISubDomain* sd)
95{
96 String time_loop_name("UnitTest");
97 ITimeLoopMng* tlm = sd->timeLoopMng();
98 ITimeLoop* time_loop = tlm->createTimeLoop(time_loop_name);
99
100 {
101 List<TimeLoopEntryPointInfo> clist;
102 clist.add(TimeLoopEntryPointInfo("UnitTest.UnitTestBuild"));
103 time_loop->setEntryPoints(ITimeLoop::WBuild, clist);
104 }
105
106 {
107 List<TimeLoopEntryPointInfo> clist;
108 clist.add(TimeLoopEntryPointInfo("UnitTest.UnitTestInit"));
109 time_loop->setEntryPoints(ITimeLoop::WInit, clist);
110 }
111
112 {
113 List<TimeLoopEntryPointInfo> clist;
114 clist.add(TimeLoopEntryPointInfo("UnitTest.UnitTestDoTest"));
115 time_loop->setEntryPoints(ITimeLoop::WComputeLoop, clist);
116 }
117
118 {
119 List<TimeLoopEntryPointInfo> clist;
120 clist.add(TimeLoopEntryPointInfo("UnitTest.UnitTestExit"));
121 time_loop->setEntryPoints(ITimeLoop::WExit, clist);
122 }
123
124 {
125 StringList clist;
126 clist.add("UnitTest");
127 clist.add("ArcanePostProcessing");
128 time_loop->setRequiredModulesName(clist);
129 }
130
131 tlm->registerTimeLoop(time_loop);
132}
133
134/*---------------------------------------------------------------------------*/
135/*---------------------------------------------------------------------------*/
136
139{
140 for (IUnitTest* service : options()->test)
141 service->buildInitializeTest();
142
143 for (IXmlUnitTest* service : options()->xmlTest)
144 service->buildInitializeTest();
145}
146
147/*---------------------------------------------------------------------------*/
148/*---------------------------------------------------------------------------*/
149
150void UnitTestModule::
151unitTestInit()
152{
153 // Initialize in case no test does it
154 m_global_deltat = 1.0;
155
156 for (IUnitTest* service : options()->test)
157 service->initializeTest();
158
159 for (IXmlUnitTest* service : options()->xmlTest)
160 service->initializeTest();
161}
162
163/*---------------------------------------------------------------------------*/
164/*---------------------------------------------------------------------------*/
165
166void UnitTestModule::
167unitTestDoTest()
168{
170
171 for (IUnitTest* service : options()->test)
172 service->executeTest();
173
174 if (options()->xmlTest.size() > 0) {
175 XmlNode xtests = m_tests_doc->documentNode().documentElement();
176 for (IXmlUnitTest* service : options()->xmlTest) {
177 XmlNode xservice = xtests.createAndAppendElement("service");
178 if (!service->executeTest(xservice))
179 m_success = false;
180 }
181 }
182}
183
184/*---------------------------------------------------------------------------*/
185/*---------------------------------------------------------------------------*/
186
187void UnitTestModule::
188unitTestExit()
189{
190 for (IUnitTest* service : options()->test)
191 service->finalizeTest();
192
193 for (IXmlUnitTest* service : options()->xmlTest)
194 service->finalizeTest();
195
196 if (options()->xmlTest.size() > 0) {
197 // writing the XML report.
198 // In parallel, only the master processor writes the file
199 IParallelMng* pm = subDomain()->parallelMng();
200 if (pm->isMasterIO()) {
201 Directory listing_dir(subDomain()->listingDirectory());
202 String filename(listing_dir.file("unittests.xml"));
203 info() << "Output of the report of the unit test in '" << filename << "'";
204 subDomain()->ioMng()->writeXmlFile(m_tests_doc.get(), filename);
205 }
206
207 // exit with exception if requested
208 if (!m_success)
209 ARCANE_FATAL("Some errors have occured in the unit tests.");
210 }
211}
212
213/*---------------------------------------------------------------------------*/
214/*---------------------------------------------------------------------------*/
215
216} // End namespace Arcane
217
218/*---------------------------------------------------------------------------*/
219/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
ISubDomain * subDomain() const override
Sub-domain associated with the module.
Generation de la classe de base du Module.
CaseOptionsUnitTest * options() const
Options du jeu de données du module.
VariableScalarReal m_global_deltat
Global Delta T.
virtual bool writeXmlFile(IXmlDocumentHolder *doc, const String &filename, const bool indented=false)=0
Writes the XML tree of the document doc to the file filename.
Interface of the subdomain manager.
Definition ISubDomain.h:75
virtual IIOMng * ioMng()=0
Returns the I/O manager.
virtual IParallelMng * parallelMng()=0
Returns the parallelism manager.
virtual ITimeLoopMng * timeLoopMng()=0
Returns the time loop manager.
virtual void stopComputeLoop(bool is_final_time, bool has_error=false)=0
Indicates that the compute loop must stop.
static const char * WExit
called upon termination of the code.
Definition ITimeLoop.h:53
static const char * WBuild
called when reading the dataset
Definition ITimeLoop.h:43
static const char * WComputeLoop
called during the calculation loop
Definition ITimeLoop.h:41
static const char * WInit
called during initialization, initialization of a restart, or a new case
Definition ITimeLoop.h:45
Interface of a unit test service.
Definition IUnitTest.h:35
Interface of a unit test service providing a test report in the form of an XML node.
Definition IUnitTest.h:64
Information for building a module.
Encapsulation of an automatically destructing pointer.
Definition ScopedPtr.h:44
TraceMessage info() const
Flow for an information message.
ScopedPtrT< IXmlDocumentHolder > m_tests_doc
Unit test traces.
VersionInfo versionInfo() const override
Module version.
bool m_success
True as long as a unit test has not returned an error.
void unitTestBuild() override
points d'entrée
Information about a version.
Definition VersionInfo.h:47
Element of a DOM tree.
Definition XmlNode.h:406
Node of a DOM tree.
Definition XmlNode.h:51
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
List< String > StringList
Unicode string list.
Definition UtilsTypes.h:509