2015-09-07 22 views
5

Metodo 1: -Come conta affermazione sono calcolate in unità di prova

test.rb

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

Risultato: - Finito in 38.329532529 secondi.

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

Metodo 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 

Risultato: -

Finished in 38.329532529 seconds. 

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

Metodo 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 

Risultato: -

Finished in 38.329532529 seconds. 

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

Quando si utilizza il metodo 3, sto ottenendo conta come asserzione "0" anziché "2".

È possibile ottenere il conteggio delle asserzioni come 2 utilizzando il Metodo 2?

+0

perché per avvolgere affermazione in un modulo separato ? – Anatoly

+0

@Anatoly Ho dichiarazioni di asserzione comuni da verificare per più file di test. Quindi ho messo le asserzioni comuni in un file helper e chiamandolo dai file di test. – karan

+0

test_helper.rb è per le funzioni comuni ma ** le asserzioni ** devono essere incluse nei test – Anatoly

risposta

1

È possibile passare il vostro attuale TestCase al modulo, in questo modo:

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

Siamo spiacenti, ma non riesco a riprodurre la situazione, potresti fornire la versione Test::Unit e la tua versione di rubino? La cosa migliore sarebbe il tuo Gemfile con Gemfile.lock. La configurazione che segue funziona per me (io uso ruby ​​2.2.0 e test-unit 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 

In esecuzione testrb fornisce 2 asserzioni, come previsto.

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 ♥ 

UPDATE: Questo è in realtà strano che non si ottiene alcun errore (sul metodo 3), perché ottengo questo: NoMethodError: undefined method 'assert_true' for AssertionHelper:Module e questo è vero, dal momento che AssertionHelper non implementa altri metodi , non è possibile eseguire alcun metodo assert_* su di esso. Basta usare il mio codice sopra (il tuo metodo 2) e dovresti stare bene. Se sei ancora curioso di sapere cosa si può fare, dai uno sguardo a Test::Unit::Assertions, ci sono anche molte affermazioni predefinite definite, forse lo trovi utile.

Oppure, meglio, utilizzare MiniTest o RSpec, dal momento che Test :: Unità is deprecated e viene lasciato nella libreria standard solo per le suite di test legacy.

+0

Ho aggiornato la mia domanda. Puoi ri-testare ora. – karan

+0

Includi "include Test :: Unità :: Asserzioni" in helper.rb. Il tuo errore assert_true sarà risolto. – karan

+0

Dovresti "estenderlo" piuttosto che "include". Sì, questo corregge l'errore, ma ciò non risolve il problema: le asserzioni fatte nel modulo non vengono ancora conteggiate. Questo succede perché il metodo 'add_assertion' è vuoto in' Test :: Unità :: Asserzioni'. Per farlo funzionare devi passare un'istanza del tuo 'Test :: Unit :: TestCase' in modo da poter chiamare il suo metodo 'add_assertion' dal' add_assertion' del tuo modulo per farlo contare. Tutto sommato, è troppo complicato implementarlo, quindi preferisco la mia soluzione sopra. Perché non ti piace? –