繁体   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