这是我第一次尝试入侵NUnit界面,并且遇到了一些问题。
这就是我想要实现的目标:
这是我到目前为止所拥有的
/// <summary> Validates whether the code files are valid </summary>
public string Validate(string[] codefiles)
{
// To avoid NullReferenceException within NUnit
// don't know why this is needed!
TestExecutionContext.CurrentContext.TestPackage
= new TestPackage("PersonTest");
var assembly = BuildAssembly(codefiles);
TestSuite suite = new TestAssembly(assembly, "PersonTest");
var result = suite.Run(new NullListener(), TestFilter.Empty);
return result.IsSuccess ? string.Empty : result.Message;
}
/// <summary> Builds an assembly </summary>
private static Assembly BuildAssembly(string[] code)
{
var compilerparams = new CompilerParameters(new[] {"nunit.framework.dll"})
{
GenerateExecutable = false,
GenerateInMemory = true
};
var results = new CSharpCodeProvider()
.CompileAssemblyFromSource(compilerparams, code);
if (!results.Errors.HasErrors) return results.CompiledAssembly;
throw new Exception(GetErrors(results));
}
这是我试图编译的两个字符串(通过将带有两个元素的字符串数组发送到上面的validate方法中)
private const string File1 = @"
public class Person
{
public string Name {get;set;}
public Person(string name)
{
Name = name;
}
}";
private const string ProperInput = @"
using NUnit.Framework;
[TestFixture]
public class PersonTest
{
[Test]
public void CheckConstructor()
{
var me = new Person(""Roopesh Shenoy"");
Assert.AreEqual(""Roopesh Shenoy"", me.Name);
}
}";
问题:
编译很好 - 生成的程序集甚至有这两种类型。也没有将nunit.framework.dll传递给引用的程序集会产生编译异常(动态编译时),因此这意味着TestFixture / Test addributes也被识别。
但是,当我调试TestAssembly时,只显示测试计数为零。因此,它没有显示测试成功或失败,而是显示不确定,基本上没有做任何事情。
我尝试了NUnit代码库,但还没有得出任何好的结论 - 他们大多数方便的方法都希望程序集在文件系统上,所以我试图弄清楚如何做到这一点。有什么想法吗?
答案 0 :(得分:2)
NUnit在运行时会创建一些AppDomain,我想知道是否会以某种方式动态地破坏它并且由于缺少依赖性而无法看到您的测试?
答案 1 :(得分:1)
啊它有效!
我错过了几个步骤(固定装置被单独识别并添加到装配中)+最初调用“InstallBuiltIns”方法。它现在有效,这就是它的外观:
//this is needed only once
CoreExtensions.Host.InstallBuiltins();
var assembly = BuildAssembly(codefiles.ToArray());
// to avoid NullReferenceException - don't know why this is needed!
TestExecutionContext.CurrentContext.TestPackage
= new TestPackage(assembly.GetName().FullName);
var suite = GetTestSuiteFromAssembly(assembly);
return suite.Run(new NullListener(), TestFilter.Empty);
其中:
/// <summary>
/// Converts a given assembly containing tests to a runnable TestSuite
/// </summary>
protected static TestSuite GetTestSuiteFromAssembly(Assembly assembly)
{
var treeBuilder = new NamespaceTreeBuilder(
new TestAssembly(assembly, assembly.GetName().FullName));
treeBuilder.Add(GetFixtures(assembly));
return treeBuilder.RootSuite;
}
/// <summary>
/// Creates a tree of fixtures and containing TestCases from the given assembly
/// </summary>
protected static IList GetFixtures(Assembly assembly)
{
return assembly.GetTypes()
.Where(TestFixtureBuilder.CanBuildFrom)
.Select(TestFixtureBuilder.BuildFrom).ToList();
}
BuildAssembly与以前相同。我必须从NUnit重复一些逻辑的唯一原因是,由于某种原因,这些逻辑在NUnit代码库中是私有的!