Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to execute a method before and after all tests in the assembly?

Tags:

c#

selenium

nunit

I would like to built an nunit project for selenium ui automation. I would like to sign in to the site before running all tests (all of them) and to close the browser after running all tests (all of them).

I can't use the SetUp since it related to fixtures and I want to do it before and after everything.

Do you know who to execute it?


I'm familiar with the SetUp and TearDown attribute. Let me explain it again.

I need some logic to be executed before all tests from all fixtures starts (AKA - First test in the entire assembly) and also some logic to be executed after all tests from all fixtures ended (AKA - Last test in the entire assembly).

like image 486
boger Avatar asked Aug 28 '13 10:08

boger


People also ask

Which attribute in NUnit lets you execute a method before and after a test case?

This attribute is to identify methods that are called once prior to executing any of the tests in a fixture.

What is Setupfixture?

This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace. The class may contain at most one method marked with the OneTimeSetUpAttribute and one method marked with the OneTimeTearDownAttribute.

What is TestFixtureSetUp?

Whereas the TestFixtureSetUp is where the test fixture, the required initial state of the system before tests are run, is initialized. Follow this answer to receive notifications.

What is test attribute in NUnit?

The Test attribute is one way of marking a method inside a TestFixture class as a test. It is normally used for simple (non-parameterized) tests but may also be applied to parameterized tests without causing any extra test cases to be generated.


1 Answers

If all your test fixtures are within the same namespace then you can use the [SetUpFixture] attribute to mark a class as the global setup and teardown. You can then put all your login/logout functionality in there.

NUNIT 2.x

namespace MyNamespace.Tests {     using System;     using NUnit.Framework;      [SetUpFixture]     public class TestsSetupClass     {         [SetUp]         public void GlobalSetup()         {             // Do login here.         }          [TearDown]         public void GlobalTeardown()         {             // Do logout here         }     } } 

See: http://www.nunit.org/index.php?p=setupFixture&r=2.4

NUNIT 3.x

namespace MyNamespace.Tests {     using System;     using NUnit.Framework;      [SetUpFixture]     public class TestsSetupClass     {         [OneTimeSetUp]         public void GlobalSetup()         {             // Do login here.         }          [OneTimeTearDown]         public void GlobalTeardown()         {             // Do logout here         }     } } 

See: https://github.com/nunit/docs/wiki/SetUpFixture-Attribute

like image 200
matthewrdev Avatar answered Sep 19 '22 17:09

matthewrdev