2011-06-29 4 views
26

स्वचालित रूप से सभी परीक्षण मामलों को चलाने के बजाय, क्या रूबी टेस्ट/इकाई ढांचे के तहत एक परीक्षण को निष्पादित करने का कोई तरीका है। मुझे पता है कि मैं रेक का उपयोग करके इसे प्राप्त कर सकता हूं लेकिन मैं इस पल में रेक पर स्विच करने के लिए तैयार नहीं हूं।रूबी टेस्ट/यूनिट का उपयोग करके मैं एक परीक्षण कैसे निष्पादित करूं?

ruby unit_test.rb #this will run all the test case 
ruby unit_test.rb test1 #this will only run test1 

उत्तर

39

आप एक ही परीक्षण चलाने के लिए कमांड लाइन पर -n विकल्प पारित कर सकते हैं:

ruby my_test.rb -n test_my_method 

जहां 'test_my_method' परीक्षा पद्धति आप चलाना चाहते है का नाम है।

+1

+1 बिल्कुल वही जो मैं चाहता हूं। लेकिन मैं इसे केवल 6 मिनट के बाद स्वीकार करता हूं .. – pierrotlefou

+3

यदि आप लंबे विकल्प पसंद करते हैं तो पूरा विकल्प '--name' है। –

+3

भी regex का समर्थन करें: ruby ​​my_test.rb -n /test_.*/ – imwilsonxu

8

यदि आप एक गैर-खोल समाधान की तलाश में हैं, तो आप एक टेस्ट सूइट परिभाषित कर सकते हैं।

उदाहरण:

gem 'test-unit' 
require 'test/unit' 
require 'test/unit/ui/console/testrunner' 

#~ require './demo' #Load the TestCases 
# >>>>>>>>>>This is your test file demo.rb 
class MyTest < Test::Unit::TestCase 
    def test_1() 
    assert_equal(2, 1+1) 
    assert_equal(2, 4/2) 

    assert_equal(1, 3/2) 
    assert_equal(1.5, 3/2.0) 
    end 
end 
# >>>>>>>>>>End of your test file 


#create a new empty TestSuite, giving it a name 
my_tests = Test::Unit::TestSuite.new("My Special Tests") 
my_tests << MyTest.new('test_1')#calls MyTest#test_1 

#run the suite 
Test::Unit::UI::Console::TestRunner.run(my_tests) 

वास्तविक जीवन में, परीक्षण वर्ग MyTest मूल परीक्षण फ़ाइल से लोड किया जाएगा।