WSH脚本单元测试框架

4

我正在寻找适用于WSH脚本(vbs/wsf,不包括VB6、VBA)的单元测试框架。

除了这个项目(看起来不错,但是最近一次活动记录是大约两年前,我还没有测试过),我找不到其他任何东西:

http://code.google.com/p/vbslib/

谢谢!

2个回答

3

还有一个名为ScriptUnit的工具。如果你在Google上搜索“vbscript unittest”,你会找到我曾经发布过的一篇古老的帖子(并不太成功)。我仍然对这个话题感兴趣,并希望以适合您的方式进行合作。


其实我确实找到了那篇帖子,但是错过了X1的链接(视觉失明...)。谢谢! - AmonPL
几个问题:
  1. 您是如何执行断言的?我使用的是WSH版本5.8,但出现了“类未注册”的错误。
  2. 您是如何将被测试的vbs文件包含到测试文件中的?(使用ReadAll、Execute等方法?)
- AmonPL
抱歉,我不确定你指的是什么。我的单元测试“系统”有点像Perl方法(不需要类),你只需为适当的值调用比较函数;模块“系统”由ReadAll和ExecuteGlobal实现;它需要一个预处理步骤来获取行号和语法糖。 - Ekkehard.Horner
@AmonPL - 在你的测试.vbs文件中,你可以使用ExecuteGlobal从另一个.vbs文件中“包含”函数和子程序。 - JohnZaj
最终我决定将脚本迁移到Python(并使用Jenkins进行测试),但Ekkehard.Horner的回答也很好,所以我接受了。谢谢! - AmonPL

2
我刚刚推送了一个GitHub仓库,其中包含一个基于超轻量级VBScript的单元测试框架。目前它只有AssertEqual和AssertErrorRaised,但如果需要添加更多的话,这将非常容易:https://github.com/koswald/VBScript 下面是一个spec文件中的片段。
With CreateObject("includer")
    Execute(.read("VBSValidator"))
    Execute(.read("TestingFramework"))
End With

Dim val : Set val = New VBSValidator 'Class Under Test

With New TestingFramework

    .describe "VBSValidator class"

    .it "should return True when IsBoolean is given a True"

        .AssertEqual val.IsBoolean(True), True

End With

这里是一个示例 测试启动器
Main
Sub Main
    With CreateObject("includer")
        ExecuteGlobal(.read("VBSTestRunner"))
    End With
    Dim testRunner : Set testRunner = New VBSTestRunner
    With WScript.Arguments
        If .Count Then

            'if it is desired to run just a single test file, pass it in on the 
            'command line, using a relative path, relative to the spec folder

            testRunner.SetSpecFile .item(0)
        End If
    End With

    'specify the folder containing the tests; path is relative to this script

    testRunner.SetSpecFolder "../spec"

    'run the tests

    testRunner.Run
End Sub

1
卡尔,谢谢。你能否剪切并粘贴一个有用的片段到这里,以便不会出现“你的答案在另一个城堡中”的情况?(参考链接:http://meta.stackexchange.com/questions/225370) - Drew
谢谢您的评论,Drew。我已经相应地进行了更改,希望这样可以了。 - Karl Oswald
谢谢分享,Karl :p - Drew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接