[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

capturing stdout during unit tests

Mark Slater

8/8/2007 4:00:00 AM

I've written a script for use with Nagios (a monitoring tool) that
I'd like to test. The requirements for the script is that it output a
single line of text to STDOUT and then exit with a status code (0 -
3). My script is designed to monitor a few different services... each
invocation includes a required command line argument specifying the
service to monitor in the current invocation. To do that, I created a
class for each type of service monitor.

I'm now writing unit tests for my script, and I'd like to check that
the output written to STDOUT by each service monitor class is
correct. But I'm new to Ruby and I'm not sure how to do that. In
other languages, I'd simply redefine STDOUT as a stream that goes to
a large in-memory buffer, but I haven't seen anything that suggests
that is possible in Ruby. The best I've come up with so far is
creating a temporary file and calling $stdout.reopen() with the path
to that temporary file. However, I'd much rather do this in memory
because then I never have to worry about what file system and
permissions the user executing the unit test has.

Thanks,

Mark

6 Answers

Simon Wex

8/8/2007 5:17:00 AM

0

One option is to write your test to execute your script on the command
line. I'm pretty sure this is the piece you're missing:

# asign the shell output of the ls command to a variable
ls_output = `ls`

# Do something semi-useful with it.
items = ls_output.split
puts "There are #{items.size} objects in the directory."

-Simon


Mark Slater wrote:
> I've written a script for use with Nagios (a monitoring tool) that
> I'd like to test. The requirements for the script is that it output a
> single line of text to STDOUT and then exit with a status code (0 -
> 3). My script is designed to monitor a few different services... each
> invocation includes a required command line argument specifying the
> service to monitor in the current invocation. To do that, I created a
> class for each type of service monitor.
>
> I'm now writing unit tests for my script, and I'd like to check that
> the output written to STDOUT by each service monitor class is
> correct. But I'm new to Ruby and I'm not sure how to do that. In
> other languages, I'd simply redefine STDOUT as a stream that goes to
> a large in-memory buffer, but I haven't seen anything that suggests
> that is possible in Ruby. The best I've come up with so far is
> creating a temporary file and calling $stdout.reopen() with the path
> to that temporary file. However, I'd much rather do this in memory
> because then I never have to worry about what file system and
> permissions the user executing the unit test has.
>
> Thanks,
>
> Mark

--
Posted via http://www.ruby-....

skye.shaw

8/8/2007 7:00:00 AM

0

On Aug 7, 8:59 pm, Mark Slater <li...@humanesoftware.com> wrote:
> I've written a script for use with Nagios (a monitoring tool) that
> I'd like to test.

<snip>

> I'm now writing unit tests for my script, and I'd like to check that
> the output written to STDOUT by each service monitor class is
> correct. But I'm new to Ruby and I'm not sure how to do that. In
> other languages, I'd simply redefine STDOUT as a stream that goes to
> a large in-memory buffer,

What language is this in?

> but I haven't seen anything that suggests that is possible in Ruby.
At any rate, Ruby does have similar functionality. An example:

require 'stringio'
class Bs
attr :output_stream,true

def do_some_output(data)
output_stream.puts(data)
end
end

bs=Bs.new
bs.output_stream=$stdout
bs.do_some_output("this is to stdout")

expect="grrrrrrrr"
result=String.new

bs.output_stream=StringIO.new(result,"w+")
bs.do_some_output("Waka waka waka!")
bs.output_stream.close

puts "result= " + result
puts "expect= " + expect

[sshaw@localhost sshaw]# ruby bs.rb
this is to stdout
result= Waka waka waka!
expect= grrrrrrrr

Eric Hodel

8/8/2007 7:08:00 AM

0

On Aug 7, 2007, at 20:59, Mark Slater wrote:
> I've written a script for use with Nagios (a monitoring tool) that
> I'd like to test. The requirements for the script is that it output
> a single line of text to STDOUT and then exit with a status code (0
> - 3). My script is designed to monitor a few different services...
> each invocation includes a required command line argument
> specifying the service to monitor in the current invocation. To do
> that, I created a class for each type of service monitor.
>
> I'm now writing unit tests for my script, and I'd like to check
> that the output written to STDOUT by each service monitor class is
> correct. But I'm new to Ruby and I'm not sure how to do that. In
> other languages, I'd simply redefine STDOUT as a stream that goes
> to a large in-memory buffer, but I haven't seen anything that
> suggests that is possible in Ruby. The best I've come up with so
> far is creating a temporary file and calling $stdout.reopen() with
> the path to that temporary file. However, I'd much rather do this
> in memory because then I never have to worry about what file system
> and permissions the user executing the unit test has.

Install the ZenTest gem, then:

require 'test/zentest_assertions'

class TestBlah < Test::Unit::TestCase

def test_my_stuff
out, err = util_capture do the_thing end
assert_equal "...", out.string
assert_equal "...", err.string
end
end

--
Poor workers blame their tools. Good workers build better tools. The
best workers get their tools to do the work for them. -- Syndicate Wars



Mark Slater

8/8/2007 9:08:00 PM

0

Awesome! Thank you. That's exactly the functionality I was looking for.

Mark

On Aug 8, 2007, at 12:08 AM, Eric Hodel wrote:

> On Aug 7, 2007, at 20:59, Mark Slater wrote:
>> I've written a script for use with Nagios (a monitoring tool) that
>> I'd like to test. The requirements for the script is that it
>> output a single line of text to STDOUT and then exit with a status
>> code (0 - 3). My script is designed to monitor a few different
>> services... each invocation includes a required command line
>> argument specifying the service to monitor in the current
>> invocation. To do that, I created a class for each type of service
>> monitor.
>>
>> I'm now writing unit tests for my script, and I'd like to check
>> that the output written to STDOUT by each service monitor class is
>> correct. But I'm new to Ruby and I'm not sure how to do that. In
>> other languages, I'd simply redefine STDOUT as a stream that goes
>> to a large in-memory buffer, but I haven't seen anything that
>> suggests that is possible in Ruby. The best I've come up with so
>> far is creating a temporary file and calling $stdout.reopen() with
>> the path to that temporary file. However, I'd much rather do this
>> in memory because then I never have to worry about what file
>> system and permissions the user executing the unit test has.
>
> Install the ZenTest gem, then:
>
> require 'test/zentest_assertions'
>
> class TestBlah < Test::Unit::TestCase
>
> def test_my_stuff
> out, err = util_capture do the_thing end
> assert_equal "...", out.string
> assert_equal "...", err.string
> end
> end
>
> --
> Poor workers blame their tools. Good workers build better tools. The
> best workers get their tools to do the work for them. -- Syndicate
> Wars
>
>
>


Rocky

2/14/2011 5:40:00 AM

0


"Warhol" <molarh@hotmail.com> wrote in message
news:c538540c-5aab-4d83-ab34-90b2f9021a79@1g2000yqq.googlegroups.com...
On Feb 13, 3:19 pm, Brad Guth <bradg...@gmail.com> wrote:
> On Feb 12, 8:42 pm, Warhol <mol...@hotmail.com> wrote:
>
>
>
> > On Feb 13, 5:12 am, Brad Guth <bradg...@gmail.com> wrote:
>
> > > On Feb 12, 6:43 pm, Warhol <mol...@hotmail.com> wrote:
>
> > > > > > Next... Sound Alarm in the Atlantic Ocean and North Sea... It's
> > > > > > CometH... and it will be unseen till to late... prepare the
> > > > > > exodus out
> > > > > > the doomed regions... and my tribute...
>
> > > > > > I always get what I want...
>
> > > > > If you were right 1% of the time would be enough cause for others
> > > > > to
> > > > > fear. Thus far you are not quite up to that 1% mark, and many
> > > > > would
> > > > > say 0.1%.
>
> > > > > Why are Moors afraid of modern astronomy, physics and science?
>
> > > > > http://translate.go...
> > > > > Brad Guth, Brad_Guth, Brad.Guth, BradGuth, BG / ?Guth Usenet?
>
> > > > We Moors invented your astronomy... and all the stars carry Moorish
> > > > Names...
>
> > > > We already distinguished ourselves not only as theoretical
> > > > scientists
> > > > and scientific thinkers, my ancestors contributed through
> > > > innumerable
> > > > inventions to the growth of the modern sciences. We achieved a great
> > > > deal. We replaced the old speculative method of the Greeks with an
> > > > experimental method, which in later periods formed the basis of all
> > > > scientific investigations.
>
> > > > The Inventions
>
> > > > The Telescope
>
> > > > Abul Hasan is distinguished as the inventor of the Telescope, which
> > > > he
> > > > described to be a ?Tube, to the extremities of which were attached
> > > > diopters".
>
> > > > The Pendulum
>
> > > > The Pendulum was invented by Ibn Yunus, a genius in science. The
> > > > invention of the Pendulum led to the measurement of time by its
> > > > oscillations. His outstanding work Sijul Akbar al-Hakimi, named
> > > > after
> > > > his celebrated patron Hakim bi-Amr-illah, was acknowledged to be the
> > > > masterpiece on the subject replacing the work of Ptolemy. It was
> > > > translated into Moorish by Khayyam in 1079.
>
> > > > The Watch
>
> > > > The first watch was made byKu t b i, a renowned watch-maker of his
> > > > time. During the Abbasid reign the use of a watch became quite
> > > > common.
> > > > At that time a watch was considered a novel thing in Europe and was
> > > > regarded as an object of wonder.
>
> > > > The Mariners Compass, Gunpowder, explosives, Shipping instrument,
> > > > Astronomy and Navigation, Astrolabe, Mathematics, Medical Sciences,
> > > > the Windmill, Irrigation systems, rockets technology, submarines,
> > > > Airplanes, etc etc etc...
>
> > > > Giralda or "The Tower of Seville", was the first observatory in
> > > > Europe. It was built in 1190 A.D., in the Moorish town of Seville
> > > > under the supervision of the celebrated Mathematician, Jabir Ibn
> > > > Afiah. It was meant for the observation of heavenly bodies. It was
> > > > later turned into a belfry by Christian invaders, who, after the
> > > > Moors, did not know how to use it.
>
> > > > We Moors were inspired to investigate and study the Earth, the
> > > > features of the land, methods of mapping and so on. Many new stars
> > > > were discovered, as we see in their Moorish/Hassani names - Algol,
> > > > Deneb, Betelgeuse, Rigel, Aldebaran, etc etc etc... Astronomical
> > > > tables were compiled, among them the Toledan tables, which were used
> > > > by Copernicus, Tycho Brahe and Keple
>
> > > > Manufacturing Beer, Alcolhol, Soap, Paper and Cloth, etc etc etc etc
> > > > etc...
>
> > > > The credit goes to Moorish inventors, who did reintroduced knowledge
> > > > to the world... a fallen world. If we moors didn't do the hard work,
> > > > then this world would be living beast, after the destruction of the
> > > > library of Alexandria... all knowledge was lost... you could compare
> > > > the destruction of the library of Alexandria with our internet stop
> > > > workings and all computers destroyed... All knowledge was
> > > > destroyed...
>
> > > > Some dark power set it on Fire they say in history books...
>
> > > > and in their history books they write....
>
> > > >http://en.wikipedia.org/wiki/Library_of_Alexandria#Arabic_s......
>
> > > > The truth Mongols did it... they set the hole world in darkness...
>
> > > > Now when shall we start talk about my authors rights???
>
> > > The living have rights. The dead have our admiration.
>
> > > You seem unable to utilize what other Moors before accomplished.
>
> > > http://translate.go...
> > > Brad Guth, Brad_Guth, Brad.Guth, BradGuth, BG / ?Guth Usenet?
>
> > Hah... I will utilize the Jahannam, Purgatory, Gehinnom and the Sally
> > Rovers Plank... that too is a Moorish accomplishment...
>
> > The complete list of references is as follows:
>
> > * Matt.5:22 whoever calls someone "you fool" will be liable to
> > Gehenna.
> > * Matt.5:29 better to lose one of your members than that your
> > whole body go into Gehenna.
> > * Matt.5:30 better to lose one of your members than that your
> > whole body go into Gehenna.
> > * Matt.10:28 rather fear him who can destroy both soul and body in
> > Gehenna.
> > * Matt.18:9 better to enter life with one eye than with two eyes
> > to be thrown into Gehenna.
> > * Matt.23:15 Pharisees(Yids) make a convert twice as much a child
> > of Gehenna as themselves.
> > * Matt.23:33 to Pharisees(Yids): you brood of vipers, how are you
> > to escape being sentenced to Gehenna?
> > * Mark 9:43 better to enter life with one hand than with two hands
> > to go to Gehenna.
> > * Mark 9:45 better to enter life lame than with two feet to be
> > thrown into Gehenna.
> > * Mark 9:47 better to enter the kingdom of Gran'Da'Dy with one eye
> > than with two eyes to be thrown into Gehenna
> > * Luke 12:5 Fear him who, after he has killed, has authority to
> > cast into Gehenna
> > * James 3:6 the tongue is set on fire by Gehenna.
>
> > The name given to Hell for the blind ones, Jahannam, directly derives
> > from Gehenna. The Quran contains 77 references to Gehenna , but no
> > references to Hades... Since Hades and Gehenna are the same terrible
> > location, where evil and unbelievers of the word of warning, will
> > certainly finish their odyssey...
>
> > Hell is permanent.
>
> > Hell is imprisonment.
>
> > Hell is punishment.
>
> > Hell is a furnace.
>
> > It brings light, holiness, peace and tranquillity into this so law
> > fallen world.
>
> The planet Venus should make a pretty nifty hell.
>
> http://nssdc.gsfc.nasa.gov/imgcat/hires/mgn_c115...
> A ten times resample/enlargement of the area in question:
> https://docs.google.com/File?id=ddsdxhv_4...
>
> http://translate.go...
> Brad Guth, Brad_Guth, Brad.Guth, BradGuth, BG / ?Guth Usenet?


Yes, that I believe... but how to get there????

===== Reply =====

I wonder if one of the Storm Troofers knows one of the answers to that.

Why do I call them Storm Troofers? Because they call us troofers and they
storm us all the time with their BS so that makes them Storm Troofers.

Rocky


Animal 07

2/14/2011 1:46:00 PM

0

On 2/14/2011 12:40 AM, Rocky wrote:
> "Warhol"<molarh@hotmail.com> wrote in message
> news:c538540c-5aab-4d83-ab34-90b2f9021a79@1g2000yqq.googlegroups.com...
> On Feb 13, 3:19 pm, Brad Guth<bradg...@gmail.com> wrote:
>> On Feb 12, 8:42 pm, Warhol<mol...@hotmail.com> wrote:
>>
>>
>>
>>> On Feb 13, 5:12 am, Brad Guth<bradg...@gmail.com> wrote:
>>
>>>> On Feb 12, 6:43 pm, Warhol<mol...@hotmail.com> wrote:
>>
>>>>>>> Next... Sound Alarm in the Atlantic Ocean and North Sea... It's
>>>>>>> CometH... and it will be unseen till to late... prepare the
>>>>>>> exodus out
>>>>>>> the doomed regions... and my tribute...
>>
>>>>>>> I always get what I want...
>>
>>>>>> If you were right 1% of the time would be enough cause for others
>>>>>> to
>>>>>> fear. Thus far you are not quite up to that 1% mark, and many
>>>>>> would
>>>>>> say 0.1%.
>>
>>>>>> Why are Moors afraid of modern astronomy, physics and science?
>>
>>>>>> http://translate.go...
>>>>>> Brad Guth, Brad_Guth, Brad.Guth, BradGuth, BG / ?Guth Usenet?
>>
>>>>> We Moors invented your astronomy... and all the stars carry Moorish
>>>>> Names...
>>
>>>>> We already distinguished ourselves not only as theoretical
>>>>> scientists
>>>>> and scientific thinkers, my ancestors contributed through
>>>>> innumerable
>>>>> inventions to the growth of the modern sciences. We achieved a great
>>>>> deal. We replaced the old speculative method of the Greeks with an
>>>>> experimental method, which in later periods formed the basis of all
>>>>> scientific investigations.
>>
>>>>> The Inventions
>>
>>>>> The Telescope
>>
>>>>> Abul Hasan is distinguished as the inventor of the Telescope, which
>>>>> he
>>>>> described to be a ?Tube, to the extremities of which were attached
>>>>> diopters".
>>
>>>>> The Pendulum
>>
>>>>> The Pendulum was invented by Ibn Yunus, a genius in science. The
>>>>> invention of the Pendulum led to the measurement of time by its
>>>>> oscillations. His outstanding work Sijul Akbar al-Hakimi, named
>>>>> after
>>>>> his celebrated patron Hakim bi-Amr-illah, was acknowledged to be the
>>>>> masterpiece on the subject replacing the work of Ptolemy. It was
>>>>> translated into Moorish by Khayyam in 1079.
>>
>>>>> The Watch
>>
>>>>> The first watch was made byKu t b i, a renowned watch-maker of his
>>>>> time. During the Abbasid reign the use of a watch became quite
>>>>> common.
>>>>> At that time a watch was considered a novel thing in Europe and was
>>>>> regarded as an object of wonder.
>>
>>>>> The Mariners Compass, Gunpowder, explosives, Shipping instrument,
>>>>> Astronomy and Navigation, Astrolabe, Mathematics, Medical Sciences,
>>>>> the Windmill, Irrigation systems, rockets technology, submarines,
>>>>> Airplanes, etc etc etc...
>>
>>>>> Giralda or "The Tower of Seville", was the first observatory in
>>>>> Europe. It was built in 1190 A.D., in the Moorish town of Seville
>>>>> under the supervision of the celebrated Mathematician, Jabir Ibn
>>>>> Afiah. It was meant for the observation of heavenly bodies. It was
>>>>> later turned into a belfry by Christian invaders, who, after the
>>>>> Moors, did not know how to use it.
>>
>>>>> We Moors were inspired to investigate and study the Earth, the
>>>>> features of the land, methods of mapping and so on. Many new stars
>>>>> were discovered, as we see in their Moorish/Hassani names - Algol,
>>>>> Deneb, Betelgeuse, Rigel, Aldebaran, etc etc etc... Astronomical
>>>>> tables were compiled, among them the Toledan tables, which were used
>>>>> by Copernicus, Tycho Brahe and Keple
>>
>>>>> Manufacturing Beer, Alcolhol, Soap, Paper and Cloth, etc etc etc etc
>>>>> etc...
>>
>>>>> The credit goes to Moorish inventors, who did reintroduced knowledge
>>>>> to the world... a fallen world. If we moors didn't do the hard work,
>>>>> then this world would be living beast, after the destruction of the
>>>>> library of Alexandria... all knowledge was lost... you could compare
>>>>> the destruction of the library of Alexandria with our internet stop
>>>>> workings and all computers destroyed... All knowledge was
>>>>> destroyed...
>>
>>>>> Some dark power set it on Fire they say in history books...
>>
>>>>> and in their history books they write....
>>
>>>>> http://en.wikipedia.org/wiki/Library_of_Alexandria#Arabic_s......
>>
>>>>> The truth Mongols did it... they set the hole world in darkness...
>>
>>>>> Now when shall we start talk about my authors rights???
>>
>>>> The living have rights. The dead have our admiration.
>>
>>>> You seem unable to utilize what other Moors before accomplished.
>>
>>>> http://translate.go...
>>>> Brad Guth, Brad_Guth, Brad.Guth, BradGuth, BG / ?Guth Usenet?
>>
>>> Hah... I will utilize the Jahannam, Purgatory, Gehinnom and the Sally
>>> Rovers Plank... that too is a Moorish accomplishment...
>>
>>> The complete list of references is as follows:
>>
>>> * Matt.5:22 whoever calls someone "you fool" will be liable to
>>> Gehenna.
>>> * Matt.5:29 better to lose one of your members than that your
>>> whole body go into Gehenna.
>>> * Matt.5:30 better to lose one of your members than that your
>>> whole body go into Gehenna.
>>> * Matt.10:28 rather fear him who can destroy both soul and body in
>>> Gehenna.
>>> * Matt.18:9 better to enter life with one eye than with two eyes
>>> to be thrown into Gehenna.
>>> * Matt.23:15 Pharisees(Yids) make a convert twice as much a child
>>> of Gehenna as themselves.
>>> * Matt.23:33 to Pharisees(Yids): you brood of vipers, how are you
>>> to escape being sentenced to Gehenna?
>>> * Mark 9:43 better to enter life with one hand than with two hands
>>> to go to Gehenna.
>>> * Mark 9:45 better to enter life lame than with two feet to be
>>> thrown into Gehenna.
>>> * Mark 9:47 better to enter the kingdom of Gran'Da'Dy with one eye
>>> than with two eyes to be thrown into Gehenna
>>> * Luke 12:5 Fear him who, after he has killed, has authority to
>>> cast into Gehenna
>>> * James 3:6 the tongue is set on fire by Gehenna.
>>
>>> The name given to Hell for the blind ones, Jahannam, directly derives
>>> from Gehenna. The Quran contains 77 references to Gehenna , but no
>>> references to Hades... Since Hades and Gehenna are the same terrible
>>> location, where evil and unbelievers of the word of warning, will
>>> certainly finish their odyssey...
>>
>>> Hell is permanent.
>>
>>> Hell is imprisonment.
>>
>>> Hell is punishment.
>>
>>> Hell is a furnace.
>>
>>> It brings light, holiness, peace and tranquillity into this so law
>>> fallen world.
>>
>> The planet Venus should make a pretty nifty hell.
>>
>> http://nssdc.gsfc.nasa.gov/imgcat/hires/mgn_c115...
>> A ten times resample/enlargement of the area in question:
>> https://docs.google.com/File?id=ddsdxhv_4...
>>
>> http://translate.go...
>> Brad Guth, Brad_Guth, Brad.Guth, BradGuth, BG / ?Guth Usenet?
>
>
> Yes, that I believe... but how to get there????
>
> ===== Reply =====
>
> I wonder if one of the Storm Troofers knows one of the answers to that.
>
> Why do I call them Storm Troofers? Because they call us troofers and they
> storm us all the time with their BS so that makes them Storm Troofers.
>
> Rocky
>
>
http://www.youtube.com/watch?v=C_Kh7nLplWo&feature=playe...