Menu

Dynamic Test approach?

Help
2007-08-13
2013-04-22
  • Matthew R. Pavlovich

    I have a project that has hundreds of XML files that contain data to be processed by an application.  I would like to create a cppunit framework around them.  My initial approach was to create a test that executed all the 'success' data and another one that executed all the 'failure' data (testing for proper handling of error conditions).  The problem with this approach is that the test would error out on the first file that had a problem.  I would like a solution where I could process each file as a CPPUNIT Test/Feature independently, so when one data file has an error, the rest would continue to process.  Ideally, this would be done dynamically, so you could add/remove test data files without making changes to the code.

    Any suggestions on a solution?

    Thanks in advance.

     
    • Code Guru

      Code Guru - 2007-10-05

      I apologize if this response is a little delayed.  I would suggest doing something similar to the following:

      // XMLTest.h
      class XMLTest : TestCase {
        public:
          XMLTest(std::string filename);
          // various testXxx() methods for a single file

        private:
          std::string filename;
      };

      // XMLTest.cpp
      // contains implementations of XMLTest::testXxx() methods

      -------------------------------------

      // XMLSuite.h
      class XMLSuite : TestSuite {
        public:
          XMLSuite(std::string directory);
          static TestCase suite();

        private:
          std::string directory;
      }

      // XMLSuite.cpp
      static TestCase XMLSuite::suite() {
        TestCase suite;

        // for each filename in this->directory
        //   suite.add(XMLTest(filename));

        return suite;
      }

      ---------------------------------------------

      I have left out a few of the details.  The contstructors should be trivial to implement.  The comments in XMLSuite::suite() give pseudocode for how to dynamically create your test cases.  The exact implementation is dependent on the development environment and operating system.

      Note that you won't have to recompile this test to test a new file.  Just place it in the directory specified in the XMLSuite constructor.  You can also take this up another level and create a suite for specific directories.  This would allow you to organize the XML files if there are a lot of them.  You could also create a text file with a list of directories to read in to create several XMLSuite objects.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.