C#NUnit Selenium-将对象从SetUp传递到TestCase [英] C# NUnit Selenium - Passing Objects from SetUp to TestCase

查看:78
本文介绍了C#NUnit Selenium-将对象从SetUp传递到TestCase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不了解作用域在测试案例中如何工作.

I am not understanding how the scoping works on a test case.

[TestFixture]
class MockTests
{
    [Setup]
    private void StartTest()
    {
        IWebDriver driver = new ChromeDriver();
        TestLogger logger = new TestLogger();
    }

    [TestCase(TestName = "mock1")]
    {
        //Problem is here. Driver and logger "does not exist in the current context"
        driver.Navigate().GoToUrl("http://my.url.com");
        logger.Out("Hello! I cannot be accessed!");
    }

    [TestCase(TestName = "mock2")]
    {
        //I now have a new instance of driver and logger in my mock2 test
        driver.Navigate().GoToUrl("http://my.url.com");
        logger.Out("Hello! I am a new Instance of TestLogger!");
    }

    [TearDown]
    private void EndTest()
    {
        driver.Quit();
        logger.PrintReport(TestContext.name, TestContext.status);
    }
}

在我看来,如果您无法在整个测试中使用SetUp访问对象的唯一实例,那么SetUp就毫无意义.我看到以类似的方式使用vars/objects,但是在类范围内定义了vars/objects.即:私有IWebDriver驱动程序;

It seems to me the SetUp is pointless if you can't use it to access unique Instances of Objects across the tests. I see vars/objects used in a similar fashion, but with the vars/objects defined in the scope of the class; ie: private IWebDriver driver;

因此,如果我在一个测试夹具中有25个测试并并行运行;测试功能可能正在与错误的驱动程序通信,而不是在每个TestCase中创建唯一的实例.

So if I have 25 tests inside a single test fixture and run them in parallel; the test functions could be talking to the wrong driver instead of creating unique Instance inside every TestCase.

我该如何做才能知道TestLogger和驱动程序确实是唯一的?如何在设置测试和TearDown之间通过它们?

How do I do this so I know my TestLogger and driver are indeed unique? How do I pass them between the SetUp Test and TearDown?

TestLogger只是一个例子.它可能类似于User对象模型:

And TestLogger is just an example. It could be anything like a User object model:

public class User
{
    public string fName{get; set;}
    public string lName{get; set;}
    //etc...
}

也许我只是不了解应该如何设置测试.

Maybe I just am not understand how the test should be setup.

推荐答案

变量driverlogger在您的设置方法中是本地的.当方法退出时,它们将超出范围.

The variables driver and logger are local to your setup method. They go out of scope when the method exits.

如果您希望测试可以访问设置方法中的任何设置,则必须将其保存在成员变量中.

If you want your tests to have access to anything setup in the setup method, then it has to be saved in a member variable.

您的测试用例"是不可编译的.它们不是方法.它们似乎没有使用您传递的参数:"mock1"和"mock2".您最有可能要使用[Test]而不是[TestCases].

Your "test cases" are not compilable. They are not methods. They do not appear to make any use of the arguments you are passing: "mock1" and "mock2". Most likely, you want to user [Test] rather than [TestCases].

我看到的大多数Web测试示例都使用[OneTimeSetUp]而不是[SetUp]为每个测试治具创建一次驱动程序.您是否真的要为每个测试更改驱动程序?如果是这样,并且使用成员变量,则不能并行运行测试用例,因为它们会相互干扰.幸运的是,对于许多测试基座,并行运行的夹具可以提供足够好的性能.

Most examples I have seen of web testing create the driver only once per test fixture, using [OneTimeSetUp] rather than [SetUp]. Do you really want to change the driver for each test? If so, and if you use a member variable, then you can't run your test cases in parallel because they will interfere with one another. Fortunately, for many test bases, running fixtures in parallel gives sufficiently good performance.

更新:在您发表评论之后,很明显,您希望每个测试用例都有一个单独的驱动程序.您有两种选择...

Update: Following your comment, it's clear you want to have a separate driver per test case. You have two choices...

  1. 请勿并行运行测试用例.在固定装置上使用[Parallelizable],使其与其他固定装置并行运行,但由于测试范围中没有Children,因此测试用例是串行运行的.如果您还有其他可以并行运行的灯具,我想您会发现它非常有效.

  1. Do not run test cases in parallel. Use [Parallelizable] on the fixture, causing it to run in parallel with other fixtures, but with it's test cases running serially because you don't have Children in the scope. If you have a bunch of other fixtures, with which you can run in parallel, I think you will find this to be pretty effective.

并行运行案例,但不要使用SetUp方法创建驱动程序.而是在每个测试中创建它.如果占用一行以上,请使用一种方法将其返回.您的每个测试用例都有一个单独的驱动程序,您消除了任何测试相互叠加的机会.

Run the cases in parallel, but don't use the SetUp method to create a driver. Instead, create it in each test. If it takes more than a line, use a method to return it. Each of your test cases will have a separate driver and you eliminate any chance of the tests stepping on one another.

FWIW,该示例使我想再次看一下如何在不重写整个框架的情况下为并行测试提供更好的夹具分离.

FWIW, this example is leading me to want to take another look at how to provide better fixture separation for parallel tests without rewriting the whole framework.

这篇关于C#NUnit Selenium-将对象从SetUp传递到TestCase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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