Guard::Minitest 项目教程
1. 项目的目录结构及介绍
Guard::Minitest 是一个用于自动运行 Minitest 测试的工具。项目的目录结构如下:
guard-minitest/
├── Gemfile
├── Guardfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── guard-minitest.gemspec
├── lib/
│ ├── guard/
│ │ ├── minitest/
│ │ │ ├── runner.rb
│ │ │ ├── templates/
│ │ │ │ └── Guardfile
│ │ │ └── version.rb
│ │ └── minitest.rb
│ └── guard.rb
└── test/
├── guard/
│ └── minitest/
│ └── runner_test.rb
├── test_helper.rb
└── minitest_test.rb
目录结构介绍
Gemfile
:定义了项目所需的依赖。Guardfile
:项目的默认配置文件。LICENSE.txt
:项目的许可证文件。README.md
:项目的说明文档。Rakefile
:用于定义 Rake 任务。guard-minitest.gemspec
:项目的 gemspec 文件。lib/
:包含项目的核心代码。guard/
:Guard 插件的核心代码。minitest/
:Minitest 插件的具体实现。runner.rb
:测试运行器。templates/
:模板文件。version.rb
:版本信息。
minitest.rb
:Minitest 插件的入口文件。
guard.rb
:Guard 的主文件。
test/
:包含项目的测试代码。guard/
:Guard 插件的测试代码。minitest/
:Minitest 插件的测试代码。
test_helper.rb
:测试辅助文件。minitest_test.rb
:Minitest 测试文件。
2. 项目的启动文件介绍
Guard::Minitest 的启动文件是 lib/guard/minitest.rb
。这个文件是 Minitest 插件的入口文件,负责初始化和启动 Guard::Minitest。
require 'guard/compat/plugin'
require 'guard/minitest/runner'
module Guard
class Minitest < Plugin
def initialize(options = {})
super
@options = options
@runner = ::Guard::Minitest::Runner.new(options)
end
def start
run_all if @options[:all_on_start]
end
def run_all
UI.info 'Running all tests'
@runner.run(['--verbose'])
end
def run_on_changes(paths)
UI.info "Running tests for #{paths.join(' ')}"
@runner.run(paths)
end
end
end
启动文件介绍
require 'guard/compat/plugin'
:引入 Guard 的兼容插件。require 'guard/minitest/runner'
:引入 Minitest 的运行器。initialize
:初始化方法,设置选项和运行器。start
:启动方法,如果设置了all_on_start
选项,则运行所有测试。run_all
:运行所有测试的方法。run_on_changes
:在文件变化时运行测试的方法。
3. 项目的配置文件介绍
Guard::Minitest 的配置文件是 Guardfile
。这个文件定义了 Guard 的行为,包括监视哪些文件和目录,以及在文件变化时执行哪些操作。
标准 Guardfile 配置
使用 Minitest::Unit 的 Guardfile
guard :minitest do
watch(%r[^test/(.*)\/test_(.*)\.rb$])
watch(%r[^lib/(.*)/(.*)\.rb$]) { |m| "test/#{m[1]}/test_#{m[2]}.rb" }
watch(%r[^test/test