This project is read-only.
1
Vote

Attribute AssemblyInitialize is not considered

description

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
<AssemblyInitialize()>
Public Shared Sub OnLocalAssemblyInitialize(ByVal context As TestContext)
    cnt1 = 100
End Sub
<ClassInitialize()>
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

comments

staxmanade wrote May 14, 2014 at 3: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 7:45 AM

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

pgrishanov wrote May 15, 2014 at 9:22 AM

Update:
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()
    {
        base.LogWriter.GranularAssemblyTestStage...
        base.TestHarness.Events.SendTestAssemblyStarting...
        this.EnqueueAssemblyInitialize();
        this.EnqueueTestClasses();
        this.EnqueueAssemblyCleanup();
            }
    private void EnqueueAssemblyInitialize()
    {
              ....
        this.EnqueueMethodDispatcher(this._assembly.AssemblyInitializeMethod);
    }
    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 12:29 PM

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

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

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

staxmanade wrote May 15, 2014 at 3:56 PM

Good find