NUnit Testing: Failing to properly instantiate the test suite due to SetUp attributes

时光怂恿深爱的人放手 提交于 2020-01-06 20:15:15

问题


Good morning,

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.

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.

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.

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.


回答1:


I hope you followed this pattern:

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");
    }
  }
}

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.

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


来源:https://stackoverflow.com/questions/35157029/nunit-testing-failing-to-properly-instantiate-the-test-suite-due-to-setup-attri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!