问题
I have some methods that are used for initializing and cleaning up a database that I'm using with my tests, but my methods with attributes AssemblyInitialize and AssemblyCleanup aren't firing.
Any ideas?
[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);
}
回答1:
Figured it out. Those methods posted earlier were in a base class that the rest of the test cases were going to inherit from, but the base class was not marked as a TestClass. Apparently, without it you can still run the TestInitialize and TestCleanup methods, but not the Assembly ones >.<
回答2:
AssemblyCleanup is
static public void AssemblyCleanup
Make sure thats its class is public and has a TestClass
attribute
来源:https://stackoverflow.com/questions/5171531/assemblycleanup-not-firing