使用 Rake 运行 Ruby 单元测试 [英] Running Ruby unit tests with Rake
问题描述
我正在研究使用 Rake 构建工具来自动运行单元测试.我在网上搜索,但所有的例子都是使用 rails 的.我通常只编写小型命令行程序或简单的 Sinatra 应用程序.
I was investigating using the Rake build tool to automate running unit tests. I searched the web, but all the examples were for using rails. I usually just write small command-line programs or simple Sinatra applications.
所以我想出了以下(可能是糟糕的)解决方案,它只是模拟我在命令行上所做的事情:(我只是以一个单元测试为例.)
So I came up with the following (probably bad) solution that just emulates what I would do on the command-line: (I just ran one unit test as an example.)
desc 'Run unit tests'
task :test do
sh 'ruby -I lib test/test_entry.rb'
end
task :default => :test
它有效,但我不禁想到一定有更好的方法,只是编写 require 'test/test_entry.rb'
不起作用.我遇到 require
问题,Ruby 找不到 lib
目录,所有文件都在那里.
It works, but I can't help thinking there must be a better way, just writing require 'test/test_entry.rb'
doesn't work. I get require
problems, Ruby can't find the lib
directory, where all the files are.
推荐答案
Use Rake::TestTask http://rake.rubyforge.org/classes/Rake/TestTask.html.把它放到你的 Rake 文件中,然后运行 rake test
:
Use Rake::TestTask http://rake.rubyforge.org/classes/Rake/TestTask.html . Put this into your Rake file and then run rake test
:
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/test*.rb']
t.verbose = true
end
这篇关于使用 Rake 运行 Ruby 单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!