Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey guys!

I have to develop a simple Inhouse-Testframework.
For this task we have some test files (every file has one class and 1 to n test methods) in a given directory and we wont to execute all of them.

So i have a solution with 2 projects:
1: Console Application (TestRunner)
2: Class Library

The console application has a reference to the class library.
In the class library we have a Assert class and a method attribute ( SamwinTest )

In the following method i iterate over all files, compile them to a dll and want to execute them.

VB
Private Sub RunTestFile(ByVal testFile As System.IO.FileInfo)
       If Not System.IO.File.Exists(testFile.FullName) Then
           ' file not found, log exception
       Else
           Try
               Dim compilerParams As New System.CodeDom.Compiler.CompilerParameters()

               ' set compiler parameters
               compilerParams.OutputAssembly = System.IO.Path.Combine(App.Default.OutputFolder, String.Format("{0}.dll", testFile.Name))
               Dim asm As System.Reflection.Assembly
               For Each asm In AppDomain.CurrentDomain.GetAssemblies()
                   compilerParams.ReferencedAssemblies.Add(asm.Location)
               Next
               compilerParams.GenerateInMemory = False
               compilerParams.GenerateExecutable = False

               ' compile the given test file and catch compiler result
               Dim compilerResult As System.CodeDom.Compiler.CompilerResults = _vbProvider.CompileAssemblyFromFile(compilerParams, {testFile.FullName})

               Dim errorOccured As Boolean = False

               ' check compiler result
               If compilerResult.Errors.Count > 0 Then
                   errorOccured = True
                   Console.ForegroundColor = ConsoleColor.Red
                   Console.WriteLine("compilation failed")
                   If App.Default.PrintErrorsInConsole Then
                       Console.WriteLine(Common.FormatError(compilerResult.Errors))
                   End If
               Else
                   Console.ForegroundColor = ConsoleColor.Green
                   Console.WriteLine("compilation passed")
               End If
               Console.ForegroundColor = ConsoleColor.White

               ' if no error has occured invoke all test methods
               If Not errorOccured Then
                   Dim testCase As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom(compilerParams.OutputAssembly)

                   ' get all types
                   For Each t In testCase.GetTypes()
                       Try
                           If t.Name.StartsWith("UnitTest") Then
                               Dim bindingFlag As System.Reflection.BindingFlags = System.Reflection.BindingFlags.DeclaredOnly Or
                                                                                   System.Reflection.BindingFlags.Public Or
                                                                                   System.Reflection.BindingFlags.NonPublic Or
                                                                                   System.Reflection.BindingFlags.Instance

                               Dim obj As Object = t.InvokeMember(t.Name, bindingFlag Or Reflection.BindingFlags.CreateInstance, Nothing, Nothing, Nothing)

                               ' get all methods of the given type
                               For Each method In t.GetMethods()
                                   ' here we want to test all methods with the method-attribute
                               Next

                           End If
                       Catch ex As Exception
                           Log(ex.ToString(), LogType.Err)
                       End Try
                   Next
               End If

           Catch ex As Exception
               Log(ex.ToString(), LogType.Err)
           End Try

       End If
   End Sub


Problem: it dont references the class library.
- Error message: "Type 'SamwinTest' is not defined
- Error message: "'Assert' is not declared. It may be inaccessible due its protection level

With the following code i thought that i add every assembly wich the console app refers.
VB
Dim asm As System.Reflection.Assembly
               For Each asm In AppDomain.CurrentDomain.GetAssemblies()
                   compilerParams.ReferencedAssemblies.Add(asm.Location)
               Next

But i cant find the "Samwin.UnitTest.dll"

Here is just a simple and silly test class (test file)
VB
Public Class Test
	
	<SamwinTest()>
    Public Sub HelloWorld()
        Dim x As Integer
        Dim y As Integer
        Dim z As Integer
        x = 1
        y = 1
        Assert.AreEqual(x, y)
        z = x + y
    End Sub

End Class


Anybody???
Thanks in adv
Cheers James
Posted
Comments
Sergey Alexandrovich Kryukov 17-Apr-12 12:56pm    
I voted 4 for this question. OP's idea of getting referenced assembly it good, but the problem could be easily resolved, which could be easier than giving my advice... :-)
--SA

You are using pretty wise method of getting assembly location without messing with file systems. You should run this code under debugger and see what's wrong with "Samwin.UnitTest.dll" and compare with its real location, but I can suggest that the problem is simple: this assembly is not included in the collection AppDomain.CurrentDomain.GetAssemblies(); and it is not included, because you might not have this assembly referenced by your host application. The problem is very simple; just make sure you run the code under debugger.

Actually, you should run your code under debugger and pin-point the problem more precisely before asking questions like this one. And you should run your code under debugger in case of even the slightest concerns about your run-time behavior.

—SA
 
Share this answer
 
Comments
Mehdi Gholam 17-Apr-12 12:36pm    
My 5!
Sergey Alexandrovich Kryukov 17-Apr-12 12:54pm    
Thank you, Mehdi.
--SA
VJ Reddy 17-Apr-12 12:52pm    
Good answer and advice. +5
Sergey Alexandrovich Kryukov 17-Apr-12 12:54pm    
Thank you, VJ.
--SA
Christopher James Kleinheinz 18-Apr-12 2:40am    
Of course i run the code under debugger ;). And yes, the Samwin.UnitTest.dll is not included within AppDomain.CurrentDomain.GetAssemblies(). My host application has a reference to Samwin.UnitTest.dll.

Maybe another idea?
Little Hint: LATE BINDING

Because in my TestRunner console application I never used the Samwin.UnitTest library, it's just needed in the test files. So it wasn't referenced in my application.
I just created one instance of the Assert-Class in my console application and TAADAA it's working.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900