Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Setup and Teardown working per-Fixture?

Tags:

c#

nunit

I have the following:

    [TestFixture]
    public class SmokeTest : BaseTest
    {
        [Test(Description = "Should Do This")]
        public void ShouldDoThis()
        {
            //Tests,Assertions,etc
        }

        [Test(Description = "Should Do That")]
        public void ShouldDoThat()
        {
            //Tests,Assertions,etc
        }

    }

With BaseTest defined as:

   [TestFixture]
   public class BaseTest
   {
    [TestFixtureSetUp]
    public void SetUp()
    {
        // set up browsers
    }
    [TearDown]
    public void Dispose()
    {
        // dispose browsers
    }
   }

The goal is to have the selenium browsers' drivers created once per testFixture (// set up browsers), then at the end of the Fixture, torn down. Right now the browsers are being killed after the first test though, and the second test fails with some "Unable to connect to the remote server" error.

I'd like to target the first problem here - why is the TearDown method being called after the first test?

like image 579
RobVious Avatar asked Oct 08 '13 21:10

RobVious


People also ask

Where will you use setup () and teardown () methods?

Prepare and Tear Down State for a Test Class XCTest runs setUp() once before the test class begins. If you need to clean up temporary files or capture any data that you want to analyze after the test class is complete, use the tearDown() class method on XCTestCase .

How can you run setup () and teardown () code at once for all of your tests?

As outlined in Recipe 4.6, JUnit calls setUp( ) before each test, and tearDown( ) after each test. In some cases you might want to call a special setup method once before a series of tests, and then call a teardown method once after all tests are complete.

What is test setup and teardown?

The setUp() and tearDown() methods allow you to define instructions that will be executed before and after each test method. They are covered in more detail in the section Organizing test code. The final block shows a simple way to run the tests. unittest.main() provides a command-line interface to the test script.

Does teardown run after every test?

Code in a teardown() block is run upon completion of a test file, even if it exits with an error.


1 Answers

You need to use the TestFixtureTearDown attribute instead of the TearDown attribute in your base test. The TestFixtureTearDown attribute will cause the method to be run only once at the end of all of the tests in the fixture.

like image 107
adrianbanks Avatar answered Oct 03 '22 19:10

adrianbanks