7

I'm trying to do unit testing on my C# ASP.NET project but need to run some initialization code for all tests:

[TestClass()]
public class Init
{

    [AssemblyInitialize]
    public static void initialize()
    {
            ContextProvider.setContext(new TestContext());
    }
}

That's all I need to run before my tests, but it is not running. I tried to debug all my tests and put a breakpoint in that line but it is not hit. If I comment out the [AssemblyInitialize] and run one particular test that does not require this initialization, it runs successfully. However, with this line in, no tests run (and no breakpoints at all are hit)

What am I missing here?

2
  • 2
    All examples I have seen receive TestContext as an argument. Are you sure that your code is legal?
    – Phil1970
    Jan 4, 2017 at 13:54
  • TestContext is a class I have created to mock some features I need from HttpContext in a simple manner. I can comment out that line and put any other dummy code and it still does not call the method
    – YuriW
    Jan 4, 2017 at 15:00

4 Answers 4

12

Here is (I think) the full list of things to check:

  • You should be using Microsoft.VisualStudio.TestTools.UnitTesting;
  • The class must be public
  • The class CANNOT be static or abstract
  • The class must be decorated with [TestClass]
  • The method must be public
  • The method MUST be static
  • The method must be decorated with [AssemblyInitialize]
  • The method must have the signature: void InitName(TestContext tc)
2
  • Is there a reference for this in the Microsoft docs? Feb 25, 2022 at 17:39
  • I think, if such existed, an excerpt or reference to it would be "an answer" rather than this "Here is (I think)..." answer. Sadly, for the last 20+ years programmers have moved towards rarely if ever documenting anything. If one can find something, it's typically so inadequate that it borders on useless thus feeding the cycle of less and less since what does get created is so rarely referenced.
    – bielawski
    Feb 26, 2022 at 19:53
11

Phil1970's helpful comment assisted in resolving the issue.

The problem with the initialize method is that it must receive TestContext (Microsoft.VisualStudio.TestTools.UnitTesting.TestContext). I couldn't find any guide/Microsoft documentation that explicitly states this, but the examples in this msdn page do have TestContext as input for the method.

After I added it, the tests ran successfully. On a side note, I had created a class to mock some data I needed and called it TestContext, which turned out to be a very poor name selection and made it more difficult to understand my mistake. I renamed it to APITestContext, here's my fixed initialization class.

[TestClass()]
public class Init
{

    [AssemblyInitialize()]
    public static void initialize(TestContext testContext)
    {
            ContextProvider.setContext(new APITestContext());
    }
}
0

Don't know if it can be related but in the documentation it says

This attribute should not be used on ASP.NET unit tests, that is, any test with [HostType("ASP.NET")] attribute. Because of the stateless nature of IIS and ASP.NET, a method decorated with this attribute might be called more than once per test run.

Are you using that HostType?

1
  • I have not used [HostType] anywhere at least yet. In either case, being called more than once would not really be a problem.. It is not called once, the debugger closes without errors/exceptions without hitting the breakpoint in this method...
    – YuriW
    Jan 4, 2017 at 14:58
0

Special case:

"If you have [AssemblyInitialize()] or [AssemblyCleanup] defined in a base class of a unit test class...",

..then the base class also needs to be decorated with the [TestClass] attribute, even though the inheriting class already has this attribute attached.

[TestClass]
public class UnitTest : TestBase
{...

    [TestMethod]
    public void TocFileTest()
    {...
    
    

[TestClass] // <== here again!
public class TestBase
{       
    /// <summary>
    /// You have to add [TestClass] attribute also to the base class in order to have AssemblyInitialize/Cleanup executed
    /// This is only needed when you put them in a base class, otherwise not
    /// </summary>
    /// <param name="testContext"></param>
    [AssemblyInitialize()]
    public static void AssemblyInitialize(TestContext testContext)
    {...}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.