Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# AssemblyInitialize not running

Tags:

c#

asp.net

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?

like image 816
YuriW Avatar asked Jan 04 '17 13:01

YuriW


1 Answers

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());
    }
}
like image 120
YuriW Avatar answered Oct 11 '22 16:10

YuriW