因此,我有很多带有测试文件的目录,并为其中的每个目录运行脚本。 看起来像:
unitTests
-func1Tests
---- run.coffee
---- suiteGroup1.coffee
---- suiteGroup2.coffee
-func2Tests
---- run.coffee
---- suiteGroup1.coffee
---- suiteGroup2.coffee
每个run.coffee都包含以下代码:
describe 'func1 tests', ()->
require './suiteGroup1.coffee'
require './suiteGroup2.coffee'
和suiteGroup文件是BDD摩卡测试,带有describe,before / after及其它。
因此,在控制台中,我使用带通配符(例如mocha ./unitTests/*/run.coffee)的命令运行所有测试
我想通过要求运行脚本的路径(我通过glob获得的路径)来以编程方式运行所有测试。
describe 'All tests', ()->
glob "unitTests/*/run.coffee", (err, files)->
for path in files
require path
但这不起作用。我也尝试过。
describe 'All tests', ()->
glob "unitTests/*/run.coffee", (err, files)->
require files[0]
但是这也不起作用。似乎由于使用glob,mocha无法在所需文件中执行描述。
当然,我可以使用控制台命令编写bash脚本,但是我更喜欢通过coffeescript解决此问题。
我还希望避免像这样对所有运行路径进行硬编码:
describe 'all tests', ->
require 'unitTests/func1Tests/run.coffee'
require 'unitTests/func2Tests/run.coffee'
...
那么,如何解决这个问题呢?
答案 0 :(得分:0)
好吧,一段时间后,我找到了解决方法。 我的错是使用回调。我将glob更改为glob.sync,并且一切正常!
describe 'All tests', ()->
for path in glob.sync "unitTests/*/run.coffee"
require path
似乎在异步代码下,mocha不能很好地工作。