NUnit API and Software Testing

This is the first time I'm trying to hack into the NUnit interface and run into some problems.

This is what I am trying to achieve:

  • Take a set of string inputs as code files and compile them into an assembly
  • take the assembly in memory and pass it to NUnit to identify and run the tests in this code

This is what I still have

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

These are two strings that I am trying to compile (sending as a string array with two elements to the verification method above)

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

Problem:

Compilation is excellent — the build even has these two types. In addition, not skipping nunit.framework.dll in referenced assemblies gives a compilation exception (during dynamic compilation), therefore this means that TestFixture / Test attributes are also recognized.

However, when I debug TestAssembly, it just shows the number of tests as zero. Therefore, instead of showing the success of the test or failure, it shows unconvincing and basically just does nothing.

I tried using the NUnit codebase but have not yet reached good output. Most of their handy methods expect the build to be on the file system, so I'm trying to figure out how to do this. Any ideas?

+4
source share
2 answers

NUnit creates some kind of AppDomain application at startup, I wonder how somehow it all dynamically breaks and cannot see your tests due to the lack of dependencies?

+2
source

And it works!

I was missing a couple of steps (where the lights were separately identified and added to the assembly) + first, calling the "InstallBuiltIns" method. Now it works, and here is what it looks like:

 //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(); } 

BuildAssembly is similar to the previous one. The only reason I had to repeat some logic from NUnit was because for some reason they were private in NUnit Codebase!

+1
source

Source: https://habr.com/ru/post/1443793/


All Articles