Run unit tests on dynamically created DLL

≯℡__Kan透↙ 提交于 2019-12-24 16:32:00

问题


I am thinking on how to implement this , and seems like my knowledge is too poor to understand the implementation.

I have a code that compiles source code to DLL.

Then I need somehow to run Unit test on this Dll , and check 3-4 methods inside.

I am trying to run unit tests this way.

CompilerResults compileResults = codeProvider.CompileAssemblyFromSource(compilerParameters, new string[] { sourceCode });

            Assembly myAssembly = compileResults.CompiledAssembly;

            TestPackage testPackage = new TestPackage(@"TestingProject.dll");
            testPackage.Assemblies.Add(myAssembly);

            RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
            remoteTestRunner.Load(testPackage);
            TestResult testResult = remoteTestRunner.Run(new NullListener(),TestFilter.Empty,false,LoggingThreshold.All);

And this for example Test

   [Test]
    public void AddTest(IDynamicScript s)
    {            
        Assert.AreEqual(10, s.Add(5,5));
        Assert.AreNotEqual(10,s.Add(4,5));

    }

Since Assembly is compiled dynamically , I can't reference it to unit test Project , and it will not compile , any suggestions please on how to implement this please


回答1:


Your code works just fine after a few modifications. These are the required changes:

  • The test code needs to be wrapped in a class with appropriate namespace imports, you can't compile standalone methods with the C# compiler
  • You need to inform the compiler about the assembly references of the code you're compiling, in this case I supplied the path to the nunit.framework.dll assembly, which I got from the location of the copy loaded in the main AppDomain, but you can also reference it from the file system without loading it. Usually you will also need to supply the paths of the assemblies containing the code you're testing
  • The constructor of the TestPackage class takes an absolute path in its constructor, in this case I supplied the location of the generated assembly

This is the code with the corrections:

var sourceCode = @"
using NUnit.Framework;

public class Fixture
{
    [Test]
    public void AddTest()
    {            
        Assert.AreEqual(10, 5+5);
    }
}";

var provider = new CSharpCodeProvider();

CompilerResults compileResults = provider.CompileAssemblyFromSource(
    new CompilerParameters(new[]{ typeof(TestAttribute).Assembly.Location }), 
    new[]{ sourceCode });

var assembly = compileResults.CompiledAssembly;
var package = new TestPackage(assembly.Location);
var runner = new RemoteTestRunner();

runner.Load(package);

var result = runner.Run(new NullListener(), 
                        TestFilter.Empty, 
                        false, 
                        LoggingThreshold.All);


来源:https://stackoverflow.com/questions/13145030/run-unit-tests-on-dynamically-created-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!