Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssemblyCleanup not firing

Tags:

c#

mstest

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);
    }
like image 885
khr055 Avatar asked Mar 02 '11 18:03

khr055


2 Answers

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 >.<

like image 87
khr055 Avatar answered Sep 27 '22 20:09

khr055


AssemblyCleanup is

static public void AssemblyCleanup

Make sure thats its class is public and has a TestClass attribute

like image 34
keshav Avatar answered Sep 27 '22 20:09

keshav