You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For details of the process of reaching this example, see the [TDD Example](https://github.com/VBA-tools/VBA-TDD/wiki/TDD-Example)
50
47
51
-
###Advanced Example
48
+
## Advanced Example
52
49
53
-
For an advanced example of what is possible with VBA-TDD, check out the [specs for VBA-Web](https://github.com/VBA-tools/VBA-Web/tree/master/specs)
50
+
For an advanced example of what is possible with vba-test, check out the [tests for VBA-Web](https://github.com/VBA-tools/VBA-Web/tree/master/specs)
54
51
55
-
###Getting Started
52
+
## Getting Started
56
53
57
-
1. Download the [latest release (v2.0.0-beta)](https://github.com/VBA-tools/VBA-TDD/releases)
58
-
2. Add `src/SpecSuite.cls`, `src/SpecDefinition.cls`, `src/SpecExpectation.cls`, add `src/ImmediateReporter.cls` to your project
59
-
3. If you're starting from scratch with Excel, you can use `VBA-TDD - Blank.xlsm`
54
+
1. Download the [latest release (v2.0.0-beta.2)](https://github.com/vba-tools/vba-test/releases)
55
+
2. Add `src/TestSuite.cls`, `src/TestCase.cls`, add `src/ImmediateReporter.cls` to your project
56
+
3. If you're starting from scratch with Excel, you can use `vba-test-blank.xlsm`
60
57
61
-
### It and Expect
58
+
##TestSuite
62
59
63
-
`It` is how you describe desired behavior and once a collection of specs is written, it should read like a list of requirements.
60
+
A test suite groups tests together, runs test hooks for actions that should be run before and after tests, and is responsible for passing test results to reporters.
64
61
65
62
```vb
66
-
WithSpecs.It("should allow user to continue if they are authorized and up-to-date")
67
-
' ...
68
-
EndWith
69
-
70
-
WithSpecs.It("should show an X when the user rolls a strike")
71
-
' ...
63
+
' Create a new test suite
64
+
DimSuiteAsNewTestSuite
65
+
Suite.Description="Module Name"
66
+
67
+
' Create a new test
68
+
DimTestAsTestCase
69
+
SetTest=Suite.Test("Test Name")
70
+
Test.IsEqual' ...
71
+
72
+
' or create and use test using With
73
+
WithSuite.Test("Test Name")
74
+
.IsEqual'...
72
75
EndWith
73
76
```
74
77
75
-
`Expect` is how you test desired behavior
78
+
__TestSuite API__
79
+
80
+
-`Description`
81
+
-`Test(Name) As TestCase`
82
+
-_Event_`BeforeEach(Test)`
83
+
-_Event_`Result(Test)`
84
+
-_Event_`AfterEach(Test)`
85
+
86
+
## TestCase
87
+
88
+
A test case uses assertions to test a specific part of your application.
76
89
77
90
```vb
78
-
WithSpecs.It("should check values")
79
-
.Expect(2+2).ToEqual4
80
-
.Expect(2+2).ToNotEqual5
81
-
.Expect(2+2).ToBeLessThan7
82
-
.Expect(2+2).ToBeLT6
83
-
.Expect(2+2).ToBeLessThanOrEqualTo5
84
-
.Expect(2+2).ToBeLTE4
85
-
.Expect(2+2).ToBeGreaterThan1
86
-
.Expect(2+2).ToBeGT2
87
-
.Expect(2+2).ToBeGreaterThanOrEqualTo3
88
-
.Expect(2+2).ToBeGTE4
89
-
.Expect(2+2).ToBeCloseTo3.9,0
91
+
WithSuite.Test("specific part of your application")
92
+
.IsEqualA,B,"(optional message, e.g. result should be 12)"
93
+
.NotEqualB,C
94
+
95
+
.IsOkC>B
96
+
.NotOkB>C
97
+
98
+
.IsUndefined' Checks Nothing, Empty, Missing, or Null
99
+
.NotUndefined
100
+
101
+
.IncludesArray(1,2,3),2
102
+
.NotIncludesArray(1,2,3),4
103
+
.IsApproximate1.001,1.002,2
104
+
.NotApproximate1.001,1.009,3
105
+
106
+
.Pass
107
+
.Fail"e.g. should not have gotten here"
108
+
.Plan4' Should only be 4 assertions, more or less fails
109
+
.Skip' skip this test
90
110
EndWith
91
111
92
-
WithSpecs.It("should check Nothing, Empty, Missing, and Null")
93
-
.Expect(Nothing).ToBeNothing
94
-
.Expect(Empty).ToBeEmpty
95
-
.Expect().ToBeMissing
96
-
.Expect(Null).ToBeNull
97
-
98
-
' `ToBeUndefined` checks if it's Nothing or Empty or Missing or Null
99
-
100
-
.Expect(Nothing).ToBeUndefined
101
-
.Expect(Empty).ToBeUndefined
102
-
.Expect().ToBeUndefined
103
-
.Expect(Null).ToBeUndefined
104
-
105
-
' Classes are undefined until they are instantiated
-`Test.Fail([Message])` - Explicitly fail the test
155
+
-`Test.Plan(Count)` - For tests with loops and branches, it is important to catch if any assertions are skipped or extra
156
+
-`Test.Skip()` - Notify suite to skip this test
157
+
158
+
Generally, more advanced assertions should be added with custom assertions functions (detailed above), but there are common assertions that will be added (e.g. `IsApproximate` = close within significant fixtures, `Includes` = array/collection includes value, )
123
159
124
-
With your specs defined, the easiest way to display the test results is with `ImmediateReporter`. This outputs results to the Immediate Window (`ctrl+g` or View > Immediate Window) and is useful for running your tests without leaving the VBA editor.
160
+
## ImmediateReporter
161
+
162
+
With your tests defined, the easiest way to display the test results is with `ImmediateReporter`. This outputs results to the Immediate Window (`ctrl+g` or View > Immediate Window) and is useful for running your tests without leaving the VBA editor.
125
163
126
164
```vb
127
-
PublicFunctionSpecsAsSpecSuite
128
-
SetSpecs=NewSpecSuite
129
-
Specs.Description="..."
165
+
PublicFunctionSuiteAsTestSuite
166
+
SetSuite=NewTestSuite
167
+
Suite.Description="..."
130
168
131
-
' Create reporter and attach it to these specs
132
-
DimReporterAsNewImmediateReporter
133
-
Reporter.ListenToSpecs
169
+
' Create reporter and attach it to these specs
170
+
DimReporterAsNewImmediateReporter
171
+
Reporter.ListenToSuite
134
172
135
-
' -> Reporter will now output results as they are generated
173
+
' -> Reporter will now output results as they are generated
136
174
EndFunction
137
175
```
138
176
139
-
### RunMatcher
177
+
##Context / Lifecycle Hooks
140
178
141
-
For VBA applications that support `Application.Run` (which is at least Windows Excel, Word, and Access), you can create custom expect functions with `RunMatcher`.
179
+
`TestSuite` includes events for setup and teardown before tests and a `Context` object for passing values into tests that are properly torn down between tests.
142
180
143
181
```vb
144
-
PublicFunctionSpecsAsSpecSuite
145
-
SetSpecs=NewSpecSuite
146
-
147
-
WithSpecs.It("should be within 1 and 100")
148
-
.Expect(50).RunMatcher"ToBeWithin","to be within",1,100
0 commit comments