如何进行整合测试 [英] How tear down integration tests

查看:172
本文介绍了如何进行整合测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对我的项目进行一些集成测试.现在,我正在寻找一种机制,该机制允许我在所有测试开始之前执行一些代码,并在所有测试结束之后执行一些代码.

I want to do some integration tests on my project. Now I am looking for a mechanism that allow me execute some code before all tests begin and execute some code after all tests ends.

请注意,我可以为每个测试设置方法和拆卸方法,但是整体上我需要相同的功能.

Note that I can have set up methods and tear down method for each tests but I need the same functionality for all tests as a whole.

请注意,我使用的是Visual Studio,C#和NUnit.

Note that I using Visual Studio, C# and NUnit.

推荐答案

使用NUnit.Framework.TestFixture属性注释测试类,并使用NUnit.Framework.TestFixtureSetUp和NUnit注释所有测试设置和所有测试拆卸方法. Framework.TestFixtureTearDown.

Annotate your test class with the NUnit.Framework.TestFixture attribute, and annotate you all tests setup and all tests teardown methods with NUnit.Framework.TestFixtureSetUp and NUnit.Framework.TestFixtureTearDown.

这些属性的功能类似于SetUp和TearDown,但每个夹具只运行一次它们的方法,而不是每次测试之前和之后.

These attributes function like SetUp and TearDown but only run their methods once per fixture rather than before and after every test.

编辑以回应评论: 为了在所有测试完成后运行方法,请考虑以下内容(不是最干净的方法,但我不确定有更好的方法):

EDIT in response to comments: In order to have a method run after ALL tests have finished, consider the following (not the cleanest but I'm not sure of a better way to do it):

internal static class ListOfIntegrationTests {
    // finds all integration tests
    public static readonly IList<Type> IntegrationTestTypes = typeof(MyBaseIntegrationTest).Assembly
        .GetTypes()
        .Where(t => !t.IsAbstract && t.IsSubclassOf(typeof(MyBaseIntegrationTest)))
        .ToList()
        .AsReadOnly();

    // keeps all tests that haven't run yet
    public static readonly IList<Type> TestsThatHaventRunYet = IntegrationTestTypes.ToList();
}

// all relevant tests extend this class
[TestFixture]
public abstract class MyBaseIntegrationTest {
    [TestFixtureSetUp]
    public void TestFixtureSetUp() { }

    [TestFixtureTearDown]
    public void TestFixtureTearDown() {
        ListOfIntegrationTests.TestsThatHaventRunYet.Remove(this.GetType());
        if (ListOfIntegrationTests.TestsThatHaventRunYet.Count == 0) {
            // do your cleanup logic here
        }
    }
}

这篇关于如何进行整合测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆