9

I'm using Visual Studio's Test Tools for unit testing. I need some initialization code to run before each test.

I have a Setup class for the initialization code. I already added code to run before each test run, using [AssemblyInitialize], but I can't figure out how to do the same on a single test basis.

I tried using the [TestInitialize] attribute, but this only applies to tests in the same file as the [TestInitialize] method. I would like the initialization code to run automatically for all tests in the assembly, without having to explicitly call it in each and every test file.

[TestClass]
public class Setup
{
    [AssemblyInitialize]
    public static void InitializeTestRun(TestContext context)
    {
        //... code that runs before each test run
    }

    [TestInitialize] //this doesn't work!
    public static void InitializeTest()
    {
        //... code that runs before each test
    }
}
fikkatra
  • 5,605
  • 4
  • 40
  • 66
  • 2
    There is no equivalent of `AssemblyInitialize` that runs for every test. I think you would have to base all your classes on a common base class to do that. What do you need to do in the initialisation? There may be another way. – stuartd Jun 16 '16 at 15:18
  • Basically I'm mocking an Entity Framework context by using in memory dbsets, to avoid actually calling the real database. I'd like a fresh instance of a context for each test, i.e. a clean, empty 'db', so the tests do not interfere with eachother. – fikkatra Jun 16 '16 at 15:36
  • I'm not sure there's a clean way of doing that, all your test files may have to have their own `TestInitialize` method which calls the db initialization. – stuartd Jun 16 '16 at 15:40
  • 1
    Don't know MSTest enough but the following should work (at least it works with other test frameworks): Create a base class DatabaseIntegrationTests with the TestInitialize method. Derive your other Testclasses from that base class. – tobsen Jun 17 '16 at 06:52
  • @tobsen if you convert your comment to an answer, I will accept it – fikkatra Jul 04 '16 at 11:00

2 Answers2

8

The following should work (at least it works with other test frameworks):

  • Create a base class DatabaseIntegrationTests with the TestInitialize method
  • Derive your other Testclasses from that base class
tobsen
  • 5,328
  • 3
  • 34
  • 51
7

It is [TestInitialize] but you have the syntax wrong, it doesn't take a context:

[TestInitialize]
public static void InitializeTests()
{
    //... code that runs before each test
}
stuartd
  • 70,509
  • 14
  • 132
  • 163
  • I indeed got the syntax wrong (I will adjust it in the question), but sadly this doesn't solve the problem: TestInitialize does not apply to tests in another file – fikkatra Jun 16 '16 at 14:49
  • is the other file a partial file of the same class? IIRC the InitializeTests will only be run for all the tests of the surrounding class (in your case public class Setup) – tobsen Jun 16 '16 at 15:04
  • No it isn't. I have many test classes, but only a single Setup class. – fikkatra Jun 16 '16 at 15:40