在动态创建的DLL上运行单元测试 [英] Run unit tests on dynamically created DLL

查看:168
本文介绍了在动态创建的DLL上运行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑如何实现这一点,似乎我的知识太差,无法理解实现。



我有一个将源代码编译为DLL的代码



然后我需要以某种方式在此Dll上运行单元测试,并检查3-4种方法。



我试图以这种方式运行单元测试。

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

装配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);

这个例如Test

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

}

由于程序集是动态编译的,我无法引用单元测试项目,它不会编译,有关如何实现的任何建议请

解决方案

您的代码工作正常经过几次修改。这些是必需的更改:




  • 测试代码需要用适当的命名空间导入包装在一个类中,您无法编译独立方法使用C#编译器

  • 您需要通知编译器您正在编译的代码的汇编引用,在这种情况下,我提供了路径到 nunit。我从主AppDomain中加载的副本的位置获得了framework.dll,而您也可以从文件系统中引用它,而不加载它。通常,您还需要提供包含您正在测试的代码的程序集的路径

  • TestPackage 类的构造函数在其构造函数中的绝对路径,在这种情况下,我提供了生成的程序集的位置。



这是具有更正的代码: / p>

  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);


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

解决方案

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);

这篇关于在动态创建的DLL上运行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆