我有一些方法用于初始化和清理我正在使用我的测试的数据库,但我的方法没有触发属性AssemblyInitialize和AssemblyCleanup。
有什么想法吗?
[TestInitialize]
public void Init()
{
LoadData();
}
[AssemblyInitialize]
public void AssemblyInit()
{
}
public void LoadData(string testDataFileName = "TestData.xml")
{
connectionString = ConfigurationManager.ConnectionStrings["NDbUnit"].ConnectionString;
mySqlDatabase = new NDbUnit.Core.SqlClient.SqlDbUnitTest(connectionString);
mySqlDatabase.ReadXmlSchema("DataSet.xsd");
mySqlDatabase.ReadXml(testDataFileName);
mySqlDatabase.PerformDbOperation(NDbUnit.Core.DbOperationFlag.CleanInsertIdentity);
dataSet = mySqlDatabase.CopyDataSet();
}
[AssemblyCleanup]
public void RemoveDatabases()
{
List<string> databasesToDelete = new List<string>();
ServerConnection serverConnection = new ServerConnection(new SqlConnection(connectionString));
Server server = new Server(serverConnection);
foreach (Database db in server.Databases)
{
if (db.Name.ToLower().Contains(testDatabaseIdentifier))
{
databasesToDelete.Add(db.Name);
}
}
databasesToDelete.ForEach(x =>
{
Database db = new Database(server, x);
db.Refresh();
db.Drop();
});
}
[TestCleanup]
public void CleanUpData()
{
mySqlDatabase.PerformDbOperation(NDbUnit.Core.DbOperationFlag.DeleteAll);
}
答案 0 :(得分:13)
想出来。之前发布的那些方法是在其他测试用例将继承的基类中,但是基类没有标记为TestClass。显然,如果没有它,你仍然可以运行TestInitialize和TestCleanup方法,但不能运行程序集&gt;。&lt;
答案 1 :(得分:5)
AssemblyCleanup是
static public void AssemblyCleanup
确保其类是公开的并且具有TestClass
属性