How to do unit testing of file input methods?

I use Parasoft's C ++ Test for unit testing C ++ code. I ran into the following problem. I have a function similar to the following (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 test files for unit test testing?

In my case, std :: fstream is used to enter the file .

Are there any better deals? (Best if done in C ++ Test, but not required).

+3
source share
6 answers

.

unit test, .

read_entire_file_to_buffer() , , , , , - 40- , .

+3

. . , , , , , .

+6

:

, .

unit test .

, .

+2

, . , ( , malloc return 0 ..). . , wikipedia .

+1

, . std::istream, ifstream (, ). , istringstream ifstream, .

+1

, , , , ( ). , , , , , .. 3 :

if (openfile (rStrFileName) == successl)

false. , . . .

Remember that the thing is to test the function as close to reality as possible. This ensures that many strange cases that you cannot expect will be automatically caught so that you can fix this, which is the beginning of unit testing.

0
source

Source: https://habr.com/ru/post/1712324/


All Articles