我实现了一个类似于 xUnit 文档中示例的 DatabaseFixture,如下所示:
public class DatabaseFixture : IDisposable
{
public DatabaseFixture()
{
Db = new SqlConnection("MyConnectionString");
// ... initialize data in the test database ...
}
public void Dispose()
{
// ... clean up test data from the database ...
}
public SqlConnection Db { get; private set; }
}
public class MyDatabaseTests : IClassFixture<DatabaseFixture>
{
DatabaseFixture fixture;
public MyDatabaseTests(DatabaseFixture fixture)
{
this.fixture = fixture;
}
// ... write tests, using fixture.Db to get access to the SQL Server ...
}
但是我想跨多个环境运行测试,其中数据库连接字符串可能会发生变化。我知道 xUnit 不支持夹具定义中的参数化向量,但对字符串进行硬编码感觉很糟糕。鉴于固定装置在测试生命周期中很早就初始化,我如何根据测试运行的位置更改此字符串?
我认为[理论]-属性可能是您的解决方案