Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test file system operations

I have a method which synchronizes two folders, that looks like this:

void Synchronize(string folderAPath, string folderBPath)
{
    //Code to synchronize the folders
}

What would be the best way to test if the files were synchronized properly, or in general, test methods that manipulate the file system? Is there a way to set up virtual folders?

like image 556
Flagbug Avatar asked Feb 20 '11 21:02

Flagbug


People also ask

What is file testing?

The file testing checklist is a very powerful fact-gathering tool used to verify that all needed files are included in the system being tested.

What to unit test?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.

Should unit tests read files?

Strictly speaking, unit tests should not use the file system because it is slow. However, readability is more important.

Why unit test?

Unit testing ensures that all code meets quality standards before it's deployed. This ensures a reliable engineering environment where quality is paramount. Over the course of the product development life cycle, unit testing saves time and money, and helps developers write better code, more efficiently.


1 Answers

As @BrokenGlass already advised, hiding the actual API behind a mockable interface allows you to unit test your logic. I would do this only though if there is substantial logic in there to justify the extra complexity needed to make the interface unit testable. Testing that the code really works under real circumstances, on the real filesystem needs integration tests anyway, so in simple cases the unit tests could be skipped, to use one's limited resources more effectively.

like image 84
Péter Török Avatar answered Sep 28 '22 18:09

Péter Török