Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET compiler Runtime
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.
 
 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.
 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)
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 17-Apr-12 5:48am
Comments
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Mehdi Gholam at 17-Apr-12 12:36pm
   
My 5!
SAKryukov at 17-Apr-12 12:54pm
   
Thank you, Mehdi.
--SA
VJ Reddy at 17-Apr-12 12:52pm
   
Good answer and advice. +5
SAKryukov at 17-Apr-12 12:54pm
   
Thank you, VJ.
--SA
Christopher James Kleinheinz at 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?
SAKryukov at 19-Apr-12 0:05am
   
Do you need one more idea? Either reference this assembly by the host application, so it would make it included in GetAssemblies (again, your method of listing assemblies is very nice), or add it assembly explicitly by its exact file name (its main executable module file name). As the problem is pin-pointed, since this moment the solution is trivial. Bound late? Add it later? I don't see any problem.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 18 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100