Writing Automated test cases for a folder Archiving Class.

Using C# and Visual Studio, I’ve developed a class that zips and archives a folder into desired location, creating directories and manipulating files on a machine.

The intended use is as an part of an embedded piece of software that will collect logging information for another piece of software after its completion.

This class is setup from parent classes that will pass it known configuration information, this class does not care where it came from it just needs to be there. The class requires directory and file information passed to it which is derived from the config files.

I know what needs to be tested but what I am struggling with is how I should write automated tests if that is even possible. The reason this is such a struggle is that the configuration information is most certainly going to be different for each user.

Are automated tests even possible without developer having to modify them every time we need to run tests? How would you approach this or am I overthinking this?

If you encapsulate all IO operations in a small class with an interface and inject it to the archiving class you can mock calls to the IO system during testing.

public class Archiver
{
    public IFileSystem Filesystem { private get; set; }

    public void DoWork() 
    {
        //business logic here
    }

    public Archiver() { Filesystem = new RealFilesystemClass(); }
}


[TestClass]
class ArchiveUnitTests
{
    Archiver archiver = new Archiver { Filesystem = new MockFilesystem() };

    [Test]
    public void Reads_From_Filesystem()
    {
        //Test code here
    }
}

2

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *