从SetUpFixture将参数传递给TestFixture OneTimeSetUp [英] Passing an argument to TestFixture OneTimeSetUp From SetUpFixture

查看:231
本文介绍了从SetUpFixture将参数传递给TestFixture OneTimeSetUp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用C#,nunit和Selenium构建一个自动化项目,并且我试图拥有一个安装类,该类将在运行任何TestFixture之前以及它们全部结束并初始化后初始化selenium webdriver.试图从每个TestFixture OneTimeSetUp属性中的TestFixture获取该webdriver,可以这样做还是应该将selenium类更改为static,以便能够在每个TestFixture设置中获取driver字段?

I'm currently building an automation project using C#, nunit and Selenium and I'm trying to have a setup class that will initialize the selenium webdriver before any TestFixture will run and after all of them have ended and trying to get that webdriver from the TestFixture in each TestFixture OneTimeSetUp attribute, can it be done or should I change the selenium class to static in order to be able to get the driver field in each TestFixture setup?

所以基本结构是这样:

[SetUpFixture]
public class Test
{
    [OneTimeSetUp]
    public void Init()
    {
       _driver = new Driver();
    }
}

[TestFixture]
public class FirstTest
{
    [OneTimeSetUp]
    public void Init()
    {
        xxxxxxxxxx - here I need to initialize a class with the driver from the setup class
    }
}

推荐答案

按照目前的情况,NUnit为您执行此操作的唯一方法是使用静态方法.但是,如果您同时测试多个驱动程序,则此方法将无法正常工作.

As things currently stand, the only way NUnit can do this for you is if you use a static. But this won't work well if you are testing multiple drivers in parallel.

一个真正的解决方案(需要新的NUnit功能)是允许安装程序夹具将信息保存在TestContext中,然后您的个人夹具就可以访问信息.

A true solution (requiring a new NUnit feature) would be to allow setup fixtures to save info in the TestContext, which your individual fixture could then access.

一种解决方法是在夹具onetimesetup中创建一个新驱动程序,但前提是尚未创建该驱动程序.这也需要一个静态变量,但您可以对其进行设置,以使所支持的每种驱动程序类型都有一个单独的静态变量.

A workaround would be to create a new driver in the fixture onetimesetup, but only if it had not already been created. That too would require a static, but you could set it up so that there was a separate static for each driver type you support.

这篇关于从SetUpFixture将参数传递给TestFixture OneTimeSetUp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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