ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Test ဆိုတာ ကိုယ့် code က ကိုယ်ပြောထားတဲ့အတိုင်း အလုပ်လုပ်မလုပ် စစ်တဲ့ program သေးသေးလေး တစ်ခုပါ။ "ဒီ input ပေးရင် ဒီအဖြေ ထွက်ရမယ်" လို့ ရေးထားပြီး ကွန်ပျူတာကို အကြိမ်တိုင်း စစ်ခိုင်းတာပါ။ ကိုယ့်မျက်စိနဲ့ output ကို ကြည့်စစ်တာနဲ့ မတူဘဲ စစ်ချက်က မမေ့တော့ပါဘူး၊ တစ်စက္ကန့်အတွင်း ထောင်ချီ စစ်နိုင်ပါတယ်။
Minitest က Ruby နဲ့အတူ ပါလာတဲ့ test library ပါ၊ install လုပ်စရာ မလိုဘူး။ ကြီးကျယ်တဲ့ DSL မရှိဘဲ class နဲ့ method ရိုးရိုးလေးတွေနဲ့ ရေးရတာမို့ Ruby ကို သိပြီးသားဆိုရင် မိနစ်အနည်းငယ်အတွင်း စရေးနိုင်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: မျက်စိနဲ့ စစ်တာက အကုန်မမိဘူး
average method တစ်ခု ရေးပြီး ကိုယ်ကြိုက်တဲ့ နံပါတ်တွေနဲ့ puts နဲ့ ကြည့်လိုက်ပါတယ်။ ပထမ output က မှန်သလိုပဲ ဖြစ်နေလို့ ပြီးပြီလို့ ထင်မိပါတယ်။ ဒါပေမယ့် အောက်က code မှာ bug နှစ်ခု ပုန်းနေပါတယ်။ [1, 2] ရဲ့ ပျမ်းမျှက 1.5 ဖြစ်ရမယ့်အစား integer စားခြင်းကြောင့် 1 ထွက်ပြီး အလွတ် array ကတော့ ZeroDivisionError နဲ့ crash ဖြစ်ပါတယ်။
မျက်စိနဲ့ စစ်တာမှာ ကိုယ်စမ်းမိတဲ့ ဥပမာတွေပဲ စစ်ပြီး code ပြင်ပြီးတိုင်း အားလုံးကို ပြန်စစ်ဖို့ မေ့ပါတယ်။ ဒါကြောင့် ပြင်လိုက်လို့ အဟောင်းတစ်ခု ပျက်သွားတာကို နောက်မှ အသုံးပြုသူက သိတာမျိုး ဖြစ်ပါတယ်။
def average(numbers) = numbers.sum / numbers.size
puts average([2, 4, 6]) # 4 looks right
puts average([1, 2]) # 1 should be 1.5: integer division
puts average([]) # ZeroDivisionError: divided by 0ဘာကြောင့် test လိုတာလဲ
Test က "မှန်တယ်" ဆိုတာကို ကိုယ့်ခံစားချက်ကနေ ကွန်ပျူတာ စစ်ပေးတဲ့ အချက်အလက်အဖြစ် ပြောင်းပေးပါတယ်။ ပုံမှန်၊ အလွတ်၊ နယ်နိမိတ်နဲ့ error case တွေကို တစ်ခါရေးထားရင် code ပြောင်းတိုင်း အလိုအလျောက် ပြန်စစ်ပေးပြီး bug ကိုလည်း ပြန်မဖြစ်အောင် တားပေးပါတယ်။
Core concept: test class၊ assertion နဲ့ report
Minitest test file မှာ အပိုင်းသုံးပိုင်း ရှိပါတယ်။ ပထမ require "minitest/autorun" က library ကို load လုပ်ပြီး file ပြီးတဲ့အခါ test အားလုံး run ဖို့ စီစဉ်ပေးပါတယ်။ ဒုတိယ Minitest::Test ကို inherit လုပ်တဲ့ class တစ်ခုပါ၊ နာမည်က test_ နဲ့ စတဲ့ method တစ်ခုစီက test တစ်ခုပါ။ တတိယက အဲဒီ method တွေထဲက assertion တွေပါ၊ assert_equal expected, actual၊ assert_raises(SomeError) { ... }၊ assert၊ assert_nil စတာတွေပါ။ မှန်တဲ့ assertion က ဘာမှ မလုပ်ပါဘူး၊ မမှန်ရင် Minitest::Assertion ကို raise လုပ်ပြီး test ကို failed လို့ မှတ်ပါတယ်။
Test တိုင်းကို class ရဲ့ instance အသစ်တစ်ခုမှာ run ပြီး setup က test တစ်ခုစီ မတိုင်ခင်၊ teardown က test တစ်ခုစီပြီးမှ run လို့ test တိုင်းက သန့်ရှင်းတဲ့ အနေအထားကနေ စပါတယ်။ Minitest က test တွေကို random အစီအစဉ်နဲ့ run ပြီး report ထိပ်မှာ seed ကို ပြပါတယ် (--seed 42 နဲ့ အဲဒီအစီအစဉ်ကို ပြန်ဖွင့်လို့ရပါတယ်)။ ဒါက ရည်ရွယ်ချက်ရှိပါတယ်၊ တခြား test တစ်ခု run ပြီးမှ အောင်တဲ့ test ဆိုတာ ဖုံးကွယ်ထားတဲ့ မှီခိုမှု ဖြစ်ပြီး random အစီအစဉ်က အဲဒါကို မြန်မြန် ဖော်ပေးပါတယ်။
Report ဖတ်ပုံက test တစ်ခုအောင်ရင် dot တစ်လုံး၊ assertion မှားရင် F၊ မမျှော်လင့်တဲ့ exception (စာလုံးပေါင်းမှားလို့ NoMethodError လိုမျိုး) တက်ရင် E ပြပါတယ်။ Failure နဲ့ error တွေကို test နာမည်၊ line နဲ့ assert_equal ဆိုရင် expected နဲ့ actual တန်ဖိုးတို့နဲ့အတူ အောက်မှာ စာရင်းပြပါတယ်။ နောက်ဆုံးစာကြောင်းက run၊ assertion၊ failure၊ error၊ skip အရေအတွက်ပါ။ Seed နဲ့ timing စာကြောင်းက run တိုင်း ပြောင်းလို့ ဘယ်တော့မှ expected output အဖြစ် copy မကူးပါနဲ့။
assert_equal က expected တန်ဖိုးကို ပထမ၊ actual တန်ဖိုးကို ဒုတိယ ယူပါတယ်။ ပြောင်းထည့်မိတာကို ဘာမှ မတားပါဘူး၊ test က မှန်မှန်ကန်ကန် အောင်/ကျ ဆက်ဖြစ်ပါတယ်။ ဒါပေမယ့် မအောင်တဲ့အခါ message က ဂဏန်းတွေကို ပြောင်းပြန်တံဆိပ်ကပ်ပြီး မှားတဲ့ဘက်ကို ရှာခိုင်းပါလိမ့်မယ်။
| Assertion | ဘယ်အချိန် အောင်လဲ |
|---|---|
| assert_equal expected, actual | expected == actual |
| assert_nil value | value က nil ဖြစ် |
| assert_includes list, item | list.include?(item) |
| assert_in_delta expected, actual | float နှစ်ခုက delta အတွင်း နီး (default 0.001) |
| assert_raises(Error) { ... } | block က Error ကို raise လုပ်၊ exception ကို ပြန်ပေး |
| refute_equal၊ refute_nil ... | assert_ ပုံစံရဲ့ ပြောင်းပြန် |
- Setup ပြင်၊ action လုပ်၊ assert စစ်
- Test တိုင်း instance အသစ်မှာ run
- Dot က အောင်၊ F က ကျ၊ E က error
- Code ပြင်ပြီး ပြန် run
Test တစ်ခုချင်းက သီးခြားဖြစ်ရပါမယ်။ ဘယ်အစီအစဉ်နဲ့ run run ရလဒ် တူရမယ်။
အခြေခံ ပုံစံ
require "minitest/autorun"
class NameTest < Minitest::Test
def setup
@subject = Thing.new
end
def test_describes_expected_behaviour
assert_equal expected, @subject.action
assert_raises(SomeError) { @subject.bad_action }
end
endClass နာမည်က XxxTest၊ method တွေက test_ နဲ့ စပါတယ်။ setup က test တစ်ခုစီ မတိုင်ခင် run ပြီး @subject လို instance variable တွေကို ပြင်ဆင်ပေးပါတယ်။ Method နာမည်ကို ဘာကို စစ်လဲ ပြောတဲ့ စာကြောင်းလို ရေးပါ။
Example: shopping cart ကို စမ်းသပ်ခြင်း
Cart class တစ်ခုနဲ့ test သုံးခု ပါပါတယ်၊ အလွတ် cart၊ ဈေးပေါင်းခြင်း၊ ဈေး 0 ကို ပယ်ခြင်းတို့ပါ။ ကိုယ့် file တွေမှာ require "minitest/autorun" ရေးရင် file ပြီးတဲ့အခါ test တွေ အလိုအလျောက် run ပါတယ်။ ဒီ lesson ရဲ့ ဥပမာမှာတော့ run ကို ကိုယ်တိုင် စတင်ပါတယ်၊ ဒါမှ seed ကို 42 လို့ ပုံသေထားနိုင်ပြီး run တိုင်း ပြောင်းတတ်တဲ့ "Finished in" timing စာကြောင်းကို ဖြုတ်လို့ ရလို့ output က အမြဲတူပါတယ်။
require "minitest"
require "stringio"
class Cart
def initialize
@items = []
end
def add(name, cents)
raise ArgumentError, "price must be positive" unless cents.positive?
@items << [name, cents]
self
end
def size = @items.size
def total_cents = @items.sum { |_, cents| cents }
end
class CartTest < Minitest::Test
def setup
@cart = Cart.new
end
def test_new_cart_is_empty
assert_equal 0, @cart.size
assert_equal 0, @cart.total_cents
end
def test_total_adds_prices
@cart.add("pen", 150).add("book", 1250)
assert_equal 1400, @cart.total_cents
end
def test_rejects_zero_price
error = assert_raises(ArgumentError) { @cart.add("free", 0) }
assert_equal "price must be positive", error.message
end
end
# Start the run ourselves so the seed is fixed and the timing line can be removed
def run_quietly(seed)
captured = StringIO.new
original = $stdout
$stdout = captured
begin
Minitest.run(["--seed", seed.to_s])
ensure
$stdout = original
end
captured.string.sub(/^Finished in .*\n\n/, "")
end
puts run_quietly(42)Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Test class
class CartTest < Minitest::TestMinitest::Test ကို inherit လုပ်ထားလို့ Minitest က ဒီ class ကို test အဖြစ် သိပါတယ်။ ထဲက test_ နဲ့ စတဲ့ method တစ်ခုစီက test တစ်ခုစီ ဖြစ်ပါတယ်။
- setup
@cart = Cart.newsetup က test တစ်ခုစီ မတိုင်ခင် run လို့ test တိုင်းမှာ cart အလွတ်အသစ်တစ်ခု ရပါတယ်။ ဒါကြောင့် test တစ်ခုက ထည့်လိုက်တဲ့ item တွေက တခြား test ကို မထိခိုက်ပါဘူး။
- Expected first
assert_equal 1400, @cart.total_centsပထမ argument 1400 က ကိုယ်မျှော်လင့်တဲ့ တန်ဖိုး၊ ဒုတိယက code ကပေးတဲ့ တန်ဖိုးပါ။ 150 + 1250 = 1400 မို့ ဒီ assertion အောင်ပါတယ်။
- assert_raises
error = assert_raises(ArgumentError) { @cart.add("free", 0) }Block ထဲက code က ArgumentError ကို raise လုပ်ရမယ်၊ မလုပ်ရင် test ကျပါတယ်။ ပြန်ရတဲ့ exception ကို variable ထဲ ယူပြီး message ကိုလည်း ဆက်စစ်နိုင်ပါတယ်။
- Fixed seed
Minitest.run(["--seed", seed.to_s])ဒီစာကြောင်းက test အားလုံးကို seed 42 နဲ့ run ပါတယ်။ Seed တူရင် random အစီအစဉ်လည်း တူလို့ ထပ်ပြောင်းလဲမှု မရှိပါဘူး။ ကိုယ့် file တွေမှာတော့ autorun ကို သုံးရင် ပြီးပါတယ်။
- Dropping the timing
captured.string.sub(/^Finished in .*\n\n/, "")Minitest ရဲ့ output ကို StringIO ထဲ ဖမ်းပြီး "Finished in ..." စာကြောင်းကို ဖယ်ထုတ်ပါတယ်။ ကျန်တဲ့ အပိုင်း (seed၊ dots၊ အရေအတွက်) က Minitest ရဲ့ တကယ့် output ပါပဲ။
Run options: --seed 42
# Running:
...
3 runs, 5 assertions, 0 failures, 0 errors, 0 skipsReport အစစ်ကို ဖတ်ခြင်း
ကိုယ့် file ကို ruby cart_test.rb နဲ့ run ရင် autorun က အောက်ကလို report ထုတ်ပါတယ်။ အောက်က report နှစ်ခုက တကယ် run ထားတာပါ၊ ပထမတစ်ခုမှာ test_total_adds_prices ရဲ့ expected ကို 1500 လို့ တမင်မှားရေးထားပြီး ဒုတိယတစ်ခုမှာ test_new_cart_is_empty ထဲက method နာမည်ကို totl_cents လို့ တမင်စာလုံးပေါင်းမှားထားပါတယ်။ Seed၊ "Finished in" စာကြောင်းနဲ့ dot/F အစီအစဉ်တွေက run တိုင်း ကွဲပြားမှာဖြစ်လို့ ကိုယ့်စက်မှာ တူချင်မှ တူပါလိမ့်မယ်၊ ဒါပေမယ့် ပုံစံကတော့ အတူတူပါပဲ။
Run options: --seed 51234
# Running:
..F
Finished in 0.001178s, 2547.3380 runs/s, 4245.5634 assertions/s.
1) Failure:
CartTest#test_total_adds_prices [cart_test.rb:31]:
Expected: 1500
Actual: 1400
3 runs, 5 assertions, 1 failures, 0 errors, 0 skipsRun options: --seed 7
# Running:
..E
Finished in 0.000889s, 3375.7174 runs/s, 4500.9565 assertions/s.
1) Error:
CartTest#test_new_cart_is_empty:
NoMethodError: undefined method 'totl_cents' for an instance of Cart
cart_test.rb:26:in 'CartTest#test_new_cart_is_empty'
3 runs, 4 assertions, 0 failures, 1 errors, 0 skipsFailure နဲ့ Error ကို ခွဲကြည့်ပါ။ Failure ဆိုတာ assertion က "မမှန်ဘူး" လို့ ပြောတာပါ၊ code က အလုပ်လုပ်ပြီး အဖြေ မှားနေတာပါ။ Error ဆိုတာ test run နေတုန်း မမျှော်လင့်တဲ့ exception တက်တာပါ၊ ဥပမာ totl_cents လို method နာမည်စာလုံးပေါင်းမှားတာပါ။ Error ဆိုရင် assertion တစ်ခုမှ မပြီးခင်မှာတင် ရပ်သွားလို့ နောက်ကထပ်ရှိတဲ့ assertion တွေကို မစစ်ပါဘူး။
ဘယ်ဘက်က ရေးရမလဲ၊ failure message ကို ဖတ်ခြင်း
အောက်က script က test class မရေးဘဲ assertion တွေကို တိုက်ရိုက် စမ်းပြီး message တွေကို ပြပါတယ်။ Probe class က Minitest::Assertions ကို include လုပ်ထားလို့ test class အပြင်မှာလည်း assertion တွေ သုံးလို့ရပါတယ်။ total([2, 3, 1]) က 6 ပါ။ ကိုယ်က 5 လို့ မျှော်လင့်ထားတာမို့ ပထမ message က "Expected: 5, Actual: 6" ဖြစ်ပြီး မှန်ပါတယ်။ ဒုတိယမှာ argument နှစ်ခုကို ပြောင်းလိုက်တော့ message က "Expected: 6, Actual: 5" လို့ ပြပြီး မှားပြောနေသလို ဖြစ်သွားပါတယ်။ Float ကိုတော့ assert_equal နဲ့ မစစ်ပါနဲ့၊ 0.1 + 0.2 က 0.30000000000000004 ဖြစ်လို့ assert_in_delta ကို သုံးပါ။
require "minitest"
# A tiny object that can run assertions outside a test class
class Probe
include Minitest::Assertions
attr_accessor :assertions
def initialize = @assertions = 0
end
def total(prices) = prices.sum
def message_for
yield
"(passed)"
rescue Minitest::Assertion => e
e.message
end
probe = Probe.new
puts "Right order:"
puts message_for { probe.assert_equal 5, total([2, 3, 1]) }
puts "Swapped:"
puts message_for { probe.assert_equal total([2, 3, 1]), 5 }
puts "Floats:"
puts message_for { probe.assert_equal 0.3, 0.1 + 0.2 }
puts message_for { probe.assert_in_delta 0.3, 0.1 + 0.2 }
puts "assert_raises:"
puts message_for { probe.assert_raises(ArgumentError) { 1 + 1 } }Right order:
Expected: 5
Actual: 6
Swapped:
Expected: 6
Actual: 5
Floats:
Expected: 0.3
Actual: 0.30000000000000004
(passed)
assert_raises:
ArgumentError expected but nothing was raised.Test style နဲ့ spec style
Minitest မှာ ရေးနည်း နှစ်မျိုး ရှိပါတယ်။ အခုအထိ သုံးတဲ့ test style က class၊ method နဲ့ assert_ ပါ။ Spec style (require "minitest/spec") ကတော့ RSpec နဲ့ ဆင်တူပြီး describe၊ it၊ before နဲ့ _(value).must_equal ပါ။ နှစ်မျိုးလုံး အတူတူ runner၊ report ကို သုံးပြီး မတူတာ ရေးပုံပဲ ဖြစ်ပါတယ်၊ ဒါကြောင့် အဖွဲ့ရဲ့ ဘုံထုံးစံကို ရွေးရင် ရပါတယ်။ Spec style မှာလည်း must_equal က expected ကို ယူတာ (_(actual).must_equal expected) ဖြစ်လို့ ဘယ်ဘက်ကဘာလဲ မမှားပါနဲ့။
| Test style | Spec style |
|---|---|
| class StackTest < Minitest::Test | describe Stack do |
| def setup | before do |
| def test_pops_last_item | it "pops the last item" do |
| assert_equal 2, stack.pop | _(stack.pop).must_equal 2 |
| assert_raises(IndexError) { ... } | _ { ... }.must_raise IndexError |
require "minitest/spec"
require "stringio"
class Stack
def initialize = @items = []
def push(item) = @items.push(item) && self
def pop = @items.pop || raise(IndexError, "stack is empty")
def empty? = @items.empty?
def size = @items.size
end
describe Stack do
before do
@stack = Stack.new
end
it "starts empty" do
_(@stack.empty?).must_equal true
end
it "pops the last pushed item" do
@stack.push(1).push(2)
_(@stack.pop).must_equal 2
_(@stack.size).must_equal 1
end
it "raises when popping an empty stack" do
error = _ { @stack.pop }.must_raise IndexError
_(error.message).must_equal "stack is empty"
end
end
captured = StringIO.new
original = $stdout
$stdout = captured
begin
Minitest.run(["--seed", "7"])
ensure
$stdout = original
end
puts captured.string.lines.last # 3 runs, 5 assertions, 0 failures, 0 errors, 0 skipsCommand line ကနေ run ခြင်း
လက်တွေ့ project မှာ test file တွေကို test/ folder ထဲ ထားပြီး ruby ကို တိုက်ရိုက်ခေါ်တာ ဒါမှမဟုတ် rake test နဲ့ run ပါတယ်။ --seed နဲ့ မအောင်တဲ့ အစီအစဉ်ကို ပြန်ဖွင့်နိုင်ပြီး -n နဲ့ test တစ်ခုတည်း ဒါမှမဟုတ် နာမည်ကိုက်တာတွေကိုပဲ run နိုင်ပါတယ်။ Minitest 6 ရဲ့ help text မှာ အဲဒီ option ကို -i/--include လို့ ပြပေမယ့် -n ကလည်း ဆက်အလုပ်လုပ်ပါတယ်။ Rakefile ထဲမှာ Minitest::TestTask.create တစ်ကြောင်းက rake test task ကို သတ်မှတ်ပေးပါတယ်။
ruby test/cart_test.rb # run one file
ruby test/cart_test.rb --seed 42 # the same order every time
ruby test/cart_test.rb -n test_rejects_zero_price # one test by name
ruby test/cart_test.rb -n /rejects/ # tests whose name matches
rake test # every file in test/ (needs a Rakefile)require "minitest/test_task"
Minitest::TestTask.create # defines the "rake test" taskလက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Rails က app အသစ်ကို ဆောက်တဲ့အခါ test/ folder ကို ဖန်တီးပေးပြီး default test framework က Minitest ပါ။ Ruby gem အများစုလည်း ကိုယ်ပိုင် test တွေ ပါလာကြပါတယ်။ အဖွဲ့တွေက CI မှာ rake test ကို push တိုင်း run ပြီး မအောင်ရင် merge မလုပ်ပါဘူး။ ဒီအလေ့အကျင့်က code ပြောင်းလဲမှုကို ကြောက်ရွံ့စရာမလိုအောင် လုပ်ပေးပါတယ်၊ refactor လုပ်ပြီး test အောင်ရင် အပြုအမူ မပြောင်းဘူးဆိုတာ ယုံနိုင်ပါတယ်။
Bug တစ်ခု တွေ့ရင် ပထမဆုံး အဲဒီ bug ကို ပြန်ဖြစ်စေတဲ့ test တစ်ခု ရေးပါ၊ အဲဒါ မအောင်တာကို မြင်ပြီးမှ code ကို ပြင်ပါ။ ဒီ regression test က bug ပြန်မလာအောင် အမြဲ စောင့်ကြည့်ပေးပါတယ်။ Feature အသစ်ကိုလည်း ဒီနည်းနဲ့ စလုပ်လို့ရပါတယ် (test အရင်ရေး၊ မအောင်ကြည့်၊ code ရေး၊ အောင်စေ)။ အဲဒါကို TDD လို့ ခေါ်ပါတယ်။ RSpec လို တခြား framework တွေလည်း ရှိပေမယ့် assertion၊ setup၊ random order စတဲ့ အခြေခံအယူအဆတွေက တူညီပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
၁။ assert_equal ရဲ့ argument နှစ်ခုကို ပြောင်းထည့်တာ
assert_equal total(items), 5 လို့ actual ကို အရင်ရေးရင် မအောင်တဲ့အခါ "Expected: 6, Actual: 5" လို မှားပြောတဲ့ message ရပါတယ်၊ ဘယ်တန်ဖိုးက code က လာတာလဲ မှားမှတ်မိပြီး နာရီပေါင်းများစွာ မှားရှာနိုင်ပါတယ်။ အမြဲ expected ကို ပထမ၊ code ရဲ့ ရလဒ်ကို ဒုတိယ ရေးပါ။
၂။ Test တွေကြား state ကို မျှဝေတာ
class constant၊ @@variable ဒါမှမဟုတ် global တစ်ခုထဲကို test တွေက ရေးရင် တစ်ခု run ပြီးမှ ကျန်တစ်ခု ရလဒ်ပြောင်းသွားပါတယ်။ Random order မှာ တစ်ခါအောင်၊ တစ်ခါကျ ဖြစ်ပါတယ်။ အောက်က code မှာ ကြည့်ပါ၊ state ကို setup ထဲက instance variable အဖြစ် ဆောက်ရင် ပြေလည်ပါတယ်။
၃။ test_ နဲ့ မစတဲ့ method နာမည်
def check_total ... end လို့ ရေးရင် Minitest က test အဖြစ် မမြင်ပါဘူး။ "0 runs, 0 assertions, 0 failures" ဆိုပြီး အောင်သလို ပေါ်နေပေမယ့် ဘာမှ မစစ်ထားပါဘူး။ Report ထဲက runs အရေအတွက်ကို အမြဲ ကြည့်ပါ။
၄။ Float ကို assert_equal နဲ့ စစ်တာ
0.1 + 0.2 က 0.30000000000000004 ဖြစ်လို့ assert_equal 0.3, 0.1 + 0.2 မအောင်ပါဘူး။ binary floating point ရဲ့ ပုံမှန်သဘောပါ။ assert_in_delta ကို သုံးပါ၊ ဒါမှမဟုတ် ပိုက်ဆံကို cent လို integer နဲ့ သိမ်းပါ။
require "minitest"
require "stringio"
class LeakyTest < Minitest::Test
LOG = [] # one array shared by every test
def test_first_entry
LOG << :a
assert_equal [:a], LOG
end
def test_second_entry
LOG << :b
assert_equal [:b], LOG
end
end
class FixedTest < Minitest::Test
def setup
@log = [] # a fresh array before every test
end
def test_first_entry
@log << :a
assert_equal [:a], @log
end
def test_second_entry
@log << :b
assert_equal [:b], @log
end
end
def summary_for(pattern)
captured = StringIO.new
original = $stdout
$stdout = captured
begin
Minitest.run(["--seed", "1", "-n", pattern])
ensure
$stdout = original
end
captured.string.lines.last.strip
end
puts "LeakyTest: #{summary_for("/LeakyTest/")}"
puts "FixedTest: #{summary_for("/FixedTest/")}"LeakyTest: 2 runs, 2 assertions, 1 failures, 0 errors, 0 skips
FixedTest: 2 runs, 2 assertions, 0 failures, 0 errors, 0 skipsLeakyTest မှာ ဘယ် seed နဲ့ပဲ run run failure က အတိအကျ တစ်ခုပါ၊ ဘာကြောင့်လဲဆိုတော့ test တစ်ခုက LOG ကို အရင်ဖြည့်ပြီး ဒုတိယ test က ပြည့်နေတဲ့ LOG ကို တွေ့လို့ပါ။ ဘယ် test ကျမလဲကတော့ seed အပေါ် မူတည်ပါတယ်၊ ဒီ lesson ရေးစဉ် seed 1 မှာ test_second_entry ကျပြီး seed 2 မှာ test_first_entry ကျတာ တွေ့ရပါတယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Test တစ်ခုကို အပြုအမူတစ်ခုအတွက်ပဲ ရေးပြီး နာမည်ကို ဘာကို စစ်လဲ ပြောတဲ့ စာကြောင်းလို ပေးပါ (test_rejects_zero_price)။
- ပုံမှန်ကိစ္စတစ်ခုတည်း မစစ်ပါနဲ့၊ အလွတ်၊ နယ်နိမိတ်၊ nil နဲ့ error case တွေကိုလည်း စစ်ပါ။
- Test တိုင်းမှာ ကိုယ့်ဒေတာကို setup ထဲက ဆောက်ပြီး global state ကို မထိပါနဲ့။ ဖိုင်တွေ လိုရင် Dir.mktmpdir ကို သုံးပါ။
- Bug တစ်ခု ပြင်ရင် အရင်ဆုံး ပြန်ဖြစ်စေတဲ့ test တစ်ခု ရေးပြီး မအောင်ကြည့်ပါ။
Mini challenge
Mini challenge: Inventory ကို test လုပ်ပါ
Inventory class တစ်ခုနဲ့ အဲဒီအတွက် Minitest test လေးခု ရေးပါ။ Test ကို ဆောက်ပြီး fixed seed နဲ့ run ကာ timing မပါတဲ့ report ကို ရိုက်ထုတ်ပါ။
- Inventory မှာ add(sku, quantity)၊ remove(sku, quantity)၊ quantity(sku) ရှိရမယ်၊ မသိတဲ့ sku ရဲ့ quantity က 0
- ရှိတာထက် ပိုထုတ်ရင် OutOfStock ကို "only 1 of pen left" လို message နဲ့ raise လုပ်ရမယ်
- Test လေးခုက အလွတ် quantity၊ add စုပေါင်းခြင်း၊ remove လျော့ခြင်း၊ OutOfStock ကို assert_raises နဲ့ စစ်ခြင်းတို့ ပါရမယ်
- setup ထဲမှာ Inventory အသစ် ဆောက်ပြီး test တစ်ခုက တခြား test ကို မမှီခိုရ
require "minitest"
require "stringio"
# TODO: class OutOfStock < StandardError
# TODO: class Inventory with add, remove (raise OutOfStock when short) and quantity
# hint: Hash.new(0) gives every unknown sku a quantity of 0
class InventoryTest < Minitest::Test
# TODO: setup builds a fresh Inventory
# TODO: four tests, each starting with test_
# use assert_equal expected, actual and assert_raises(OutOfStock) { ... }
end
def run_quietly(seed)
captured = StringIO.new
original = $stdout
$stdout = captured
begin
Minitest.run(["--seed", seed.to_s])
ensure
$stdout = original
end
captured.string.sub(/^Finished in .*\n\n/, "")
end
# TODO: print run_quietly(1)ရလဒ်: Test လေးခု အားလုံး အောင်ရပါမယ်: Run options: --seed 1 # Running: .... 4 runs, 6 assertions, 0 failures, 0 errors, 0 skips
အရိပ်အမြွက် ကြည့်ရန်
remove ထဲမှာ quantity > @stock[sku] ဆိုရင် raise လုပ်ပါ။ assert_raises က exception ကို ပြန်ပေးလို့ error.message ကို ဆက်စစ်နိုင်ပါတယ်။ ပြီးရင် Inventory ရဲ့ quantity က မပြောင်းတာကိုပါ စစ်ပါ။
အဖြေ ကြည့်ရန်
require "minitest"
require "stringio"
class OutOfStock < StandardError; end
class Inventory
def initialize
@stock = Hash.new(0)
end
def add(sku, quantity)
@stock[sku] += quantity
end
def remove(sku, quantity)
raise OutOfStock, "only #{@stock[sku]} of #{sku} left" if quantity > @stock[sku]
@stock[sku] -= quantity
end
def quantity(sku) = @stock[sku]
end
class InventoryTest < Minitest::Test
def setup
@inventory = Inventory.new
end
def test_unknown_sku_has_zero_quantity
assert_equal 0, @inventory.quantity("pen")
end
def test_add_accumulates
@inventory.add("pen", 5)
@inventory.add("pen", 3)
assert_equal 8, @inventory.quantity("pen")
end
def test_remove_reduces_stock
@inventory.add("pen", 5)
@inventory.remove("pen", 2)
assert_equal 3, @inventory.quantity("pen")
end
def test_remove_too_many_raises
@inventory.add("pen", 1)
error = assert_raises(OutOfStock) { @inventory.remove("pen", 2) }
assert_equal "only 1 of pen left", error.message
assert_equal 1, @inventory.quantity("pen")
end
end
def run_quietly(seed)
captured = StringIO.new
original = $stdout
$stdout = captured
begin
Minitest.run(["--seed", seed.to_s])
ensure
$stdout = original
end
captured.string.sub(/^Finished in .*\n\n/, "")
end
puts run_quietly(1)Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Minitest test ဆိုတာ Minitest::Test ကို inherit လုပ်တဲ့ class ထဲက test_ နဲ့ စတဲ့ method တွေပါ၊ assertion မအောင်ရင် failure ပါ။
- assert_equal expected, actual ကို အစဉ်လိုက်ရေးပါ၊ ပြောင်းထည့်ရင် failure message က ပြောင်းပြန်ပြပါတယ်။
- setup နဲ့ teardown က test တိုင်းကို သန့်ရှင်းတဲ့ state ပေးပြီး random order နဲ့ seed က တစ်ခုနဲ့တစ်ခု မှီခိုနေတာကို ဖော်ပါတယ်။
- Report မှာ dot က အောင်၊ F က failure၊ E က error ပါ၊ seed နဲ့ timing စာကြောင်းက run တိုင်း ပြောင်းပါတယ်။
Minitest Documentation — Ruby Documentation