Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters in TestCleanup

I want to perform TestCleanup in my unit tests but I need to pass a parameter to the clean-up method. But since the default TestCleanup is called automatically I am not able to pass any parameters to it.

Can somebody please suggest a way to do this?

like image 578
ssingh Avatar asked Aug 02 '11 07:08

ssingh


1 Answers

You could use a test class instance variable to communicate between the setup, test, and cleanup test methods:

namespace YourNamespace
{
    [TestClass]
    public class UnitTest1
    {
        private string someValue;

        [TestMethod]
        public void TestMethod1()
        {
            someValue = "someValue";
        }

        [TestCleanup]
        public void CleanUp()
        {
            // someValue is accessible here.
        }
    }
}

Since the the CleanUp() method will run after every unit test, someValue will be bound to the correct unit test's context.

Hope this helps.

like image 195
Zorayr Avatar answered Oct 17 '22 02:10

Zorayr