PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > How do you write a unit test of methods that needs user inpu
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How do you write a unit test of methods that needs user inpu

Réponse
 
LinkBack Outils de la discussion
Vieux 04/11/2007, 00h52   #1
pood.forums@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How do you write a unit test of methods that needs user inpu

Will I need to change my code or is there a way for me to write my unit
test that automatically enters some inputs?

Idealy, I'd like to do it so that when I run my unit test, It'll ask me
for the inputs. Right now it just goes thru the code without asking me
for the inputs.


THanks
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 04/11/2007, 00h59   #2
Phlip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How do you write a unit test of methods that needs user inpu

pood.forums wrote:

> Will I need to change my code or is there a way for me to write my unit
> test that automatically enters some inputs?


Get with Mocha, a mock-object library. Then mock the method that returns
user input, and put whatever the test needs into it.

> Idealy, I'd like to do it so that when I run my unit test, It'll ask me
> for the inputs. Right now it just goes thru the code without asking me
> for the inputs.


Okay, this is different. If it's interative, it's not an automated test. If
you to test many different inputs, put them into a table and loop over the
table each time you test.

Make the table easy to edit, such as a YAML file.

See "FIT" and "Fitnesse" for good GUIs on these tables of test data. Then
invent your own, Ruby-style, without devolving into FIT's sounce code!

--
Phlip


  Réponse avec citation
Vieux 04/11/2007, 07h31   #3
bbxx789_05ss@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How do you write a unit test of methods that needs user inpu

Feng Tien wrote:
> Will I need to change my code or is there a way for me to write my unit
> test that automatically enters some inputs?
>


It's somewhat difficult. This works for me:

my_program.rb:
--------------
def get_input
while true
print "Enter a non-zero integer greater than -5: "
num = gets

num_int = num.to_i
num_float = num.to_f

if num_int != num_float
puts "You entered a float. Try again."
elsif num_int <= -5
puts "You entered a number that was too small."
elsif num_int == 0
puts "You entered 0 or a word. That is not allowed."
else
return num_int
end

end
end

if __FILE__ == $0
result = get_input
puts "You entered #{result}. Thank you."
end



my_program_UnitTest.rb
--------------
require 'my_program'
require 'test/unit'
require 'stringio'

class TestMyProgram < Test::Unit::TestCase

def setup
$stdin = DATA #DATA is like a file containing the stuff after
__END__
trash_can = StringIO.new
$stdout = trash_can

#If you want to see the prompts shown to the user as part of
#the unit test output and/or be able to send output to your
#terminal for debugging, comment out the previous line.
end

def teardown
$stdin = STDIN
$stdout = STDOUT
end

def test_get_input
while not DATA.eof?
num = get_input

num_int = num.to_i
num_float = num.to_f

assert_not_equal(0, num_int)
assert(num_int > -5, "The number was not greater than -5.")
assert_equal(num_int, num_float)
end
end
end

__END__
-6
-4
0
2.5
6 #Must end the data with a good number--otherwise get_input will
#run out of data, causing gets in get_input to return nil, which
will
#cause get_input to go into an infinite loop.


> Idealy, I'd like to do it so that when I run my unit test, It'll ask me
> for the inputs.
>


After typing in 5 - 10 different numbers into the command line for hours
on end, you will probably think differently about testing code by hand.
In fact, if you are going to test code by hand, you don't need unit
testing. Unit testing is designed to enable you to test your program by
driving it with another program. That allows you to do tests relatively
quickly and thoroughly--and the tests are repeatable.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h17.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10678 seconds with 11 queries