AssemblyCleanup not firing

≡放荡痞女 提交于 2019-12-23 08:54:36

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!