首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在“rake test`”之后执行rake任务

在“rake test`”之后执行rake任务
EN

Stack Overflow用户
提问于 2015-11-19 18:37:30
回答 3查看 1.5K关注 0票数 4

我试图为Rails 4.0.2 (Ruby2.2.3)项目创建一个rake任务,该项目创建测试数据库和种子,然后运行测试套件,然后通过删除测试数据库进行清理。下面是一个简单的例子:

代码语言:javascript
复制
task better_test: :environment do
  Rake::Task["test:setup"].execute
  Rake::Task["test"].execute
  Rake::Task["test:cleanup"].execute
end

上面的任务调用test:setup rake任务(创建/种子测试数据库),然后调用test rake任务,最后调用test:cleanup rake任务。但是,问题是在运行完test:cleanup rake任务之前调用了最后一个test。是否在前面的任务完成后调用清理rake任务?

下面是使用跟踪运行任务的输出:

代码语言:javascript
复制
$ RAILS_ENV=test be rake better_test --trace
/usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.0.2/lib/active_support/values/time_zone.rb:282: warning: circular argument reference - now
/usr/local/rvm/gems/ruby-2.2.3/gems/honeybadger-1.16.3/lib/honeybadger/rack/user_feedback.rb:51: warning: circular argument reference - action
** Invoke better_test (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute better_test
** Execute test:setup
Creating test database.
** Execute db:test:setup
** Execute db:test:create
** Execute db:create
** Execute db:test:load
** Execute db:schema:load
** Execute db:test_project:setup
** Execute db:test_project:create
** Invoke db:create (first_time)
** Invoke environment
** Execute db:create
** Execute db:test_project:migrate:down
** Execute db:test_project:migrate:up
** Execute db:test_project:seed
** Execute test
** Invoke test:run (first_time)
** Invoke test:units (first_time)
** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment
** Invoke db:migrate:load (first_time)
** Invoke environment
** Execute db:migrate:load
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Execute db:drop
** Execute db:create
** Execute db:load
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
** Execute test:prepare
** Execute test:units
** Invoke test:functionals (first_time)
** Invoke test:prepare
** Execute test:functionals
** Invoke test:integration (first_time)
** Invoke test:prepare
** Execute test:integration
** Execute test:run
** Invoke test:decorators (first_time)
** Invoke test:prepare
** Execute test:decorators
** Execute test:cleanup
** Execute db:test:drop
** Execute db:drop
** Execute db:test_project:drop
** Invoke db:drop (first_time)
** Invoke environment
** Execute db:drop
Run options: --seed 19360

# Running tests:

EE....

Finished tests in 0.037493s, 160.0278 tests/s, 106.6852 assertions/s.

如您所见,任务按顺序调用,但清理任务是在测试完成之前执行的。有什么办法解决这个问题吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-11-23 14:22:21

事实证明,这与rake没有什么关系,而更多的是与Minitest执行测试的方式有关。我在问题中提到,看起来rake任务是以正确的顺序被调用的,但是测试后来由于某种原因被执行。原因是Minitest使用ruby方法at_exit来执行测试。在调用rake test时,将读取测试文件,但是所有测试都在ruby程序的末尾执行,甚至在我随后的rake任务之后也是如此。下面是一篇更详细地解释这个问题的博客文章:http://blog.arkency.com/2013/06/are-we-abusing-at-exit/

票数 4
EN

Stack Overflow用户

发布于 2015-11-19 19:48:12

尝试使用rake标准语法调用多个任务:

代码语言:javascript
复制
task better_test: ["test:setup", "test", "test:cleanup"]

也许这能解决你的问题。

而且,Rake似乎实际上构建了在执行这些任务之前应该运行的所有任务的列表,并且每个任务只运行一次。例如:

代码语言:javascript
复制
task :a
task :b
task :c

task better_test: ["a", "b", "c", "b"]

在以下方面的成果:

代码语言:javascript
复制
$ rake better_test -t
** Invoke better_test (first_time)
** Invoke a (first_time)
** Execute a
** Invoke b (first_time)
** Execute b
** Invoke c (first_time)
** Execute c
** Execute better_test

正如您所看到的,任务b只运行一次(第一次)。我相信您的test:setuptest在某种程度上依赖于test:cleanup任务。因此,它比预期的更早被调用。

票数 2
EN

Stack Overflow用户

发布于 2015-11-19 19:59:57

代码语言:javascript
复制
Rake::Task["test"].enhance do
  Rake::Task["test:cleanup"].invoke
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33811821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档