事情是,最粗糙的Nunit级别是一个集会,即,它是用一个想法编写的,即要在一个集会中共享资源,而不是在几个集会中共享。
现在,这取决于您要在框架上工作的程度😉当然,您可以编写一些共享代码,这些代码通过不同的测试组件来调用并同步它们,但老实说,这不会很有趣,而您面临的一些令人讨厌的并行性/同步问题的变化很高。namespace TestsAssemblyA
{
[SetUpFixture]
public class MySetUpClassForA
{
[OneTimeSetUp]
public void RunBeforeAnyTests() => Tests.Shared.CommonThings.ResetDatabase();
}
}
namespace TestsAssemblyB
{
[SetUpFixture]
public class MySetUpClassForB
{
[OneTimeSetUp]
public void RunBeforeAnyTests() => Tests.Shared.CommonThings.ResetDatabase();
}
}
namespace Tests.Shared
{
public static class CommonThings(ILogger logger)
{
public static void ResetDatabase()
{
if (IsFirstCallOfSession())
{
InnerResetDatabase("connectionStringOfSharedDatabase");
}
else
{
logger.LogInformation("Nothing to do");
}
}
private bool IsFirstCallOfSession() => ... // good luck!
}
}
namespace TestsAssemblyA
{
[SetUpFixture]
public class MySetUpClassForA
{
[OneTimeSetUp]
public void RunBeforeAnyTests() => Tests.Shared.CommonThings.ResetDatabase(connectionStringForDatabaseA);
}
}
namespace TestsAssemblyB
{
[SetUpFixture]
public class MySetUpClassForB
{
[OneTimeSetUp]
public void RunBeforeAnyTests() => Tests.Shared.CommonThings.ResetDatabase(connectionStringForDatabaseB);
}
}
namespace Tests.Shared
{
public static class CommonThings(ILogger logger)
{
public static void ResetDatabase(string connectionString) => InnerResetDatabase(connectionString);
}
}