Search code examples
c#testingnunit

NUnit - trying to create a common test but got The test case source could not be found


I've decided to make my tests for a feature common, so I could pass on to it different implementations of classes but keep tests equal.

Here's the common test class:

internal abstract class PictureHandlingTestsBase : IlgTestBase
{
    protected PictureHandlingTestsBase(GallerySettingsBase defaultGalSettings, GallerySettingsBase altGalSettings)
    {
        DefaultGallerySettings = defaultGalSettings;
        AltGallerySettings = altGalSettings;

        // ...
    }

    [TestCaseSource(nameof(AllGallerySettings))]
    public async Task ChangeFriendlyUrlForGalleryWithPics(GallerySettingsBase gallerySettings)
    {
      // ...
    }

    // ...

    protected static object[]? AllGallerySettings { get; private set; }

    protected static GallerySettingsBase? AltGallerySettings { get; private set; }

    protected static GallerySettingsBase? DefaultGallerySettings { get; private set; }

}

And then I have an implementation of these tests for file system:

internal class FilesystemPictureHandlingTests : PictureHandlingTestsBase
{
    public FilesystemPictureHandlingTests() : base(
        new FilesystemGallerySettings(...), new FilesystemGallerySettings(...)) { }

    [SetUp]
    public void SetUp()
    {
      // ...
    }
}

And for Azure blob storage:

internal class AzurePictureHandlingTests : PictureHandlingTestsBase
{
    public AzurePictureHandlingTests() : base(
        new AzureGallerySettings(...), new AzureGallerySettings(...)) { }

    [SetUp]
    public void SetUp()
    {
      // ...
    }
}

But when I run tests I get:

System.Exception : The test case source could not be found.

What does it actually mean?


Solution

  • As I don't have a better solution, here's what I do for now:

    internal abstract class PictureHandlingTestsBase : IlgTestBase
    {
        protected PictureHandlingTestsBase(GallerySettingsBase defaultGalSettings, GallerySettingsBase altGalSettings)
        {
            defaultGalSettings.Should().NotBeNull();
            altGalSettings.Should().NotBeNull();
    
            DefaultGallerySettings = defaultGalSettings;
            AltGallerySettings = altGalSettings;
        }
    
        [Test]
        public async Task ChangeFriendlyUrlForGalleryWithPics()
        {
            await ActualTest(DefaultGallerySettings!);
    
            SetUp(); // running it manually before simulating another run
    
            await ActualTest(AltGallerySettings!);
            return;
    
            async Task ActualTest(GallerySettingsBase gallerySettings)
            {
              // ... the test logic
            }
        }
    
        // This must exist here as I have to run it manually
        public abstract void SetUp();
    }
    

    First, I had to create my own abstract SetUp() method because I have to run it manually before changing from default settings to alternative settings. And then here's my derived class:

    internal class FilesystemPictureHandlingTests : PictureHandlingTestsBase
    {
        public FilesystemPictureHandlingTests() : base(new FilesystemGallerySettings(...), new FilesystemGallerySettings(...)) { }
    
        [SetUp]
        public override void SetUp()
        {
          // ...
        }
    }