Passing parameters to TestCleanup

I want to run TestCleanup in my unit tests, but I need to pass the parameter to the cleanup method. But since TestCleanup is called automatically by default, I cannot pass any parameters to it.

Can anyone suggest a way to do this?

+5
source share
2 answers

You can use the instance variable of the test class to communicate between installation testing, testing and cleaning 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 method CleanUp()will run after each unit test, it someValuewill be bound to the correct unit test context.

Hope this helps.

+3
source

, , , , , , , , - , , , , - .

, .

, , , , ( , , ( , ), ), ( ), , TestFixture, , , , , , , .

0

All Articles