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.
我正在研究使用Rake构建工具来自动运行单元测试。我在网上搜索过,但所有的例子都是使用rails。我通常只是编写小型命令行程序或简单的Sinatra应用程序。
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
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.
它有效,但我不禁想到必须有更好的方法,只需要写'test / test_entry.rb'就行了。我得到了问题,Ruby无法找到所有文件所在的lib目录。
2 个解决方案
#1
61
Use Rake::TestTask http://rake.rubyforge.org/classes/Rake/TestTask.html . Put this into your Rake file and then run rake test
:
使用Rake :: TestTask http://rake.rubyforge.org/classes/Rake/TestTask.html。把它放到你的Rake文件中,然后运行rake test:
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/test*.rb']
t.verbose = true
end
#2
3
The problem is that your lib
directory is not included into ruby's loading path. You can fix it like that:
问题是你的lib目录没有包含在ruby的加载路径中。你可以像这样解决它:
$:.unshift 'lib'
require 'test_entry'
or more reliable alternative that adds expanded path of lib
directory to the loading path:
或者更可靠的替代方法,它将lib目录的扩展路径添加到加载路径:
$:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
require 'test_entry'
Btw, global variable $:
has more verbose alias $LOAD_PATH
.
顺便说一下,全局变量$:有更多详细的别名$ LOAD_PATH。
#1
61
Use Rake::TestTask http://rake.rubyforge.org/classes/Rake/TestTask.html . Put this into your Rake file and then run rake test
:
使用Rake :: TestTask http://rake.rubyforge.org/classes/Rake/TestTask.html。把它放到你的Rake文件中,然后运行rake test:
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/test*.rb']
t.verbose = true
end
#2
3
The problem is that your lib
directory is not included into ruby's loading path. You can fix it like that:
问题是你的lib目录没有包含在ruby的加载路径中。你可以像这样解决它:
$:.unshift 'lib'
require 'test_entry'
or more reliable alternative that adds expanded path of lib
directory to the loading path:
或者更可靠的替代方法,它将lib目录的扩展路径添加到加载路径:
$:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
require 'test_entry'
Btw, global variable $:
has more verbose alias $LOAD_PATH
.
顺便说一下,全局变量$:有更多详细的别名$ LOAD_PATH。