Salome HOME
6bd5e05ab99a9b924d2169553ab00b95b2fe7c35
[modules/yacs.git] / src / bases / Test / BasicMainTest.hxx
1 //  Copyright (C) 2006-2008  CEA/DEN, 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.
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 #ifndef _BASICMAINTEST_HXX_
20 #define _BASICMAINTEST_HXX_
21
22 #include <cppunit/CompilerOutputter.h>
23 #include <cppunit/TestResult.h>
24 #include <cppunit/TestResultCollector.h>
25 #include <cppunit/TextTestProgressListener.h>
26 #include <cppunit/BriefTestProgressListener.h>
27 #include <cppunit/extensions/TestFactoryRegistry.h>
28 #include <cppunit/TestRunner.h>
29 #include <cppunit/TextTestRunner.h>
30 #include <stdexcept>
31
32 #include <iostream>
33 #include <fstream>
34 #include <stdlib.h>
35
36 // ============================================================================
37 /*!
38  *  Main program source for Unit Tests with cppunit package does not depend
39  *  on actual tests, so we use the same for all partial unit tests.
40  */
41 // ============================================================================
42
43 #include "UnitTestsResult.hxx"
44
45 int main(int argc, char* argv[])
46 {
47   // --- Create the event manager and test controller
48   CPPUNIT_NS::TestResult controller;
49
50   // ---  Add a listener that collects test result
51   CPPUNIT_NS::TestResultCollector result;
52   controller.addListener( &result );        
53
54   // ---  Add a listener that print dots as test run.
55 #ifdef WIN32
56   CPPUNIT_NS::TextTestProgressListener progress;
57 #else
58   CPPUNIT_NS::BriefTestProgressListener progress;
59 #endif
60   controller.addListener( &progress );      
61
62   // ---  Get the top level suite from the registry
63
64   CPPUNIT_NS::Test *suite =
65     CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
66
67   // ---  Adds the test to the list of test to run
68
69   CPPUNIT_NS::TestRunner runner;
70   //CPPUNIT_NS::TextTestRunner runner;
71   runner.addTest( suite );
72   runner.run( controller);
73   //bool wasSucessful = runner.run();
74
75   // ---  Print test in a compiler compatible format.
76
77   system("mkdir -p /tmp/${USER}");
78   std::ofstream testFile;
79   testFile.open(UnitTestsResult.c_str(), std::ios::out | std::ios::app);
80   testFile << UNIT_TEST_HEADER << endl;
81   //CPPUNIT_NS::CompilerOutputter outputter( &result, std::cerr );
82   CPPUNIT_NS::CompilerOutputter outputter( &result, testFile );
83   outputter.write(); 
84
85   // ---  Run the tests.
86
87   bool wasSucessful = result.wasSuccessful();
88   testFile.close();
89
90   // ---  Return error code 1 if the one of test failed.
91
92   return wasSucessful ? 0 : 1;
93 }
94
95 #endif