簡體   English   中英

MSTest中的ClassCleanup是靜態的,但是構建服務器使用nunit來運行單元測試。 我該如何調整?

[英]ClassCleanup in MSTest is static, but the build server uses nunit to run the unit tests. How can i adjust?

MSTest具有[ClassCleanup()]屬性,據我所知,該屬性必須是靜態的。 我喜歡在單元測試運行后進行操作,並清理數據庫。 這一切都很好,但是當我轉到構建服務器並使用Nant構建腳本時,似乎單元測試是使用NUnit運行的。 NUnit似乎不喜歡cleanup方法是靜態的。 因此,它忽略了我在該課程中的測試。 我該怎么辦才能補救? 我不想使用[TestCleanUp()],因為它是在每次測試后運行的。 有沒有人有什么建議? 我知道[TestCleanup()]有助於解耦,但在這種情況下,我真的更喜歡[ClassCleanup()]。 這是一些示例代碼。

  ////Use ClassCleanup to run code after all tests have run
    [ClassCleanup()]
    public static void MyFacadeTestCleanup()
    {

        UpdateCleanup();

    }


    private static void UpdateCleanup()
    {
        DbCommand dbCommand;
        Database db;

        try
        {

            db = DatabaseFactory.CreateDatabase(TestConstants.DB_NAME);
            int rowsAffected;

            dbCommand = db.GetSqlStringCommand("DELETE FROM tblA WHERE biID=@biID");
            db.AddInParameter(dbCommand, "biID", DbType.Int64, biToDelete);
            rowsAffected = db.ExecuteNonQuery(dbCommand);
            Debug.WriteLineIf(rowsAffected == TestConstants.ONE_ROW, string.Format("biId '{0}' was successfully deleted.", biToDelete));

        } catch (SqlException ex) {



        } finally {
            dbCommand = null;
            db = null;

            biDelete = 0;

        }
    }

感謝您的指導,是的,我意識到我什么都沒捉到。 我需要首先克服這個障礙。

干杯,〜ck在聖地亞哥

您的構建服務器將忽略測試,因為MSTest使用一組不同的屬性來指定NUnit使用的測試。 如果未看到任何測試,則很可能是您遇到的問題。

例如:MSTest使用[TestClass][TestMethod]指定測試夾具和測試,而NUnit使用[TestFixture][Test]

同樣,NUnit等效於[ClassCleanup][TestFixtureTearDown] ,它不是靜態的。

請記住,如果您的測試絕對必須在MSTest和NUnit上運行,則可以使用這兩個框架的屬性來裝飾測試,並且它將起作用(無論如何在一定程度上)。 但是,要同時實現ClassCleanup行為,您需要這樣的東西:

[ClassCleanup]
public static void MSTestClassCleanup()
{
    CommonCleanup();
}

[TestFixtureTearDown]
public void NUnitTearDown()
{
    CommonCleanup();
}

public static void CommonCleanup()
{
    // Actually clean up here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM