NUnit 测试:由于设置属性无法正确实例化测试套件 [英] NUnit Testing: Failing to properly instantiate the test suite due to SetUp attributes

查看:60
本文介绍了NUnit 测试:由于设置属性无法正确实例化测试套件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

作为上下文,我正在使用 nUnit 3.0.1(使用 Selenium)的自动化测试套件中工作.测试直接从 Visual Studio 2013 的测试资源管理器运行.

As context, I am working in an automated test suite using nUnit 3.0.1 (with Selenium). The tests are being runned directly from Visual Studio 2013' Test Explorer.

我的问题是:在开始运行测试套件时,我试图自动设置一些设置功能.这些是所有测试套件只需要完成一次的事情,因此对每个测试类使用 [SetUp] 属性并不是我想要的.

My problem is this: There is some Setting up functionality that I am trying to make automatic when starting to run the test suite. These are things that need to be done only ONCE for all the suite of tests, so using the [SetUp] Attribute for each test class is not what I intend.

然而,当我运行测试时,我的设置甚至没有被触及:(.调试确认设置没有完成.

When I run the Tests however, my setting up is not even touched :(. Debugging confirms that the setting up is not being done.

到目前为止,我已经尝试使用:[SetUpFixture] 和 [OneTimeSetUp] 或 [SetUp](在许多组合中),还尝试删除命名空间.我仍然没有得到我需要的东西.

So far I have tried using: [SetUpFixture] along with [OneTimeSetUp] or [SetUp] (in many combinations), also tried removing the namespace. I am still not getting what I need.

我对 nUnit 还很陌生,所以我想要一些关于可能出错的指导,或者确保脚本设置运行的一些替代方法.

I am fairly new with nUnit, so I would like some guidance about what could be wrong, or some alternatives to making sure the scripted setup is run.

推荐答案

我希望你遵循这个模式:

I hope you followed this pattern:

在下面的 MySetupClass 中,它定义了仅在测试运行开始和结束时执行一次的测试套件设置和拆卸方法.请参阅 SetupFixture 文档.

In the below MySetupClass which defines the Test Suite setup and teardown methods which are executed only once at the start and end of the test runs. Refer SetupFixture documentation.

MySetupClass.cs

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

  [SetUpFixture]
  public class MySetUpClass
  {
    [OneTimeSetUp]
    public void RunBeforeAnyTests()
    {
      Console.WriteLine("SetupFixture - OneTimeSetup");
    }

    [OneTimeTearDown]
    public void RunAfterAllTests()
    {
      Console.WriteLine("Suite TearDown - OneTimeTearDown");
    }
  }
}

在下面的 SuccessTests.cs 中,SetupTearDown 在每次测试之前和之后执行,因为这些方法定义在一个类中用属性 TestFixture 标记.OneTimeSetup &OneTimeTearDown 属性定义了测试用例中的方法,在类中的所有测试方法之前和之后执行一次.

In the following SuccessTests.cs, Setup and TearDown are executed before and after each test, since these methods are defined in a class marked with atttribute TestFixture. OneTimeSetup & OneTimeTearDown attributes defines methods in the test case to be executed once before and after all the test methods in the class.

请参阅 [此处][2] 的 Setup 属性文档.

Refer to the documentation of Setup attribute [here][2].

SuccessTests.cs

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

  [TestFixture]
  public class SuccessTests
  {
    [SetUp] 
    public void SetUp()
    {  Console.WriteLine("Test Setup"); }

    [TearDown]
    public void TearDown()
    {  Console.WriteLine("Test Teardown"); }


    [OneTimeSetup]
    public void OneTimeSetup()
    {  Console.WriteLine("Test Fixture - OneTimeSetup"); }

    [OneTimeTearDown]
    public void OneTimeTearDown()
    {  Console.WriteLine("Test Fixture - OneTimeTearDown"); }

    [Test]
    public void Test1()
    {  Console.WriteLine("Actual Test1"); }

    [Test]
    public void Test2()
    {  Console.WriteLine("Actual Test2"); }

  }
}

运行上述测试将得到这个结果.

Running the above tests will give this result.

Suite Setup - OneTimeSetup
Test Fixture - OneTimeSetup
Test Setup
Actual Test1
Test Teardown
Test Setup
Actual Test2
Test Teardown
Test Fixture TearDown - OneTimeTearDown
Suite TearDown - OneTimeTearDown

这篇关于NUnit 测试:由于设置属性无法正确实例化测试套件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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