Introduction to MiniTest, a substitute for Ruby unit testing framework TestUnit, testunitminitest
MiniTest is a new generation of Ruby testing framework. It has become a built-in testing framework of Ruby 1.9. It is said that it will also become the default testing framework of Rails 4, which is promising.
Why does MiniTest become the first choice for the latest Ruby and Rails?
In the Ruby 1.8 era, the default testing frameworks of Ruby and Rails are both TestUnit. TestUnit has a long history. Its biggest problem is that it is too slow and too bloated, it contains a bunch of third-party libraries that are rarely used today, such as GTk v1, GTk v2, and FxRuby. Another major problem is that it lacks some basic test features, for example, the spec DSL test style, such as mock support.
MiniTest is equivalent to performing a major rebuild on TestUnit. It inherits most of TestUnit usage, eliminates improper dependencies in TestUnit, and adds basic test features, for example, spec and mock have become quite fast, simple, and clean.
In the world of Ruby and Rails, there is a test framework Rspec that is widely used, and even more widely used than TestUnit and MiniTest. Why is it not the default framework? I think the biggest advantage of MiniTest over Rspec is simplicity and continuity. Rspec is much more complex than MiniTest, and of course more powerful. For built-in Ruby class libraries, it is more advantageous to be simple and maintain continuity (compared with TestUnit. For Rails, the father of Rails, DHH and Rspec, have never been right. I think this is the biggest reason why Rails chose MiniTest. Haha.
MiniTest example
If you have used TestUnit, MiniTest is very simple to use. Below is a tested Hello World
Copy codeThe Code is as follows:
Class HelloWold
Def get_word
"Hello World! "
End
End
Test code
Copy codeThe Code is as follows:
Require 'minitest/autorun'
Class HelloWoldTest <MiniTest: Unit: TestCase
Def test_get_word
Assert_equal "Hello World! ", HelloWold. new. get_word
End
End
Test code, spec Style
Copy codeThe Code is as follows:
Require 'minitest/autorun'
Describe HelloWold do
It "shoshould return hello world" do
HelloWold. new. get_word.must_equal "Hello World! "
End
End
If you want to use MiniTest in a Rails project, you can use gem minitest-rails. After using minitest-rails, the test generated by rails generator is converted to minitest-style code.
Summary
MiniTest is quite simple and fast. It continues the usage of TestUnit and is very quick to get started. It also provides some very good features, and I think it is very good after trial, no wonder it is the first choice for the latest Ruby and Rails, and I plan to use it in the future.