NUnit API And Running Tests Programmatically

后端 未结 2 1094
别跟我提以往
别跟我提以往 2021-01-06 03:52

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:

  • Take a set of str
相关标签:
2条回答
  • 2021-01-06 04:26

    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!

    0 讨论(0)
  • 2021-01-06 04:40

    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?

    0 讨论(0)
提交回复
热议问题