免责声明:我拥有 Rubberduck 的 GitHub 存储库,并且是参与该项目的开发人员之一。
橡皮鸭 http://www.rubberduckvba.com正在积极开发中。它是much虽然它不仅仅是 VBA 的单元测试工具,但它运行得非常好,并且让您几乎无需任何样板即可编写 VBA 单元测试:
'@TestModule
Private Assert As New Rubberduck.AssertClass
'@TestMethod
Public Sub TestMethod1()
Assert.Inconclusive "Test method is not written yet."
End Sub
'@TestMethod
Public Sub AnotherTestMethod()
Assert.IsTrue False, "Something's wrong?"
End Sub
然后,您可以在停靠的工具窗口中导航并运行测试方法,该窗口还为您提供用于快速添加的菜单安排-行动-断言方法存根,以及AssertClass
也可以是后期绑定的,因此您不必担心将 Rubberduck 部署在您的开发环境中以外的其他地方,只是为了保持代码的可编译性。
The 单元测试维基页面 https://github.com/rubberduck-vba/Rubberduck/wiki/Unit-TestingRubberduck 的 GitHub 存储库上的 几乎解释了有关使用它的所有内容。
最新的 2.1 预发布版本包括“假货”框架的开始,该框架可用于劫持许多通常会干扰单元测试的标准库调用,方法是将标准库字面意义上的转换为“测试假货”,例如,设置为在 Rubberduck 单元测试上下文中执行时按照指定的方式运行MsgBox
calls:
'@TestMethod
Public Sub TestMethod1()
On Error GoTo TestFail
Fakes.MsgBox.Returns 42 ' MsgBox function will return 42
'here you'd invoke the procedure you want to test
Debug.Print MsgBox("This MsgBox isn't going to pop up!", vbOkOnly, "Rubberduck") 'prints 42
With Fakes.MsgBox.Verify ' Test will pass if MsgBox was invoked as specified
.Parameter "prompt", "This MsgBox isn't going to pop up!"
.Parameter "buttons", vbOkOnly
.Parameter "title", "Rubberduck"
End With
TestExit:
Exit Sub
TestFail:
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
End Sub
扩大这一范围的贡献Fakes
涵盖更多功能的 API 非常受欢迎。覆盖FileSystemObject
调用将特别有用。