Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How-to do unit-testing of methods involving file input output?

I'm using C++Test from Parasoft for unit testing C++ code. I came across the following problem. I have a function similar to the next one (pseudocode):

bool LoadFileToMem(const std::string& rStrFileName)
{
    if( openfile(rStrFileName) == successfull )
    {
         if( get_file_size() == successfull )
         {
            if( read_entire_file_to_buffer() == successfull )
            {
                return true;
            }
            return false;
         }
         return false;
    }
    return false;
}

My questions in this case are:

Should I use stubs for file system functions? Or should I include specific sample test files for running the unit tests?

In my case std::fstream class is used for file input.

Has anyone better suggestions? (Best if done in C++Test but not mandatory).

like image 579
INS Avatar asked Dec 06 '22 05:12

INS


1 Answers

I would go for short sample test files. They can be checked into source control along with the test code. The reason I would do it is that the intent of your function is to load a file, so this is what you should be testing.

like image 56
quant_dev Avatar answered Mar 23 '23 12:03

quant_dev