76

Quick question, how do I create a method that is run only once before all tests in the solution are run.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
mglmnc
  • 1,400
  • 2
  • 14
  • 15
  • I wish i knew, also :( Currently, i have an abstract base class which each TestClass inherit from. Inside that class, i have a TestInitialize method. Problem is, that method is fired off every time a new test is ran! – Pure.Krome Sep 21 '09 at 13:59
  • 1
    Have that abstract base class implement a static constructor. It will be fired only once before any of the tests are run. – mglmnc Sep 22 '09 at 11:56

3 Answers3

136

Create a public static method, decorated with the AssemblyInitialize attribute. The test framework will call this Setup method once per test run:

[AssemblyInitialize]
public static void MyTestInitialize(TestContext testContext)
{}

For TearDown its:

[AssemblyCleanup]
public static void TearDown() 
{}

EDIT:

Another very important detail: the class to which this method belongs must be decorated with [TestClass]. Otherwise, the initialization method will not run.

live2
  • 3,771
  • 2
  • 37
  • 46
driis
  • 161,458
  • 45
  • 265
  • 341
  • 4
    If you've got tests in more than one assembly then MyTestInitialize will get called more than once for your test run. – BenCr Jun 15 '11 at 10:05
  • 2
    It may not be clear - this runs for not every test, but every test RUN. Meaning if you run a set of tests, say by running all the tests in a class in one test run or all the tests in the assembly in one test run, this gets run ONCE for all of those tests in that run. So they could share the results/side-effects of this method, or not, if ony one test was run at a time. – bcr Jul 20 '20 at 14:40
8

Just to underscore what @driis and @Malice said in the accepted answer, here's what your global test initializer class should look like:

namespace ThanksDriis
{
    [TestClass]
    class GlobalTestInitializer
    {
        [AssemblyInitialize()]
        public static void MyTestInitialize(TestContext testContext)
        {
            // The test framework will call this method once -BEFORE- each test run.
        }

        [AssemblyCleanup]
        public static void TearDown() 
        {
            // The test framework will call this method once -AFTER- each test run.
        }
    }
}
Mass Dot Net
  • 2,150
  • 9
  • 38
  • 50
  • Is it not (also) true that a class marked [TestInitialize] also will execute before every test runs? Does AssemblyInitialize also run before each test runs or just once per test session? [TestInitialize()] public void TestInitialize() { ... } [TestCleanup()] public void TestCleanup() { ... } – Allen Apr 22 '20 at 15:38
  • 1
    Check out this StackOverflow link for a succinct answer about when code decorated with `[TestInitialize]` runs: https://stackoverflow.com/a/23012750/165494 – Mass Dot Net Apr 28 '20 at 20:27
0

Sorry for the crappy formatting...

        /// <summary>
        /// Use TestInitialize to run code before running each test
        /// Runs before every test executes
        /// </summary>
        [TestInitialize()]
        public void TestInitialize()
        {
           ...
           ...
        }


        /// <summary>
        /// Use TestCleanup to run code after each test has run
        /// Runs after every test executes
        /// </summary>
        [TestCleanup()]
        public void TestCleanup()
        {
           ...
           ...
        }
Allen
  • 546
  • 5
  • 12
  • This will stop your tests from running (or being discovered), the code should be: `public static void TestInitialize(TestContext testContext)` – Milan Jan 12 '23 at 09:29