简体   繁体   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 has a [ClassCleanup()] attribute, which needs to be static as far as I can tell. MSTest具有[ClassCleanup()]属性,据我所知,该属性必须是静态的。 I like to run through after my unit tests have run,and clean up my database. 我喜欢在单元测试运行后进行操作,并清理数据库。 This all works great, however when I go to our build server and use our Nant build script, it seems like the unit tests are run with NUnit. 这一切都很好,但是当我转到构建服务器并使用Nant构建脚本时,似乎单元测试是使用NUnit运行的。 NUnit doesn't seem to like the cleanup method to be static. NUnit似乎不喜欢cleanup方法是静态的。 It therefore ignores my tests in that class. 因此,它忽略了我在该课程中的测试。 What can I do to remedy this? 我该怎么办才能补救? I prefer to not use [TestCleanUp()] as that is run after each test. 我不想使用[TestCleanUp()],因为它是在每次测试后运行的。 Does anyone have any suggestions? 有没有人有什么建议? I know [TestCleanup()] aids in decoupling, but I really prefer the [ClassCleanup()] in this situation. 我知道[TestCleanup()]有助于解耦,但在这种情况下,我真的更喜欢[ClassCleanup()]。 Here is some example code. 这是一些示例代码。

  ////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;

        }
    }

Thanks for any pointers and yes i realize I'm not catching anything. 感谢您的指导,是的,我意识到我什么都没捉到。 I need to get passed this hurdle first. 我需要首先克服这个障碍。

Cheers, ~ck in San Diego 干杯,〜ck在圣地亚哥

Your build server is ignoring your tests because MSTest uses a different set of attributes to specify tests to what NUnit uses. 您的构建服务器将忽略测试,因为MSTest使用一组不同的属性来指定NUnit使用的测试。 This is more likely the issue you're having, if it's not seeing any of your tests. 如果未看到任何测试,则很可能是您遇到的问题。

For example: MSTest uses [TestClass] and [TestMethod] to specify a test fixtures and tests, while NUnit uses [TestFixture] and [Test] . 例如:MSTest使用[TestClass][TestMethod]指定测试夹具和测试,而NUnit使用[TestFixture][Test]

Also, NUnit's equivalent to [ClassCleanup] is [TestFixtureTearDown] , and it isn't static. 同样,NUnit等效于[ClassCleanup][TestFixtureTearDown] ,它不是静态的。

Bear in mind that if your tests absolutely have to run on MSTest and NUnit, you can decorate the tests with attributes for both frameworks and it will work (to a certain degree, anyway). 请记住,如果您的测试绝对必须在MSTest和NUnit上运行,则可以使用这两个框架的属性来装饰测试,并且它将起作用(无论如何在一定程度上)。 However, to get ClassCleanup behaviour with both, you'd need something like this: 但是,要同时实现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.

相关问题 如何并行运行单元测试(MSTest)? - How to run unit tests (MSTest) in parallel? 如何在每个 class 之后运行 ClassCleanup (MSTest) 并进行测试? - How to run ClassCleanup (MSTest) after each class with test? Azure Devops - MSTest - 如果我的单元测试失败,我如何使构建失败,但在集成测试失败时继续(带有警告/通知) - Azure Devops - MSTest - How can I fail a build if my unit tests fail, but continue (with warning/notification) when integration test fail MSTest - 从构建服务器隐藏一些单元测试 - MSTest - Hide some unit tests from build server 如何运行mstest单元测试,以便基础数据库有时间正确构建? - How to run mstest unit tests so that an underlying database has time to build properly? 如何并行运行NUnit(Selenium Grid)测试? - How can I run NUnit(Selenium Grid) tests in parallel? 设置Build Server以运行NUnit Selenium自动化测试 - Setting up Build Server to run NUnit Selenium Automated tests 如何设置NUnit来运行项目的单元测试? - How do I set up NUnit to run my project's unit tests? 如何确定在构建服务器中构建时要运行或忽略的nunit测试 - How to determine which nunit tests to run or ignore while building in build server 使用NUnit在不同的appdomain中运行单元测试 - Run unit tests in different appdomain with NUnit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM