Why is my rake task running twice in my test?

Turns out that rake is already required and initialized when the test runs so all of the following lines need to be removed or the task gets defined twice and runs twice even if you only invoke it once.

require 'minitest/mock'
require 'rake'
...
Rake.application.init
Rake.application.load_rakefile

Updated answer for rails 5.1 (using minitest):

I found I needed the following to load tasks once and only once:

MyAppName::Application.load_tasks if Rake::Task.tasks.empty?

Alternatively add MyAppName::Application.load_tasks to your test_helper, if you don't mind tasks being loaded even when running individual tests that don't need them.

(Replace MyAppName with your application name)