前端测试 cucumber 和 mini_test 一起工作时候遇到 invalid option 的解决方法

恒温 · 2013年10月30日 · 1542 次阅读

公司的一个项目一直用 cucumber 做验收自动化测试。 不知道是哪一次的 Gem 升级之后,发现每次运行,比如

➜  project_a git:(master) bundle exec cucumber -r features features/manage_users/login.feature

结束的时候总是会抛出异常:

/home/xxx/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/minitest-4.7.5/lib/minitest/unit.rb:1037:in `block in process_args': invalid option: -r (OptionParser::InvalidOption)
    from /home/lihuazhang/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/minitest-4.7.5/lib/minitest/unit.rb:1016:in `new'
    from /home/lihuazhang/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/minitest-4.7.5/lib/minitest/unit.rb:1016:in `process_args'
    from /home/lihuazhang/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/minitest-4.7.5/lib/minitest/unit.rb:1066:in `_run'
    from /home/lihuazhang/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/minitest-4.7.5/lib/minitest/unit.rb:1059:in `run'
    from /home/lihuazhang/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/minitest-4.7.5/lib/minitest/unit.rb:795:in `block in autorun'

而其实 feature 是全部通过的。这在平时命令行下运行或调试影响不大,但是如果集成到 Jenkins 里就会一直告诉你失败。因为 Jenkins 是通过命令执行的结果来判断成功与否。如下:

Execte Shell:

RAILS_ENV=test CUCUMBER_OPTS="--require features" 
RAILS_ENV=test rake ci:setup:cucumber
RAILS_ENV=test bundle exec cucumber -r features features --format CI::Reporter::Cucumber # 这里结束会抛出 invalid option 的 error 就会导致 Jenkins 认为整个 step 都是失败的。

在 stackoverflow 上找到方法说,在命令行末尾加 echo $?, 试用了下,也工作的挺好,但总不是治本的方法。在搜索无果的情况给老板写了封邮件,老板不出五分钟就回信了:

It looks like a bug in cucumber/multi_test.

according to https://github.com/cucumber/cucumber-rails/issues/252
autorun of some other tests is triggered if some options are passed to cucumber, in this case "-r features".

This is turned off by disabling autorun of mini_test, which can be done in features/support, according to

https://github.com/cucumber/multi_test/pull/2

Following what was suggested there, I added a file features/support/minitest_disable.rb. Minitest is not autorun after cucumber, even when args are passed to cucumber.

Please pull and try it. I also updated some gems, so you will need to bundle install.

其实就是在, features/support/ 下面添加一个 minitest_disable.rb, 放两句话:

require 'multi_test'
MultiTest.disable_autorun

这两句话其实可以在 cucumber 最近的几个版本的 core_ext/disable_mini_and_test_unit_autorun.rb 里发现,其实我检查过,我用的版本里也有,不知道为什么不起作用。 不过全部把它扔到 cucumber 的 world 里, 一劳永逸算了。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册