我决定让我的功能测试变得通用,这样我就可以将类的不同实现传递给它,但保持测试相同。
这里是常用的测试类:
internal abstract class PictureHandlingTestsBase : IlgTestBase
{
protected PictureHandlingTestsBase(GallerySettingsBase defaultGalSettings, GallerySettingsBase altGalSettings)
{
DefaultGallerySettings = defaultGalSettings;
AltGallerySettings = altGalSettings;
// ...
}
[TestCaseSource(nameof(AllGallerySettings))]
public async Task ChangeFriendlyUrlForGalleryWithPics(IlgUniGallerySettingsBase gallerySettings)
{
// ...
}
// ...
protected static object[]? AllGallerySettings { get; private set; }
protected static GallerySettingsBase? AltGallerySettings { get; private set; }
protected static GallerySettingsBase? DefaultGallerySettings { get; private set; }
}
然后我对文件系统进行了这些测试的实现:
internal class FilesystemPictureHandlingTests : PictureHandlingTestsBase
{
public FilesystemPictureHandlingTests() : base(
new FilesystemGallerySettings(...), new FilesystemGallerySettings(...)) { }
[SetUp]
public void SetUp()
{
// ...
}
}
对于 Azure Blob 存储:
internal class AzurePictureHandlingTests : PictureHandlingTestsBase
{
public AzurePictureHandlingTests() : base(
new AzureGallerySettings(...), new AzureGallerySettings(...)) { }
[SetUp]
public void SetUp()
{
// ...
}
}
但是当我运行测试时我得到:
System.Exception:找不到测试用例源。
这实际上是什么意思?
由于我没有更好的解决方案,这就是我现在所做的:
internal abstract class PictureHandlingTestsBase : IlgTestBase
{
protected PictureHandlingTestsBase(GallerySettingsBase defaultGalSettings, GallerySettingsBase altGalSettings)
{
defaultGalSettings.Should().NotBeNull();
altGalSettings.Should().NotBeNull();
DefaultGallerySettings = defaultGalSettings;
AltGallerySettings = altGalSettings;
AllGallerySettings = new object[] { DefaultGallerySettings, AltGallerySettings };
}
[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();
}
首先,我必须创建自己的抽象
SetUp()
方法,因为在从默认设置更改为替代设置之前我必须手动运行它。然后这是我的派生类:
internal class FilesystemPictureHandlingTests : PictureHandlingTestsBase
{
public FilesystemPictureHandlingTests() : base(new FilesystemGallerySettings(...), new FilesystemGallerySettings(...)) { }
[SetUp]
public override void SetUp()
{
// ...
}
}