testdriver-before-test.inc
Go to the documentation of this file.
1 // ===========================================================================
2 // Copyright (c) Insight Software Consortium
3 // Copyright (c) 2011-2012 University of Pennsylvania
4 // Copyright (c) 2013-2016 Andreas Schuh
5 // All rights reserved.
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0.txt
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ===========================================================================
19 
20 /**
21  * @file testdriver-before-test.inc
22  * @brief Default implementation of test driver.
23  *
24  * This file is included in the test driver generated by the CMake command
25  * <a href="http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:create_test_sourcelist">
26  * create_test_sourcelist()</a> directly before the call to the test main function.
27  *
28  * This file is a modified version of the itkTestDriverBeforeTest.inc file
29  * which is part of the TestKernel module of the ITK 4 project.
30  */
31 
32 //int main(int, char*)
33 //{
34  // [...]
35  try {
36  // redirect standard output to file
37  ofstream redirectstream;
38  streambuf* redirectbuf = NULL;
39  streambuf* oldcoutbuf = NULL;
40  if (redirect_output.isSet()) {
41  cout << "Test output has been redirected to: " << redirect_output.getValue() << endl;
42  redirectstream.open(redirect_output.getValue().c_str(), ios::out);
43  redirectbuf = redirectstream.rdbuf();
44  oldcoutbuf = cout.rdbuf();
45  cout.rdbuf(redirectbuf);
46  }
47  // empty current working directory
48  if (clean_cwd_before_test.getValue()) {
49  os::emptydir(os::getcwd().c_str());
50  }
51  // remove all test output images if existent from previous test run
52  if (testcmd.isSet()) {
53  for (size_t i = 0; i < regression_tests.size(); i++) {
54  const char* test_file = regression_tests[i].test_file.c_str();
55  if (os::path::isfile(test_file)) {
56  if (remove(test_file) == 0) {
57  if (verbose.getValue() > 0) {
58  cout << "Removed already existing test output file " << test_file << endl;
59  }
60  } else {
61  cerr << "Warning: Failed to remove already existing output image " << test_file << endl;
62  }
63  }
64  }
65  }
66  // execute test in a try block
67  // [...]
68  // #include <sbia/basis/testdriver-after-test.inc>
69 //}
PositionalArgs testcmd("testcmd", "The name of the test to run and optional arguments." " Displays a list of available tests if this argument is omitted" " and waits for the user to input the number of the test to run." " Exist with error if an invalid test was specified." " Note that if the -- option is not given before the test name," " labeled arguments following the test name will be considered" " to be options of the test driver if known by the test driver." " Otherwise, if the option is unknown to the test driver or the" " -- option has been given before the test name, the remaining" " arguments are passed on to the test.", false, "", "[--] [<test name> [<arg>...]]")
std::string getcwd()
Get absolute path of the (current) working directory.
Definition: os.cxx:47
streambuf * redirectbuf
bool emptydir(const std::string &path)
Remove files and directories from directory.
Definition: os.cxx:238
bool isfile(const std::string path)
Test whether a given path is the path of an existent file.
Definition: path.cxx:448
SwitchArg clean_cwd_before_test("", "clean-cwd-before", "Request the removal of all files and directories from the current" " working directory before the execution of the test. This option is" " in particular useful if the test writes any results to the current" " working directory.", false)
streambuf * oldcoutbuf
StringArg redirect_output("", "redirect-output", "Redirects the test output to the specified file.", false, "", "<file>")
MultiSwitchArg verbose("v", "verbose", "Increase verbosity of output messages.", false)
vector< RegressionTest > regression_tests
Container storing added regression tests.
Definition: testdriver.h:132