1 // Copyright (C) 2013-2024 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Frederic Pons (OpenCascade)
21 #ifndef __XAOMAINTEST_HXX__
22 #define __XAOMAINTEST_HXX__
24 #include <cppunit/CompilerOutputter.h>
25 #include <cppunit/TestResult.h>
26 #include <cppunit/TestResultCollector.h>
27 #include <cppunit/TextTestProgressListener.h>
28 #include <cppunit/BriefTestProgressListener.h>
29 #include <cppunit/extensions/TestFactoryRegistry.h>
30 #include <cppunit/TestRunner.h>
45 Arguments parseArguments(int argc, char* argv[])
55 while (++i < argc) // skip 0
57 if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--list") == 0)
61 else if (strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--err") == 0)
65 else if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--test") == 0)
76 void listTests(CPPUNIT_NS::Test* test)
79 std::cout << test->getName() << std::endl;
80 for (int i = 0; i < test->getChildTestCount(); ++i)
82 listTests(test->getChildTestAt(i));
86 // ============================================================================
88 * Main program source for Unit Tests with cppunit package does not depend
89 * on actual tests, so we use the same for all partial unit tests.
91 // ============================================================================
92 int main(int argc, char* argv[])
94 Arguments args = parseArguments(argc, argv);
96 // --- Create the event manager and test controller
97 CPPUNIT_NS::TestResult controller;
99 // --- Add a listener that colllects test result
100 CPPUNIT_NS::TestResultCollector result;
101 controller.addListener(&result);
103 // --- Add a listener that print dots as test run.
105 CPPUNIT_NS::TextTestProgressListener progress;
107 CPPUNIT_NS::BriefTestProgressListener progress;
109 controller.addListener(&progress);
111 // --- Get the top level suite from the registry
113 CPPUNIT_NS::Test *suite =
114 CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
116 // --- Adds the test to the list of test to run
118 // list tests and exit
125 if (!args.Test.empty())
128 // an exception is raised if not found
129 suite = suite->findTest(args.Test);
132 CPPUNIT_NS::TestRunner runner;
133 runner.addTest(suite);
134 runner.run(controller);
136 // --- Print test in a compiler compatible format.
140 CPPUNIT_NS::CompilerOutputter outputter(&result, std::cerr);
145 std::ofstream testFile;
146 testFile.open("UnitTestsResult", std::ios::out | std::ios::trunc);
147 CPPUNIT_NS::CompilerOutputter outputter(&result, testFile);
152 // --- Run the tests.
154 bool wasSucessful = result.wasSuccessful();
156 // --- Return error code 1 if the one of test failed.
158 return wasSucessful ? 0 : 1;