264

When I use MSTest Framework, and copy the code that Selenium IDE generated for me, MSTest doesn't recognize [TearDown] and [SetUp]. What is the alternative to this?

Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
Maya
  • 7,053
  • 11
  • 42
  • 53

4 Answers4

313

You would use [TestCleanup] and [TestInitialize] respectively.

Tejs
  • 40,736
  • 10
  • 68
  • 86
294

Keep in mind that your Initialize/Cleanup methods have to use the right signature.

http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.classinitializeattribute.aspx

    [AssemblyInitialize()]
    public static void AssemblyInit(TestContext context) {}

    [ClassInitialize()]
    public static void ClassInit(TestContext context) {}

    [TestInitialize()]
    public void Initialize() {}

    [TestCleanup()]
    public void Cleanup() {}

    [ClassCleanup()]
    public static void ClassCleanup() {}

    [AssemblyCleanup()]
    public static void AssemblyCleanup() {}
Dunken
  • 8,481
  • 7
  • 54
  • 87
108

[TestInitialize] and [TestCleanup] at the individual test level, [ClassInitialize] and [ClassCleanup] at the class level.

John Gardner
  • 24,225
  • 5
  • 58
  • 76
10

You can use [TestInitialize] for [SetUp] and [TestCleanup] for [TearDown].

Rick the Scapegoat
  • 1,056
  • 9
  • 19
Mohsin Awan
  • 1,176
  • 2
  • 12
  • 29