是否可以在组装中的所有测试之前和之后执行方法? [英] Is it possible to execute a method before and after all tests in the assembly?

查看:76
本文介绍了是否可以在组装中的所有测试之前和之后执行方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

我可以

您知道谁来执行它吗?

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

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

在所有夹具的所有测试开始之前,我需要执行一些逻辑(AKA-整个组件中的第一个测试),然后在所有固定装置的所有测试结束后(也称为-整个装配体中的最后一个测试),还需要执行一些逻辑。

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).

推荐答案

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

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.

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
        }
    }
}

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

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
        }
    }
}

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

这篇关于是否可以在组装中的所有测试之前和之后执行方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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