Attribute AssemblyInitialize is not considered


Method marked with AssemblyInitialize is not called by Statlight
In order to reproduce issue create following test calss
Imports Microsoft.VisualStudio.TestTools.UnitTesting
<TestClass()> _
Public Class HugoTest
Private Shared cnt1 As Integer = 0
Private Shared cnt2 As Integer = 0
Private Shared cnt3 As Integer = 0
Public Shared Sub OnLocalAssemblyInitialize(ByVal context As TestContext)
    cnt1 = 100
End Sub
Public Sub InitClass()
    cnt2 = 200
End Sub
<TestInitialize()> _
Public Sub MyTestInitialize()
    cnt3 = 300
End Sub
<TestMethod()> _
Public Sub test1()
    ' Asser failed
    Assert.IsTrue(cnt1 = 100)
End Sub
<TestMethod()> _
Public Sub test2()
    Assert.IsTrue(cnt2 = 200)
End Sub
<TestMethod()> _
Public Sub test3()
    Assert.IsTrue(cnt3 = 300)
End Sub
End Class

Execution results would be:
test1 - failed
test2 - OK
test3 - OK


staxmanade wrote May 14, 2014 at 2:53 AM

StatLight supports the AssemblyInitialize attribute. Try taking the Shared off as it may not be looking for static members.

pgrishanov wrote May 14, 2014 at 6:45 AM

Even without Shared it does not work. I'm using SL5.

pgrishanov wrote May 15, 2014 at 8:22 AM

Statlight detect and provide to the SL testing framework correct information about AssemblyInitializeMethod
The problem is in the SL testing framework itself: It's a bug there: in the AssemblyManager.FirstInvoke
    protected override void FirstInvoke()
    private void EnqueueAssemblyInitialize()
    private void EnqueueMethodDispatcher(MethodInfo method)
        object instance = this.ClassInstances.GetInstance(method.ReflectedType);
        if (instance == null)
            throw new InvalidOperationException....
            ClassInstances is still empty because it is filled up in method EnqueueTestClasses that is called after EnqueueAssemblyInitialize...

pgrishanov wrote May 15, 2014 at 11:29 AM

The problem is the signature of the method. Usage in .NET
Public Shared Sub OnLocalAssemblyInitialize(ByVal context As TestContext)

Here how it must be defined in SL:
Public Shared Sub OnLocalAssemblyInitialize()

Method must be without parameters, otherwisse it UT engine failed to invoke the method

staxmanade wrote May 15, 2014 at 2:56 PM

Good find