我正在考虑使用 NDbUnit 来帮助对应用程序进行单元测试。正如问题标题所述,我想知道是否可以将 NDbUnit 测试数据保存在单独的 XML 文件中。我已经注意到我的单个测试数据 XML 文件非常大,当我向其中添加更多实体时可能会开始变得难以管理。
现在,在阅读了这个问题之后,它看起来好像是不可能的,但我想确定一下。
如果有帮助,这是说明问题的示例代码。这个想法是程序与供应商相关联。我设置了包含 3 个供应商的测试数据,其中第二个有 3 个程序。TestData.xml 包含所有供应商和程序的所有测试数据。当我使用它时,单元测试按预期通过。如果我尝试使用多次调用分别读取单个 XML 文件,db.PerformDbOperation(DbOperationFlag.CleanInsertIdentity);
似乎第二次调用会覆盖第一次调用中所做的任何事情。
private const string xmlSchema = @"..\..\schema.xsd";
// All of the test data in one file.
private const string xmlData = @"..\..\XML Data\TestData.xml";
// Individual test data files.
private const string vendorData = @"..\..\XML Data\Vendor_TestData.xml";
private const string programData = @"..\..\XML Data\Program_TestData.xml";
public void WorkingExampleTest()
{
INDbUnitTest db = new SqlDbUnitTest(connectionString);
db.ReadXmlSchema(xmlSchema);
db.ReadXml(xmlData);
db.PerformDbOperation(DbOperationFlag.CleanInsertIdentity);
VendorCollection vendors = VendorController.List();
Assert.IsNotNull(vendors);
ProgramCollection collection = VendorController.GetPrograms(vendors[1].VendorID);
Assert.IsNotNull(collection);
Assert.IsTrue(collection.Count == 3);
}
public void NotWorkingExampleTest()
{
INDbUnitTest db = new SqlDbUnitTest(connectionString);
db.ReadXmlSchema(xmlSchema);
db.ReadXml(vendorData);
db.PerformDbOperation(DbOperationFlag.CleanInsertIdentity);
db.ReadXml(programData);
db.PerformDbOperation(DbOperationFlag.CleanInsertIdentity);
VendorCollection vendors = VendorController.List();
Assert.IsNotNull(vendors);
// This line throws an ArgumentOutOfRangeException because there are no vendors in the collection.
ProgramCollection collection = VendorController.GetPrograms(vendors[1].VendorID);
Assert.IsNotNull(collection);
Assert.IsTrue(collection.Count == 3);
}
这确实有效: