Salome HOME
[EDF29150] : head monitoring file management
[modules/kernel.git] / src / Basics / Test / BasicMainTestInternal.hxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #pragma once
24
25 #include <cppunit/CompilerOutputter.h>
26 #include <cppunit/TestResult.h>
27 #include <cppunit/TestResultCollector.h>
28 #include <cppunit/TextTestProgressListener.h>
29 #include <cppunit/BriefTestProgressListener.h>
30 #include <cppunit/extensions/TestFactoryRegistry.h>
31 #include <cppunit/TestRunner.h>
32 #include <stdexcept>
33
34 #include <iostream>
35 #include <fstream>
36
37 // ============================================================================
38 /*!
39  *  Main program source for Unit Tests with cppunit package does not depend
40  *  on actual tests, so we use the same for all partial unit tests.
41  */
42 // ============================================================================
43
44 static int BasicMainTestInternal()
45 {
46   // --- Create the event manager and test controller
47   CPPUNIT_NS::TestResult controller;
48
49   // ---  Add a listener that colllects test result
50   CPPUNIT_NS::TestResultCollector result;
51   controller.addListener( &result );        
52
53   // ---  Add a listener that print dots as test run.
54 #ifdef WIN32
55   CPPUNIT_NS::TextTestProgressListener progress;
56 #else
57   CPPUNIT_NS::BriefTestProgressListener progress;
58 #endif
59   controller.addListener( &progress );      
60
61   // ---  Get the top level suite from the registry
62
63   CPPUNIT_NS::Test *suite =
64     CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
65
66   // ---  Adds the test to the list of test to run
67
68   CPPUNIT_NS::TestRunner runner;
69   runner.addTest( suite );
70   runner.run( controller);
71
72   // ---  Print test in a compiler compatible format.
73
74   std::ofstream testFile;
75   testFile.open("UnitTestsResult", std::ios::out |  std::ios::trunc);
76   //CPPUNIT_NS::CompilerOutputter outputter( &result, std::cerr );
77   CPPUNIT_NS::CompilerOutputter outputter( &result, testFile );
78   outputter.write(); 
79
80   // ---  Run the tests.
81
82   bool wasSucessful = result.wasSuccessful();
83   testFile.close();
84
85   // ---  Return error code 1 if the one of test failed.
86
87   return wasSucessful ? 0 : 1;
88 }