Salome HOME
Merge branch 'omu/multijob'
[tools/ydefx.git] / src / cpp / Test / PyTestMain.cxx
1 // Copyright (C) 2019  EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include <cppunit/CompilerOutputter.h>
20 #include <cppunit/TestResult.h>
21 #include <cppunit/TestResultCollector.h>
22 #include <cppunit/TextTestProgressListener.h>
23 #include <cppunit/BriefTestProgressListener.h>
24 #include <cppunit/extensions/TestFactoryRegistry.h>
25 #include <cppunit/TestRunner.h>
26 #include <cppunit/TextTestRunner.h>
27 #include <stdexcept>
28
29 #include <iostream>
30 #include <fstream>
31 #include <stdlib.h>
32 #include <Python.h>
33
34 // ============================================================================
35 /*!
36  *  Main program source for Unit Tests with cppunit package does not depend
37  *  on actual tests, so we use the same for all partial unit tests.
38  *  This version of TestMain initializes the python library and it can be used
39  *  if you have several tests which need Py_Initialize and salome_init.
40  */
41 // ============================================================================
42
43 int main(int argc, char* argv[])
44 {
45   Py_Initialize();
46   // --- Create the event manager and test controller
47   CPPUNIT_NS::TestResult controller;
48
49   // ---  Add a listener that collects 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   std::ofstream testFile;
74   testFile.open("test.log", std::ios::out | std::ios::app);
75   testFile << "------ Idefix test log:" << std::endl;
76   CPPUNIT_NS::CompilerOutputter outputter( &result, testFile );
77   outputter.write();
78
79   // ---  Run the tests.
80
81   bool wasSucessful = result.wasSuccessful();
82   testFile.close();
83   Py_Finalize();
84
85   // ---  Return error code 1 if the one of test failed.
86
87   return wasSucessful ? 0 : 1;
88 }