How to test your own RubyGem ?, Test rubygem
How to test a Gem
If you want to use the gem after development, you need to test it. Testing a gem is actually very simple. Here we use minitest as an example. rspec is also applicable. Let's take a look at the directory structure of the current gem:
-Rw-r -- 1 lizhe 90 July 2 15:52 Gemfile-rw-r -- 1 lizhe 379 July 3 10:09 Gemfile. lockdrwxrwxr-x 3 lizhe 4096 July 2 15:52 lib-rw-r -- 1 lizhe 1062 July 2 15:52 LICENSE.txt-rw-r -- 1 lizhe 923 July 3 10:09 mygem. gemspecdrwxrwxr-x 2 lizhe 4096 July 2 18:33 pkg-rw-r -- 1 lizhe 187 July 3 10:35 Rakefile-rw-r -- 1 lizhe 556 July 2 15:52 README. md
Open mygem. gemspec and addminitest
:
spec.add_development_dependency "minitest", "~> 5.7.0"
Runbundle install
Installminitest
.
Createtest
Folder, store our test cases, and then createtest_helper.rb
File.test_helper.rb
The content is as follows:
$ LOAD_PATH <"./lib" # Add lib to load pathrequire 'minitest/autorun '# introduce minitestrequire 'myg'
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") endend
Run the test now:
$ ruby test/test_mygem.rb/home/lizhe/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- test_helper (LoadError) from /home/lizhe/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require' from test/test_mygem.rb:1:in `<main>'
Error! Not foundtest_helper
Because it is not in the loading path, let's change the method,require_relative 'test_helper'
Because our command is in the gem root directory, the relative path is the current path. If it is executed in the test directory, it needs to be writtenrequire_relative '../test_helper'
It's quite troublesome. OK. Run the following command to try it:
$ ruby test/test_mygem.rbRun options: --seed 30741# Running:.Finished in 0.000793s, 1260.9959 runs/s, 1260.9959 assertions/s.1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
Use Rake: TestTask to simplify the test process
In the previous test method, we need to manually add the lib directory to the load path, and then in each test case file, we needrequire_relative 'test_helper'
It is very troublesome. Now we can simplify this process.
AddRake::TestTask
ToRakefile
Medium:
require 'rake/testtask'Rake::TestTask.new do |t| t.libs << 'test' << 'lib' t.pattern = "test/test_*.rb"end
Now, in testhelper$LOADPATH << './lib'
Removerequire_relative
Replacerequire
Becauserak test task
You have added both the test and lib directories to the load path, and then runrake test
:
$ rake testRun 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
Further simplified, each test case file requiresrequire 'test_helper'
It is also troublesome. Can you make it automatically execute this action? Yes. You only need to add the following options:
Require 'rake/testtask' rake: testtask. new do | t. libs <'test' <'lib 't. pattern = "test/test _*. rb "t. ruby_opts <"-r test_helper" # Add the ruby runtime parameter and the end of the file specified by require
Now, in the test caserequire 'test_helper'
This line is also removed and executedrake test
You can also run the test, and write a row less: smile:
Set the default task:
Require 'rake/testtask' rake: testtask. new do | t. libs <'test' <'lib 't. pattern = "test/test _*. rb "t. ruby_opts <"-r test_helper" # Add the ruby runtime parameter. The file endtask specified by require: default =>: test
In this way, I can directly execute rake to run the test, saving the test.
If we have multiple test examples, this rake test task will run all tests. If we want to run a specified rake test task, how can we do this? Specify a TEST parameter:
rake test TEST=test/test_mygem.rb
Reference link:
This article is an original article by OneAPM engineers. OneAPM is a leading enterprise in the field of basic software in China. It helps enterprise users and developers easily achieve slow real-time crawling of program code and SQL statements. For more technical articles, visit the OneAPM official technical blog.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.