This is the first time I\'m trying to hack into the NUnit interface, and am facing some issues.
This is what I am trying to achieve:
Ah it works!
I was missing a couple of steps (where the fixtures were separately identified and added to the assembly) + initially a call to "InstallBuiltIns" method. It works now and this is how it looks:
//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);
where:
/// <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();
}
The BuildAssembly is same as before. The only reason I had to repeat some of the logic from NUnit was that for some reason these were private within the NUnit Codebase!
NUnit does some AppDomain creation when it runs, I wonder if somehow doing all this dynamically breaks that and it can't see your tests due to missing dependencies?