2011-03-02 235 views
7

我有一些方法用於初始化和清理我用我的測試使用的數據庫,但是我的方法的屬性AssemblyInitialize和AssemblyCleanup沒有觸發。AssemblyCleanup not firing

任何想法?

[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

你能告訴我們你的代碼的一部分? – 2011-03-02 18:08:40

+0

我可以。這將只需要幾分鐘 – khr055 2011-03-02 18:09:24

+1

順便說一句,我必須提到,我真的想通過AssemblyCleanup屬性來刪除這些數據庫。這不是我失去了我的想法。 – khr055 2011-03-02 18:13:11

回答

12

想通了。早些時候發佈的那些方法都在基類中,其餘的測試用例將從中繼承,但基類沒有標記爲TestClass。顯然,沒有它,你仍然可以運行TestInitialize和TestCleanup方法,但不能運行Assembly>。 <

5

AssemblyCleanup是

static public void AssemblyCleanup 

確保這就是它的類是公共的,具有TestClass屬性