3

我目前正在为 MutationTesting 编写一个框架。代码几乎是完整的,但有一点(花了半天时间)我无法弄清楚:

通过反射,我想执行“TestClass”类中的方法“TestMethod”。“TestClass”所在的项目引用了一个组件,我们称之为“Proband.dll”。

“TestMethod”在 Proband.dll 中创建某种类型的对象并在该对象上执行方法。

为了澄清一点:TestClass - 是一个包含单元测试的类。TestMethod - 是一个单一的单元测试。Proband.dll - 包含要测试的方法/类。

在执行 TestMethod 之前,我已经成功地对 Proband.dll 进行了反汇编、变异和重组。所以现在我有一个新的“Proband.dll”,它将由 TestClass 代替!

问题是 TestClass 已经在执行中。我在想是否可以创建一个 AppDomain 来加载新的 Proband.dll 并在新的 AppDomain 中执行 TestMethod。

我已经创建了这个 AppDomain 并成功地将新的 Proband.dll 加载到其中,但是,我不知道如何在这个新的 AppDomain 中执行 TestMethod。另外我不知道这是否会“替换”旧的 Proband.dll 用于测试方法。

这是我的测试类:

[TestClass()]
public class TestClass
{
    [TestInitialize()]
    public void MyTestInitialize()
    {
        // code to perform the mutation.
        // From here, the "TestMethod" should be called with 
        // the new "Proband.dll".
    }

    [TestMethod()]
    public void TestMethod()
    {
        // test code calling into Proband.dll
    }
}

有谁知道如何实现这一目标?或者任何线索或关键字?

谢谢,克里斯蒂安

4

1 回答 1

1

这可能是矫枉过正,但看看我的答案:

AppDomain 中的静态字段

您需要TestClass在已加载 Proband.dll 的 AppDomain 中创建 ,并使用 aMarshalByRef将类保留在 AppDomain 中(以便稍后卸载它)。

这是关于我所做的更多信息(不准确,因为我正在对其进行更改以匹配您需要的更多内容)。

// Create Domain
_RemoteDomain = AppDomain.CreateDomain(_RemoteDomainName,
   AppDomain.CurrentDomain.Evidence, 
   AppDomain.CurrentDomain.BaseDirectory, 
   AppDomain.CurrentDomain.BaseDirectory, 
   true);

// Load an Assembly Loader in the domain (mine was Builder)
// This loads the Builder which is in the current domain into the remote domain
_Builder = (Builder)_RemoteDomain.CreateInstanceAndUnwrap(
    Assembly.GetExecutingAssembly().FullName, "<namespace>.Builder");

_Builder.Execute(pathToTestDll)


public class Builder : MarshalByRefObject 
{ 
    public void Execute(string pathToTestDll)
    {
        // I used this so the DLL could be deleted while
        // the domain was still using the version that
        // exists at this moment.
        Assembly newAssembly = Assembly.Load(
           System.IO.File.ReadAllBytes(pathToTestDll));

        Type testClass = newAssembly.GetType("<namespace>.TestClass", 
           false, true);

        if (testClass != null)
        {
           // Here is where you use reflection and/or an interface
           // to execute your method(s).
        }
    }
}

这应该为您提供所需的解决方案。

于 2011-07-08T19:37:09.217 回答