ruby Rspec运行除特定文件夹之外的所有测试

6psbrbz9  于 2023-05-22  发布在  Ruby
关注(0)|答案(4)|浏览(147)

我正在做的项目有一个相当大的测试套件。我目前正在编写测试,当我单独运行它时,它通过了,但是当我运行整个测试套件$rspec时,我得到了一些非常奇怪的行为,导致测试失败。
现在测试是这样嵌套的:

spec/folder1/folder2/folder3/test.rb

这些命令中的每一个都可以正常运行测试,并且一切都通过了:

$rspec spec/folder1/folder2/folder3
$rspec spec/folder1/folder2
$rspec spec/folder1/

还有大约10个与folder1处于同一级别的其他文件夹,我不想单独与套件的其余部分一起运行,以确定哪个文件夹包含破坏我正在进行的测试的测试。
这可能吗?

8hhllhi2

8hhllhi21#

使用--exclude-pattern,它们非常方便:
https://www.relishapp.com/rspec/rspec-core/v/3-3/docs/configuration/exclude-pattern
他们的优点之一是
--exclude-pattern标志接受shell样式的glob联合
所以你可以像rspec --exclude-pattern "spec/{foldername1,foldername2}/**/*_spec.rb"这样

wn9m85ua

wn9m85ua2#

看看Rspec项目中的Exclusion filters,可能会有帮助。
您还可以使用Inclusion filters只运行您想要的测试。

6mw9ycah

6mw9ycah3#

我知道这个问题是针对RSpec的,但是,我试图弄清楚如何使用minitest和--exclude标志来实现这一点,它只过滤名称,而不是文件位置。
要在minitest中对文件位置进行此操作,您需要添加一个rake任务。
lib/tasks/test.rake

# lib/tasks/test.rake

Rake::Task["test:system"].clear

namespace :test do
  desc "Run all system tests except test/serial folder"
  task system: "test:prepare" do
    $: << "test"
    test_files = FileList["test/system/**/*_test.rb"].exclude(
      "test/system/serial/**/*_test.rb"
    )
    Rails::TestUnit::Runner.run(test_files)
  end

  desc "Run all serial test folder"
  task serial: "test:prepare" do
    $: << "test"
    test_files = FileList["test/system/serial/*_test.rb"]
    Rails::TestUnit::Runner.run(test_files)
  end
end
2uluyalo

2uluyalo4#

在我的例子中,我想排除尚未实现的脚手架测试,每次运行它们时都传递--exclud-pattern标志很麻烦(其他一些解决方案也是如此)。所以,我最终做的是:

*rails_helper.rb

RSpec.configure do |config|
  config.filter_run_excluding type: :skip
end

然后在我想跳过的每个测试中将type:更改为:skip。例如:

spec/views/page_spec.rb

# NOTE: Change to `type: :view` to include test
RSpec.describe 'pages/new', type: :skip do
  # some scaffolded tests
end

这使我能够在整个目录中查找/替换所有的测试,并且能够在充实测试时有选择地包含它们。

相关问题