7

In Microsoft's UnitTesting namespace (Microsoft.VisualStudio.TestTools.UnitTesting) there are AssemblyInitialize and AssemblyCleanup attributes you can apply to static methods and they will be called before and after all tests respectively.

[AssemblyInitialize]
static public void AssemblyInitialize(TestContext testCtx)
{
    // allocate resources
}

[AssemblyCleanup]
static public void AssemblyCleanup()
{
    // free resources
}

My question: is it possible and safe to access the TestContext within AssemblyCleanup()? If not, is storing resource references as static members a reasonable alternative or could that cause problems as well?

Additionally/optionally: what is the reasoning behind not passing a reference to the TestContext to clean-up methods?

Dylan Corriveau
  • 2,561
  • 4
  • 29
  • 36
Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112

2 Answers2

1

You can't pass any paramaters to AssemblyCleanup method. Here's the error if you try to do so:

Result Message: Method SomeNamespace.TestDatabaseInitializer.AssemblyCleanup has wrong signature. The method must be static, public, does not return a value and should not take any parameter.

Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
1

I'm accessing a static property on the same class and it seems to be working fine. I'll update this answer if I encounter any problems. I am not, however, accessing the TestContext so I'm curious if that would work too.

Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112