如何使用makefile和mocha在子文件夹中运行测试

时间:2013-11-25 14:38:30

标签: makefile mocha

我的测试文件夹结构如下所示:

<test>
    <test1>
    <test2>
    <test3>

是否可以编写一个makefile,在单个文件夹或文件夹的一些列表中执行测试?

由于

3 个答案:

答案 0 :(得分:3)

当然有可能:

# I like to allow the user to override the default mocha with whatever
# they want in case they are running an experimental version.
MOCHA?=mocha

# Useful to pass ad hoc parameters to mocha.
MOCHA_PARAMS?=

# To work around the fact that commas are argument separators.
comma:=,

.PHONY: test test%

# Do all the tests.
test:
    $(MOCHA) --recursive $(MOCHA_PARAMS)

# Allows for combining tests in an ad hoc manner.
test%:
        $(MOCHA) $(MOCHA_PARAMS) $(foreach test,$(subst $(comma), ,$(@:test%=%)),test/test$(test))

最后一个目标允许您执行以下操作:

make test1 # runs the tests in test/test1 only
make test1,2 # runs the tests in test/test1 and test/test2
make test1,2,3 # runs the tests in test/test1, test/test2, test/test3
make test1,3 # runs the tests in test/test1, and test/test3

最后一个目标中发生的事情是目标名称(例如test1,2)已剥离test前缀,在逗号上拆分,然后每个部分都以test/test为前缀

如果要测试它,请将上面的代码复制到Makefile中,然后创建test目录和任意数量的名为test1test2,... {{testN的子目录。 1}}在它下面。

答案 1 :(得分:0)

根本不需要使用make。尝试将以下内容添加到package.json的脚本部分:

"test": "mocha -R spec --recursive",
"test1": "mocha -R spec test/test1",
"test2": "mocha -R spec test/test2",
"test3": "mocha -R spec test/test3",
"test13": "mocha -R spec test/test1 test/test3",

然后,您可以npm testnpm run testnpm run test13

答案 2 :(得分:0)

我的解决方案是

test:
  @./node_modules/.bin/mocha -R spec -r should --recursive
cloudApi:
  find $(CURDIR)/test/cloudApi -iname "*.js" | xargs node_modules/mocha/bin/mocha -R spec -r should 
injectors:
  find $(CURDIR)/test/injectors -iname "*.js" | xargs node_modules/mocha/bin/mocha -R spec -r should  

.PHONY: test cloudApi injectors

并且执行本身看起来像: mocha test / cloudApi / injectors ...