3

現在、MutationTesting のフレームワークを作成しています。コードはほぼ完成していますが、(半日かけて)理解できない部分があります。

リフレクションを介して、クラス「TestClass」内にあるメソッド「TestMethod」を実行したいと思います。「TestClass」が存在するプロジェクトは、essembly を参照し、「Proband.dll」と呼びましょう。

「TestMethod」は、Proband.dll 内に何らかのタイプのオブジェクトを作成し、そのオブジェクトでメソッドを実行します。

少し明確にするために: TestClass - 単体テストを含むクラスです。TestMethod - 単一の単体テストです。Proband.dll - テストするメソッド/クラスが含まれています。

TestMethod を実行する前に、Proband.dll の逆アセンブル、変更、および再アセンブルに成功しました。これで、代わりに TestClass によって取得される新しい "Proband.dll" ができました。

問題は、TestClass が既に実行中であることです。新しい Proband.dll がロードされ、新しい AppDomain 内で TestMethod が実行される AppDomain を作成できるかどうかを考えていました。

既にこの AppDomain を作成し、新しい Proband.dll を正常にロードしましたが、この新しい AppDomain 内で TestMethod を実行する方法がわかりません。また、これがテスト メソッドの古い Proband.dll を「置き換える」かどうかもわかりません。

ここに私のTestClassがあります:

[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 の静的フィールド

TestClassProband.dll をロードした AppDomain にを作成し、 を使用MarshalByRefして 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 に答える