0

Basically I have a test with loads of [TestMethods], This is all well and good but I need to initialize some variables and I want to do it every time the Test runs and not make a [TestMethod] which I have to call to do it.

Can this be done?

KingM0ses
  • 44
  • 7

2 Answers2

4

What you want to use is

[TestInitialize]
    /**
     * Runs at the beginning and only once
     **/
    public void Initialize()
    {
    }

Tha will run at the start of your test, or before you run the first test method from that test.

matthiasgh
  • 321
  • 1
  • 11
2

As well at [TestInitialize], Coded UI tests also allow for methods to have [ClassInitialize] and [AssemblyInitialize] attributes allowing additional places for initialization. There are also [TestCleanup], [ClassCleanup] and [AssemblyCleanup] attributes available for methods to clean up after tests run.

An additional level of initialization and clean up is possible via the ".testsettings" files. To create a .testsettings file, right click on the solution (not the project) in Solution Explorer, select Add => New Item. You would also have to select the .testsettings file via Menu => Test.

AdrianHHH
  • 13,492
  • 16
  • 50
  • 87