101

Instead of [SetUp] and [TearDown] in Nunit what is the alternative in Visual Studio Ultimate 2010 Unit Testing. In Nunit you can imagine setup and teardown methods are as constructors and destructors for the tests in our class.

boutta
  • 24,189
  • 7
  • 34
  • 49
Etibar - a tea bar
  • 1,912
  • 3
  • 17
  • 30
  • 7
    possible duplicate of [What would be an alternate to \[SetUp\] and \[TearDown\] in MSTest?](http://stackoverflow.com/questions/6193744/what-would-be-an-alternate-to-setup-and-teardown-in-mstest) – jessehouwing Jan 22 '13 at 15:16
  • 1
    Possible duplicate of [What would be an alternate to \[SetUp\] and \[TearDown\] in MSTest?](http://stackoverflow.com/questions/6193744/what-would-be-an-alternate-to-setup-and-teardown-in-mstest) – Simon Bosley May 10 '17 at 09:39

2 Answers2

162

A method annotated with [TestInitialize] is run before each test. Likewise [TestCleanup] is after each test.

[ClassInitialize] and [ClassCleanup] are run before and after the 'suite' of tests inside the TestClass.

Mikeb
  • 6,271
  • 3
  • 27
  • 41
5

Visual Studio will use MSTest, that is Microsoft's unit testing framework, it is similar to NUnit. In fact, most of them are similar in concepts but different syntax.

In order to view the comparaison, the creators of xUnit (another unit testing framework) have a list here:

https://xunit.net/docs/comparisons

More specifically what you asked for, TestInitialize is MSTest's equivalent to NUnit's Setup, and the same for TestCleanup and TearDown.

One thing to note, I would stay with NUnit if you are attempting to do automated unit testing or some kind of continuous integration. The main reason the various *unit frameworks are favoured over MSTest is because you must have a copy of Visual Studio on the machine you are running the tests on. Fine for your own local machine, different story for a CI server. Visual Studio is a pig of a program, and to install it on a server (which is generally supposed to be as lightweight and fast as it can be), just to run tests is a bit annoying.

Zyo
  • 1,934
  • 21
  • 25
Arran
  • 24,648
  • 6
  • 68
  • 78
  • 9
    You can simply install TFS Team Test Agent (and not configure it) on a CI server to get the test runners installed (http://www.microsoft.com/en-us/download/details.aspx?id=1334). No need for a full Visual Studio installation, unless you want Generic, Ordered, CodedUI or Performance tests. – jessehouwing Jan 22 '13 at 15:22
  • 1
    Team Build 2012 installs support for different test runners out-of-the-box without the need to install Visual Studio on your build server as well. – jessehouwing Jan 22 '13 at 15:24