NUnit. Passing parameters into teardown method
c#, nunit, nunit-2.5, teardown, unit-testing
Solution
It is possible to reference the parameters to the Test directly from the `TestContext` in the `TearDown` function.
Something like this.
[Test]
[TestCase("Fred", "Bloggs")]
[TestCase("Joe", "Smith")]
public void MyUnitTest(string firstName, string lastName)
{
}
[TearDown]
public void TearDown()
{
string firstName = TestContext.CurrentContext.Test.Arguments[0] as string;
string lastName = TestContext.CurrentContext.Test.Arguments[1] as string;
}
It should be noted that `TestContext.CurrentContext.Test.Arguments` is just an object array and would be called for every test in the `TestFixture` regardless of the individual signatures so we ought to be careful to ensure we deal with all possible values in `Arguments[]` but it certainly is possible to reference these objects in the `TearDown` function
Problem
I'm using NUnit. I have my test method defined likeso: ``` [Test] [TestCase("Fred", "Bloggs")] [TestCase("Joe", "Smith")] public void MyUnitTest(string firstName, string lastName) { ... } ``` After a TestCase has finished, it goes into the TearDown Method. What'd like to do, is have those TestCase parameters that are passed into the test method but also passed into the TearDown method. Something like this: ``` [TearDown] public void TearDown(string firstName, string lastName) { ... } ``` I'm hoping that NUnit supports this out-of-the-box. Otherwise, I need to write bespoke code in the test method to store the test data in a collection. Then that collection is used in the TearDown method. If anyone has any thoughts .. would be great! Thanks. Christian