Ruby-on-rails 如何让 Rspec 运行嵌套在文件夹下的所有测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9362799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get Rspec to run all tests nested under a folder?
提问by Nathan Long
I like to run my Rspec tests with Spork running in a separate tab. I usually run my tests using rspec spec, by which I intend to say "search recursively and run everything in the specfolder."
我喜欢在单独的选项卡中运行 Spork 来运行我的 Rspec 测试。我通常使用 运行我的测试rspec spec,我打算通过它说“递归搜索并运行spec文件夹中的所有内容”。
I've recently realized that this does not actually run all my tests. I now have a spec file in spec/requestswhich isn't being run. I know this because I've edited one of the tests to raise an error, and run the following:
我最近意识到这实际上并没有运行我的所有测试。我现在有一个spec/requests未在其中运行的规范文件。我知道这一点是因为我编辑了其中一个测试以引发错误,并运行以下命令:
rspec spec- no error raised.rspec spec/requests- still no error raised, and0 examples, 0 failures!rspec spec/requests/my_controller.rb- bingo.17 examples, 1 failureand the failure has my error message.
rspec spec- 没有出现错误。rspec spec/requests- 仍然没有出现错误,并且0 examples, 0 failures!rspec spec/requests/my_controller.rb-宾果游戏。17 examples, 1 failure失败有我的错误信息。
Why isn't Rspec finding all my test files? Is this a matter of configuration, or do I need to use a different command to run my tests?
为什么 Rspec 没有找到我所有的测试文件?这是配置问题,还是我需要使用不同的命令来运行我的测试?
I need to run all my tests at once to ensure that I'm not introducing regressions.
我需要一次运行所有测试以确保我不会引入回归。
(Not using Spork makes no difference, by the way.)
(顺便说一句,不使用 Spork 没有区别。)
回答by farnoy
Rspec shouldalready look recursively through the directory you named and find all tests. Note however, that it's looking for files ending in _spec.rb. Maybe some of your files are not named correctly?
Rspec应该已经通过您命名的目录递归查找并找到所有测试。但是请注意,它正在寻找以_spec.rb. 也许您的某些文件未正确命名?
If you need to be more specific about which files it should find, you can also use the --patternoption. For example: rspec --pattern spec/requests/*_spec.rb. (Option --patternis equal to -P. Taken from rspec --help)
如果您需要更具体地了解它应该找到哪些文件,您也可以使用该--pattern选项。例如:rspec --pattern spec/requests/*_spec.rb。(选项--pattern等于-P。取自rspec --help)
回答by Priyanka Gajjar
you can create a rake task
你可以创建一个耙子任务
desc "Run PCMag tests"
RSpec::Core::RakeTask.new('test') do |t|
t.rspec_opts = ["-Ilib","--format documentation","--color"]
t.pattern = ['spec/test/*.rb']
end
Then execute command rake test.
然后执行命令rake test。
Above command will execute all .rb tests under folder 'test'
上面的命令将执行文件夹“test”下的所有 .rb 测试
Please check following link for more details.
请查看以下链接了解更多详情。
http://testautomationarchives.blogspot.in/2013/10/rspec-rake-framework-in-ruby-with.html
http://testautomationarchives.blogspot.in/2013/10/rspec-rake-framework-in-ruby-with.html

