Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vba unittest #209

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified MacmillanGT/MacmillanGT.dotm
Binary file not shown.
77 changes: 77 additions & 0 deletions MacmillanGT/TestModule1.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
Attribute VB_Name = "TestModule1"
Option Explicit

Option Private Module

' Macmillan customization:
Dim docTestfileGood As Document
Dim docTestfileBad As Document

'@TestModule
Private Assert As Object

'@ModuleInitialize
Public Sub ModuleInitialize()
'this method runs once per module.
Set Assert = CreateObject("Rubberduck.AssertClass")

End Sub

'@ModuleCleanup
Public Sub ModuleCleanup()
'this method runs once per module.
End Sub

'@TestInitialize
Public Sub TestInitialize()
'this method runs before every test in the module.
End Sub

'@TestCleanup
Public Sub TestCleanup()
'this method runs after every test in the module.
End Sub

'@TestMethod
Public Sub Test_CheckFileName()
On Error GoTo TestFail

'Arrange:
' Load test docs - they are Dim's as declarations for this module.
Set docTestfileGood = LoadTestDoc("good") 'param = 'good' or 'bad^', which test doc to load
Set docTestfileBad = LoadTestDoc("bad^") 'param = 'good' or 'bad^', which test doc to load

' Declarations for this test:
Dim boolTestGood As Boolean
Dim boolTestBad As Boolean

'Act:
' this construtction is necessary to run Private subs/functions from other modules:
' Application.Run("Module.Sub")
' Looks like if you're passing parameters its weird too:
' Application.Run "Test", Variable1, Variable2
' I had to add the parentheses to get a function returning a value to work, so will need to test
' re: functions with parameters
docTestfileGood.Activate
boolTestGood = Application.Run("Reports.CheckFileName")
docTestfileBad.Activate
boolTestBad = Application.Run("Reports.CheckFileName")

' Cleanup - delete test docs
Call DeleteTestDoc(docTestfileGood.FullName)
Call DeleteTestDoc(docTestfileBad.FullName)

'Assert:
Assert.IsFalse boolTestGood
' assert.IsTrue would have worked here too:
Assert.AreEqual boolTestBad, True


TestExit:
Exit Sub
TestFail:
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
End Sub



43 changes: 43 additions & 0 deletions MacmillanGT/UnitTestHelpers.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Attribute VB_Name = "UnitTestHelpers"
Option Explicit

Function LoadTestDoc(p_strTemplateNameSuffix As String) As Document

Dim strTimestamp As String
Dim strParentFolderPath As String
Dim strTemplatePath As String
Dim strFilePath As String
Dim strFileNameBase As String
Dim strDocxName As String
Dim docTemplate As Document

'set vars
strTimestamp = Format(Now(), "_MM-dd_hh-mm-ss")
strFileNameBase = "Test_MS_"
strDocxName = strFileNameBase & p_strTemplateNameSuffix & strTimestamp & ".docx"

' get parent folder
strParentFolderPath = CreateObject("Scripting.FileSystemObject").Getfile(ThisDocument.FullName).ParentFolder.ParentFolder.Path

' set file paths
strTemplatePath = strParentFolderPath & Application.PathSeparator & "TestDocuments" _
& Application.PathSeparator & strFileNameBase & p_strTemplateNameSuffix & ".dotx"
strFilePath = strParentFolderPath & Application.PathSeparator & "TestDocuments" _
& Application.PathSeparator & strDocxName

' open template, save as new testdoc
Set docTemplate = Documents.Open(fileName:=strTemplatePath, ReadOnly:=True)
docTemplate.SaveAs fileName:=strFilePath

'return the test document
Set LoadTestDoc = Documents(strDocxName)

End Function

Function DeleteTestDoc(p_strTestfilePath As String)

Documents(p_strTestfilePath).Close SaveChanges:=wdDoNotSaveChanges
Kill p_strTestfilePath

End Function

Binary file added TestDocuments/Test_MS_bad^.dotx
Binary file not shown.
Binary file added TestDocuments/Test_MS_good.dotx
Binary file not shown.