0

I created some tests for my SetDesktopWallpaper() static method with xUnit.
As its name implies, it changes the current users's desktop wallpaper.

Here is one of my tests.

[Theory]
[InlineData(@".\Resources\JpgWallpaper.jpg")]
[InlineData(@".\Resources\PngWallpaper.png")]
public void Changing_Wallpaper_File_Should_Work(string value)
{
    const string keyName = @"HKEY_CURRENT_USER\Control Panel\Desktop";
    const string keyVal1 = "WallPaper";

    WallChanger.SetDesktopWallpaper(value);

    Assert.Equal(Path.GetFullPath(value), Registry.GetValue(keyName, keyVal1, null).ToString());
}

I would like it to save the current wallpaper at the beginning of the tests and put it back at the end. How to achieve this with xUnit?

0lan
  • 179
  • 1
  • 14

1 Answers1

1

According to the context provided, I would implement IDisposable interface in your test class, save the info you want to restore in the constructor and, following dispose pattern implementation; having it restored at the end of every test case.

You also have this alternative using collection fixtures, but that's for more general setup/teardown, so I don't think it's the way to go in your case.

Both alternatives are described in more detail here.

eduherminio
  • 1,514
  • 1
  • 15
  • 31