C# 是否可以在程序集中的所有测试之前和之后执行一个方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18485622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:21:54  来源:igfitidea点击:

Is it possible to execute a method before and after all tests in the assembly?

c#seleniumnunit

提问by boger

I would like to built an nunit project for selenium ui automation. I would like to sign in to the site before running all tests (all of them) and to close the browser after running all tests (all of them).

我想为 selenium ui 自动化构建一个 nunit 项目。我想在运行所有测试(所有测试)之前登录该站点,并在运行所有测试(所有测试)后关闭浏览器。

I can't use the SetUp since it related to fixtures and I want to do it before and after everything.

我不能使用 SetUp,因为它与灯具有关,我想在所有事情之前和之后都这样做。

Do you know who to execute it?

你知道由谁来执行吗?



I'm familiar with the SetUp and TearDown attribute. Let me explain it again.

我熟悉 SetUp 和 TearDown 属性。让我再解释一遍。

I need some logic to be executed before all tests from all fixtures starts (AKA - First test in the entire assembly) and also some logic to be executed after all tests from all fixtures ended (AKA - Last test in the entire assembly).

我需要在所有装置的所有测试开始之前执行一些逻辑(AKA - 整个程序集中的第一次测试),以及在所有装置的所有测试结束后执行的一些逻辑(AKA - 整个程序集中的最后测试)。

回答by Ross Patterson

Sure. That's what the [TestSetUp]and [TearDown]attributes are for. Don't confuse them with [TestFixtureSetUp]and [TestFixtureTearDown], which are executed before the first test and after the last.

当然。这就是[TestSetUp][TearDown]属性的用途。不要将它们与[TestFixtureSetUp]and混淆,它们[TestFixtureTearDown]在第一次测试之前和最后一次测试之后执行。

回答by Joachim Isaksson

The closest thing in nunitis the SetupFixtureattribute, which allows you to tag a class to do setup/teardown for all test fixtures in a namespace;

最接近的nunitSetupFixture属性,它允许您标记一个类以对命名空间中的所有测试装置进行设置/拆卸;

The SetUp method in a SetUpFixture is executed once before any of the fixtures contained in its namespace. The TearDown method is executed once after all the fixtures have completed execution.

SetUpFixture 中的 SetUp 方法在其命名空间中包含的任何装置之前执行一次。TearDown 方法在所有装置执行完毕后执行一次。

回答by matthewrdev

If all your test fixtures are within the same namespace then you can use the [SetUpFixture]attribute to mark a class as the global setup and teardown. You can then put all your login/logout functionality in there.

如果您所有的测试装置都在同一个命名空间内,那么您可以使用该[SetUpFixture]属性将一个类标记为全局设置和拆卸。然后,您可以将所有登录/注销功能放在那里。

NUNIT 2.x

NUNIT 2.x

namespace MyNamespace.Tests
{
    using System;
    using NUnit.Framework;

    [SetUpFixture]
    public class TestsSetupClass
    {
        [SetUp]
        public void GlobalSetup()
        {
            // Do login here.
        }

        [TearDown]
        public void GlobalTeardown()
        {
            // Do logout here
        }
    }
}

See: http://www.nunit.org/index.php?p=setupFixture&r=2.4

参见:http: //www.nunit.org/index.php?p=setupFixture&r=2.4

NUNIT 3.x

NUNIT 3.x

namespace MyNamespace.Tests
{
    using System;
    using NUnit.Framework;

    [SetUpFixture]
    public class TestsSetupClass
    {
        [OneTimeSetUp]
        public void GlobalSetup()
        {
            // Do login here.
        }

        [OneTimeTearDown]
        public void GlobalTeardown()
        {
            // Do logout here
        }
    }
}

See: https://github.com/nunit/docs/wiki/SetUpFixture-Attribute

请参阅:https: //github.com/nunit/docs/wiki/SetUpFixture-Attribute

回答by Sumeshk

Before executing each test cases [SetUp]section will executed

在执行每个测试用例[SetUp]部分之前将执行

after completed the execution of each test cases [TearDown]section will executed.

执行完成后,每个测试用例[TearDown]部分将被执行。

if we want to initialize variables we often write in [SetUp]section like a constructor

如果我们想初始化变量,我们经常[SetUp]像构造函数一样写在section 中

if we want to dispose any object we often write in [TearDown]section

如果我们要处理任何我们经常在[TearDown]section 中写的对象

    [SetUp]
    protected void SetUp()
    {
             //initialize objects
    }

    [TearDown]
    public void TearDown()
    {
       //dispose objects
    }