Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nunit test setup method with argument

Tags:

Can we have a test set up method with arguments? I need a different set up for every test in a fixture.

Do we have something (or similar way) as the hypothetical idea :

[SetUp] [Argument("value-1")] [Argument("value-2")] [Argument("value-3")] public void InitializeTest(string value) {     //set env var with value } 
like image 352
dushyantp Avatar asked Apr 29 '13 10:04

dushyantp


People also ask

What is setup method in NUnit?

This attribute is used inside a TestFixture to provide a common set of functions that are performed just before each test method is called. SetUp methods may be either static or instance methods and you may define more than one of them in a fixture.

Does Setup run before every test NUnit?

In unit testing frameworks, Setup is called before each and every unit test within your test suite.

What is the use of setup () and TearDown ()?

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 .


1 Answers

It can be done using the TestFixture attribute with a parameter.

If all the tests in the class depend on the same parameter this is the way to go.

The class will need a constructor with the same parameter passed to the TestFixture attribute.

See Parameterized Test Fixtures on https://github.com/nunit/docs/wiki/TestFixture-Attribute

[TestFixture("Oscar")] [TestFixture("Paul")] [TestFixture("Peter")] public class NameTest {     private string _name;      public NameTest(string name)     {         _name = name;     }      [Test]     public void Test_something_that_depends_on_name()     {         //Todo...     }      [Test]     public void Test_something_that_also_depends_on_name()     {         //Todo...     }     //... } 

This code is from the nunit documentation website:

[TestFixture("hello", "hello", "goodbye")] [TestFixture("zip", "zip")] [TestFixture(42, 42, 99)] public class ParameterizedTestFixture {     private readonly string eq1;     private readonly string eq2;     private readonly string neq;      public ParameterizedTestFixture(string eq1, string eq2, string neq)     {         this.eq1 = eq1;         this.eq2 = eq2;         this.neq = neq;     }      public ParameterizedTestFixture(string eq1, string eq2)         : this(eq1, eq2, null)     {     }      public ParameterizedTestFixture(int eq1, int eq2, int neq)     {         this.eq1 = eq1.ToString();         this.eq2 = eq2.ToString();         this.neq = neq.ToString();     }      [Test]     public void TestEquality()     {         Assert.AreEqual(eq1, eq2);         if (eq1 != null && eq2 != null)             Assert.AreEqual(eq1.GetHashCode(), eq2.GetHashCode());     }      [Test]     public void TestInequality()     {         Assert.AreNotEqual(eq1, neq);         if (eq1 != null && neq != null)             Assert.AreNotEqual(eq1.GetHashCode(), neq.GetHashCode());     } } 
like image 177
Oscar Fraxedas Avatar answered Oct 02 '22 12:10

Oscar Fraxedas