jeudi 2 juin 2016

Why 3rd-party DLL need pre-load in unit test

We have a 3rd party DLL(lib1.dll), it's Win32 .NET assembly. Meanwhile, we wrote a class library(lib2.dll) which reference lib1.dll.

After that, we wrote unit test(via NUnit) to test the lib2, and the exception happens:

Could not load file or assembly or one of its dependencies.

lib1:
using System;
using System.Diagnostics;

public class Helper
{
    public Helper()
    {
    }

    public static void Execute()
    {
        Trace.WriteLine(DateTime.Now);
    }
}

lib2:
namespace lib2
{
    public class Manager
    {
        public int Add(int a, int b)
        {
            Helper.Execute();
            return a + b;
        }
    }
}

NUnit test:
[Test]
public void Foo()
{
    var manager = new Manager();
    Assert.AreEqual(5, manager.Add(2,3));
}

Our code is same behavior like above. When the test executes, the Helper.Execute() would throw an exception said it could not load assembly lib1.DLL.

The strangest thing is we cannot reproduce this issue. However, we can workaround this by add a line pre-load code like this:

[Test]
public void Foo()
{
    var pre_load = new Helper();
    var manager = new Manager();
    Assert.AreEqual(5, manager.Add(2,3));
}

But we still don't know the root cause. Does anyone met this issue before or can anyone give me some hints how to do diagnostics?

Aucun commentaire:

Enregistrer un commentaire