How do I test a Gem

It is easy to test a gem. Here we use Minitest as an example, and RSpec is the same. Let’s look at the directory structure of our current gem:

-rw-rw-r-- 1 lizhe lizhe 90 July 2 15:52 Gemfile -- rw-rw-r-- 1 lizhe lizhe 379 July 3 10:09 gemfile.lock drwxrwxr-x 3 lizhe Lizhe 4096 July 2 15:52 lib-rw-rw-r -- 1 Lizhe Lizhe 1062 July 2 15:52 license.txt-rw-rw-r -- 1 Lizhe Lizhe 923 July 3 10:09 Mygem. Gemspec drwxrwxr-x 2 Lizhe Lizhe 4096 18:33 pkg-rw-rw-r -- 1 Lizhe Lizhe 187 13:35 rakefile-rw-rw-r -- 1 Lizhe Lizhe 556 July 2 15:52 Readme.md

Open myGems. GemSpec and add minitest:

Spec. Add_development_dependency minitest ", "" - > 5.7.0"

Execute the bundle install to install minitest.

Create a new test folder to hold the use cases for our tests, and then create a new file called test_helper.rb and put it inside. Test_helper.rb has the following contents:

$LOAD_PATH << "./lib" # load path require 'minitest/autorun' # load path require 'mygem'

Let’s create a new test case, test_mygem. Rb:

require "test_helper"

class MygemTest < Minitest::Test

  def test_hello_output
    assert_equal(Mygem.hello, "hello from my gem")
  end

end

Now let’s execute the test:

$ ruby test/test_mygem.rb / home/lizhe /. RVM/rubieslast/ruby - 2.1.5 / lib/ruby/site_ruby / 2.1.0 / rubygems/core_ext/kernel_require rb: 54: ` in the require ': cannot load such file -- test_helper (LoadError) from / home/lizhe /. RVM/rubieslast/ruby - 2.1.5 / lib/ruby/site_ruby / 2.1.0 / rubygems/core_ext/kernel_require rb: 54: ` in the require 'the from test/test_mygem.rb:1:in `<main>'

Make a mistake! I can’t find test_helper because it’s not in the loading path, so I’m going to use require_relative ‘test_helper’ instead. Because my command is in the gem root directory, the relative path is the current path. If I’m executing in the test directory, You need to write require_relative ‘.. /test_helper’, which is a bit of a hassle. OK, let’s go ahead and try it:

$ ruby test/test_mygem.rb Run options: --seed 30741 # Running: Finished in 0.000793s, 1260.9959 runs/s. 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

Use Rake::TestTask to simplify the testing process

In the previous test method, we had to manually add the lib directory to the load path, and then require_relative ‘test_helper’ in each test case file, which was cumbersome, so let’s simplify this process.

First add Rake:: testTask to Rakefile:

require 'rake/testtask'

Rake::TestTask.new do |t|
  t.libs << 'test' << 'lib'
  t.pattern = "test/test_*.rb"
end

Now remove $LOADPATH << ‘./lib’ from testhelper and replace require_relative with require in the test case file. The rak test task has added both the test and lib directories to the load path, and then executes the rake test:

$ rake test Run options: --seed 29947 # Running: Finished in 0.000969s, 1031.6447 runs/s, 1031.6447 assertions/s. 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

To simplify matters further, it would be cumbersome to require ‘test_helper’ for every test case file. Could you make it do this automatically? Yes, just add one more option:

require 'rake/testtask' Rake::TestTask.new do |t| t.libs << 'test' << 'lib' t.pattern = "test/test_*.rb" t.ruby_opts << "-rtest_helper" # Add a Ruby runtime parameter that requires the specified file end

Now remove the require ‘test_helper’ line from the test case. Run the rake test, which can also run the test, with one less line 🙂

Now let’s set the default task:

require 'rake/testtask' Rake::TestTask.new do |t| t.libs << 'test' << 'lib' t.pattern = "test/test_*.rb" t.ruby_opts << End task :default => :test "-rtest_helper" # require end task :default => :test

So I can just run rake and run tests, and I don’t even have to do that test.

If we have multiple test cases, this Rake Test task will run all of the tests. What if we want to run one of the tests? Specify a TEST parameter:

rake test TEST=test/test_mygem.rb

Refer to the link

  • Rake::TestTask

This article isOneAPMEngineer Li Zhe original article. To read more technical articles, visit OneAPMOfficial Technology Blog.