2015-09-07 11 views
5

विधि 1: -कैसे दावा मायने रखता है परीक्षण इकाई में गणना कर रहे हैं

test.rb

class Test < Test::Unit::TestCase 
    def test_sample 
    assert_true(test) 
    assert_equal(a,b) 
    end 
end 

परिणाम: - ३८.३२,९५,३२,५२९ सेकंड में समाप्त हो गया।

1 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications 
100% passed 

विधि 2: -

test.rb

class Test < Test::Unit::TestCase 
require 'helper' 
include AssertionHelper 
    def test_sample 
    test_assertion 
    end 
end 

helper.rb

include Test::Unit::Assertions 
module AssertionHelper 
    def test_assertion 
    assert_true(test) 
    assert_equal(a,b) 
    end 
end 

परिणाम: -

Finished in 38.329532529 seconds. 

1 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications 
100% passed 

विधि 3: -

test.rb

class Test < Test::Unit::TestCase 
require 'helper' 
    def test_sample 
    AssertionHelper.test_assertion() 
    end 
end 

helper.rb

include Test::Unit::Assertions 
    module AssertionHelper 
     def self.test_assertion 
     assert_true(test) 
     assert_equal(a,b) 
     end 
    end 

परिणाम: -

Finished in 38.329532529 seconds. 

1 tests, 0 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications 
100% passed 

विधि 3 का उपयोग कर, मैं दावे के रूप में गिनती हो रही है "2" के बजाय "0"।

क्या मेरे लिए विधि 2 का उपयोग करके 2 के रूप में दावा गिनती संभव है?

+0

क्यों एक अलग मॉड्यूल में जोर रैप करने के लिए ? – Anatoly

+0

@ एनाटोली मेरे पास अधिक परीक्षण फ़ाइलों के लिए सत्यापित होने के लिए सामान्य दावा विवरण हैं। तो मैंने एक सहायक फाइल में आम दावे लगाए हैं और परीक्षण फाइलों से इसे बुलाया है। – karan

+0

test_helper.rb सामान्य कार्यों के लिए है लेकिन ** दावा ** परीक्षणों के भीतर होना चाहिए – Anatoly

उत्तर

1

आप पास कर सकते हैं अपने वर्तमान अपने मॉड्यूल के लिए TestCase, इस तरह:

sample_test.rb:

require 'test-unit' 
require 'helper' 

def a; true ; end 
def b; true ; end 
def test; true ; end 

class SampleTest < Test::Unit::TestCase 
    def test_sample 
     AssertionHelper.my_assertion(self) 
    end 
end 

helper.rb:

module AssertionHelper 
    def self.my_assertion(test_case) 
     test_case.instance_exec do 
     assert_true(test) 
     assert_equal(a, b) 
     end 
    end 
end 
0

क्षमा करें, लेकिन मैं आपकी स्थिति को पुन: पेश नहीं कर सकता, क्या आप Test::Unit संस्करण और अपने रूबी संस्करण प्रदान कर सकते हैं? Gemfile.lock के साथ सबसे अच्छा आपका Gemfile होगा। निम्नलिखित सेटअप मेरे लिए काम करता (मैं रूबी 2.2.0 और परीक्षण इकाई 3.0.8 का उपयोग करें):

ruby-2.2.0 in ~/projects/test-unit ♥ tree 
. 
├── Gemfile 
├── Gemfile.lock 
└── test 
    ├── helper.rb 
    └── sample_test.rb 

1 directory, 4 files 

ruby-2.2.0 in ~/projects/test-unit ♥ cat Gemfile 
# A sample Gemfile 
source "https://rubygems.org" 

# gem "rails" 
gem 'test-unit', '~> 3.0.8' 
ruby-2.2.0 in ~/projects/test-unit ♥ cat Gemfile.lock 
GEM 
    remote: https://rubygems.org/ 
    specs: 
    power_assert (0.2.2) 
    test-unit (3.0.8) 
     power_assert 

PLATFORMS 
    ruby 

DEPENDENCIES 
    test-unit (~> 3.0.8) 

sample_test.rb:

require 'test-unit' 

def a; true ; end 
def b; true ; end 
def test; true ; end 

class SampleTest < Test::Unit::TestCase 
    require 'helper' 
    include AssertionHelper 
    def test_sample 
     my_assertion 
    end 
end 

helper.rb:

module AssertionHelper 
    def my_assertion 
     assert_true(test) 
     assert_equal(a, b) 
    end 
end 

चल रहा है testrb उम्मीद के अनुसार 2 दावे देता है।

ruby-2.2.0 in ~/projects/test-unit ♥ testrb 
Loaded suite . 
Started 
. 

Finished in 0.000828 seconds. 

1 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications 
100% passed 

1207.73 tests/s, 2415.46 assertions/s 
ruby-2.2.0 in ~/projects/test-unit ♥ 

अद्यतन: यह वास्तव में अजीब है कि आप किसी भी त्रुटि (अपने 3 पद्धति पर) नहीं मिलता है क्योंकि मैं इस मिल: NoMethodError: undefined method 'assert_true' for AssertionHelper:Module और यह सच के बाद से AssertionHelper किसी अन्य तरीकों को लागू नहीं करता है, , आप इस पर assert_* विधियों को नहीं चला सकते हैं। बस ऊपर दिए गए मेरे कोड का उपयोग करें (आपकी विधि 2) और आपको ठीक होना चाहिए। यदि आप अभी भी उत्सुक हैं कि क्या किया जा सकता है, तो Test::Unit::Assertions पर एक नज़र डालें, इसमें बहुत से अंतर्निहित दावे परिभाषित किए गए हैं, शायद आपको यह उपयोगी लगे।

या बेहतर, मिनीटेस्ट या आरएसपीईसी का उपयोग करें, क्योंकि टेस्ट :: यूनिट is deprecated और केवल लीगेसी टेस्ट सूट के लिए मानक लाइब्रेरी में छोड़ा गया है।

+0

मैंने अपना प्रश्न अपडेट किया है। क्या आप इसे फिर से जांच सकते हैं। – karan

+0

में helper.rb में "टेस्ट :: यूनिट :: प्रविष्टियां शामिल करें" शामिल करें। आपकी assert_true त्रुटि हल हो जाएगी। – karan

+0

आपको 'शामिल' के बजाय इसे 'विस्तार' करना चाहिए। हां, यह त्रुटि को हल करता है, लेकिन यह आपकी समस्या को ठीक नहीं करता है - मॉड्यूल में किए गए दावों की गणना अभी भी नहीं की जाती है। ऐसा इसलिए होता है क्योंकि 'test :: unit :: Assertions' में' add_assertion' विधि खाली है। इसे काम करने के लिए आपको इसे अपने 'टेस्ट :: यूनिट :: टेस्टकेस' का एक उदाहरण पास करने की आवश्यकता है ताकि आप इसे गिनने के लिए अपने मॉड्यूल के 'add_assertion' से' add_assertion' विधि को कॉल कर सकें। सब कुछ, इसे लागू करने के लिए बहुत बोझिल है, इसलिए मैं ऊपर अपना समाधान पसंद करता हूं। आपको यह क्यों पसंद नहीं है? –

संबंधित मुद्दे