Bello TestDriven.NET però manca una funzionalità: creare un progetto di
test per il progetto corrente...
A questo proposito da un po' di tempo mi
sono fatto una bella Macro...
Perché non l'ho mai
postata? Beh, é un po' empirica, funziona solo per VB.NET e crea il progetto di
test solo per il primo progetto della lista... E' da un bel po' che mi propongo
di migliorarla e farne un add-in, ma alla fine non ho mai tempo, così...
prendela per quello che é... e modificatela...
Cosa fa:
Prende
il primo progetto della vostra soluzione, crea una sottocartella che si chiama
come la costante TestClassName (oppure vi avverte che la cartella esiste già ed
esce), crea un file .vbproj e un .vb. Il .vbproj include i riferimenti e imports
a nunit.framework ed al progetto di partenza, la soluzione viene configurata per
partire dal progetto di test avviando la GUI di NUnit (SOLO IN DEBUG). E' MOLTO
empirica e "spannometrica", diciamo che andrebbe considerata come la base di
qualcos'altro che farò...
Visto che nessuno
leggerà fino in fondo a questo post saluto qui tutti i presenti al workshop di
ieri, Faccio i miei migliori complimenti a tutti i relatori ed in modo
particolare ad Andrea Saltarello che é anche stato organizzatore!
Imports EnvDTE
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.IO
Public Module NUnitModule
Private Const TestProjectName As String = "Test"
Private Const NUnitExePath As String =
"c:\Programmi\NUnit V2.1\bin\nunit-gui.exe"
Private Const TestClassName As String = "Test"
Private Const NUnitFrameworkPath As String = "c:\Programmi\NUnit V2.1\bin\nunit.framework.dll"
Private WithEvents SolEvents As EnvDTE.ProjectItemsEvents =
DTE.Events.SolutionItemsEvents
Sub
CreateTestProject()
Dim Projs As System.Array =
DTE.ActiveSolutionProjects()
Dim Project
As Project = DirectCast(Projs.GetValue(0), Project)
Dim ProjectFI
As New
System.IO.FileInfo(Project.FullName)
Dim ProjectFolder
As System.IO.DirectoryInfo = ProjectFI.Directory
Dim TestProjectFolder
As New
System.IO.DirectoryInfo(Barra(ProjectFolder.FullName) & TestProjectName)
Dim TestProjectFI
As New
System.IO.FileInfo(Project.FullName)
If
TestProjectFolder.Exists Then
MessageBox.Show("La cartella di destinazione esiste già")
Else
TestProjectFolder.Create()
Dim TestProject
As Project =
CreateTestProjectFiles(Project, TestProjectFolder.FullName, TestProjectName)
TestProject.ProjectItems.AddFromFile(CreateTestClass(TestProjectFolder.FullName,
TestClassName))
Dim SolutionName
As String =
Path.GetFileNameWithoutExtension(DTE.Solution.FullName)
DTE.ActiveWindow.Object.GetItem(SolutionName & "" &
TestProjectName).Select(vsUISelectionType.vsUISelectionTypeSelect)
DTE.ExecuteCommand("Project.SetasStartUpProject")
End If
End Sub
Private Function
CreateTestProjectFiles(ByVal BaseProject As
Project, ByVal
TestProjectFolder As
String, ByVal TestProjectName As String) As Project
TestProjectFolder = Barra(TestProjectFolder)
Dim TestProjFileName
As String = TestProjectFolder &
TestProjectName & ".vbproj"
'Creazione file vbproj
Dim ProjFS As New FileStream(TestProjFileName, FileMode.Create,
FileAccess.Write)
Dim XW As New System.Xml.XmlTextWriter(ProjFS,
System.Text.Encoding.UTF8)
Try
XW.WriteStartElement("VisualStudioProject")
XW.WriteStartElement("VisualBasic")
XW.WriteStartElement("Build")
XW.WriteStartElement("Settings")
XW.WriteAttributeString("OutputType", "Library")
XW.WriteAttributeString("StartupObject", String.Empty)
XW.WriteStartElement("Config")
XW.WriteAttributeString("Name", "Debug")
XW.WriteAttributeString("DebugSymbols", "true")
XW.WriteAttributeString("DefineDebug", "true")
XW.WriteAttributeString("DefineTrace", "true")
XW.WriteAttributeString("IncrementalBuild", "true")
XW.WriteAttributeString("OutputPath", "bin")
XW.WriteAttributeString("StartAction", "Program")
XW.WriteAttributeString("StartArguments", TestProjectName & ".dll")
XW.WriteAttributeString("StartPage", "")
XW.WriteAttributeString("StartProgram", NUnitExePath)
XW.WriteAttributeString("StartURL", "")
XW.WriteAttributeString("StartWorkingDirectory", TestProjectFolder &
"bin")
XW.WriteEndElement() 'Config
XW.WriteStartElement("Config")
XW.WriteAttributeString("Name", "Release")
XW.WriteAttributeString("DebugSymbols", "false")
XW.WriteAttributeString("DefineDebug", "false")
XW.WriteAttributeString("DefineTrace", "true")
XW.WriteAttributeString("IncrementalBuild", "false")
XW.WriteAttributeString("Optimize", "true")
XW.WriteAttributeString("OutputPath", "bin")
XW.WriteEndElement() 'Config
XW.WriteEndElement() 'Settings
XW.WriteStartElement("References")
XW.WriteStartElement("Reference")
XW.WriteAttributeString("Name", "System")
XW.WriteEndElement() 'Reference
XW.WriteStartElement("Reference")
XW.WriteAttributeString("Name", "System.Data")
XW.WriteEndElement() 'Reference
XW.WriteStartElement("Reference")
XW.WriteAttributeString("Name", "System.XML")
XW.WriteEndElement() 'Reference
XW.WriteStartElement("Reference")
XW.WriteAttributeString("Name", "nunit.framework")
XW.WriteAttributeString("AssemblyName", "nunit.framework")
XW.WriteAttributeString("HintPath", NUnitFrameworkPath)
XW.WriteEndElement() 'Reference
XW.WriteStartElement("Reference")
XW.WriteAttributeString("Name", BaseProject.Name)
MessageBox.Show(BaseProject.UniqueName)
Dim FI As New System.IO.FileInfo(BaseProject.FullName)
XW.WriteAttributeString("HintPath", Barra(Barra(FI.DirectoryName) &
"bin") & BaseProject.Name & ".dll")
XW.WriteEndElement() 'Reference
XW.WriteEndElement() 'References
XW.WriteStartElement("Imports")
XW.WriteStartElement("Import")
XW.WriteAttributeString("Namespace", "Microsoft.VisualBasic")
XW.WriteEndElement() 'Import
XW.WriteStartElement("Import")
XW.WriteAttributeString("Namespace", "System")
XW.WriteEndElement() 'Import
XW.WriteStartElement("Import")
XW.WriteAttributeString("Namespace", "System.Collections")
XW.WriteEndElement() 'Import
XW.WriteStartElement("Import")
XW.WriteAttributeString("Namespace", "System.Data")
XW.WriteEndElement() 'Import
XW.WriteStartElement("Import")
XW.WriteAttributeString("Namespace", "System.Diagnostics")
XW.WriteEndElement() 'Import
XW.WriteStartElement("Import")
XW.WriteAttributeString("Namespace", "nunit.framework")
XW.WriteEndElement() 'Import
XW.WriteStartElement("Import")
XW.WriteAttributeString("Namespace", BaseProject.Name)
XW.WriteEndElement() 'Import
XW.WriteEndElement() 'Imports
XW.WriteEndElement() 'Build
XW.WriteStartElement("Files")
XW.WriteStartElement("Include")
XW.WriteEndElement() 'Include
XW.WriteEndElement() 'Files
XW.WriteEndElement() 'VisualBasic
XW.WriteEndElement() 'VisualStudioProject
Finally
XW.Flush()
ProjFS.Flush()
ProjFS.Close()
End Try
CreateTestProjectFiles =
DTE.Solution.AddFromFile(TestProjFileName)
End Function
Private Function CreateTestClass(ByVal TestProjectFolder As String, ByVal TestClassName As String)
TestProjectFolder =
Barra(TestProjectFolder) & TestClassName &
".vb"
Dim FS As New FileStream(TestProjectFolder, FileMode.Create,
FileAccess.Write)
Try
Dim SW As New StreamWriter(FS)
Try
SW.WriteLine("<TestFixture()> _")
SW.WriteLine("Public Class " & TestClassName)
SW.WriteLine("#Region ""Setup e TearDown""")
SW.WriteLine(" <TestFixtureSetUp()> _")
SW.WriteLine(" Public Sub Setup()")
SW.WriteLine()
SW.WriteLine(" End Sub")
SW.WriteLine()
SW.WriteLine(" <TestFixtureTearDown()> _")
SW.WriteLine(" Public Sub TearDown()")
SW.WriteLine()
SW.WriteLine(" End Sub")
SW.WriteLine("#End Region")
SW.WriteLine()
SW.WriteLine(" <Test()> _")
SW.WriteLine(" Public Sub Test()")
SW.WriteLine()
SW.WriteLine(" End Sub")
SW.WriteLine("End Class")
Finally
SW.Flush()
End Try
Finally
FS.Flush()
FS.Close()
End Try
Return
TestProjectFolder
End Function
Private Function Barra(ByVal Path As String) As String
If Path = String.Empty OrElse
Path.EndsWith(System.IO.Path.DirectorySeparatorChar) Then
Return Path
Else
Return Path &
System.IO.Path.DirectorySeparatorChar
End If
End Function
End Module