# Test script

**URL:** <https://www.ruby-forum.com/t/test-script/183285>\
**Category:** Ruby\
**Created:** [February 11, 2010, 6:01am UTC](https://www.ruby-forum.com/t/test-script/183285 "2010-02-11T06:01:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![twaynz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@twaynz](https://www.ruby-forum.com/u/twaynz)\
**Post date:** [February 11, 2010, 6:01am UTC](https://www.ruby-forum.com/t/test-script/183285/1 "2010-02-11T06:01:02Z")

</div>

Hello,  
I am new to ruby and scripting.  
I have to develop a test-driven software, so I have to run each time a  
test and make the necessary changes to the code if the test fails.  
My program takes integers as parameters through the command line.  
I need to automate tests (such that I check that all the tests still  
work after each code change) using shell scripts or batch.  
Does someone know how to do this?  
Thank you.  
Antoine

---

<div class="post-metadata">

**Author:** ![twaynz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@twaynz](https://www.ruby-forum.com/u/twaynz)\
**Post date:** [February 11, 2010, 6:29am UTC](https://www.ruby-forum.com/t/test-script/183285/2 "2010-02-11T06:29:56Z")

</div>

Antoine Makhassak wrote:

> 

First, pick a testing framework and set up your tests. If you have  
questions about how to test specific things, then you can ask within the  
context of that framework.  
Then, search for “continuous integration”.

-Justin

---

<div class="post-metadata">

**Author:** ![twaynz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@twaynz](https://www.ruby-forum.com/u/twaynz)\
**Post date:** [February 11, 2010, 6:41am UTC](https://www.ruby-forum.com/t/test-script/183285/3 "2010-02-11T06:41:52Z")

</div>

Antoine Makhassak wrote:

> Hello,  
> I am new to ruby and scripting.  
> I have to develop a test-driven software, so I have to run each time a  
> test and make the necessary changes to the code if the test fails.  
> My program takes integers as parameters through the command line.  
> I need to automate tests (such that I check that all the tests still  
> work after each code change) using shell scripts or batch.  
> Does someone know how to do this?  
> Thank you.  
> Antoine

Hi,

Already ruby has framework named as Test::Unit.

So if you want to create a test-driven software then just analyse the  
framework and design the software goal.

More about Test:Unit →   
[http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html](http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html)

Thanks  
P.Raveendran

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

---

<div class="post-metadata">

**Author:** ![twaynz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@twaynz](https://www.ruby-forum.com/u/twaynz)\
**Post date:** [February 11, 2010, 6:48am UTC](https://www.ruby-forum.com/t/test-script/183285/4 "2010-02-11T06:48:46Z")

</div>

I have used Test:Unit and the assert\_equal function, but I am just able  
to call a method I have written and check for expected and actual  
outputs.  
But the user will be entering the inputs in the command line, therefore  
he may enter too few or too many arguments. I couldn’t find a way to  
test for these errors using the assert\_equal function (since if i call  
the method I have written and pass too few or too many arguments I  
obviously get a compilation error)  
I hope you get what I mean

so basically let’s say my source file is called code.rb  
In the command line, I can enter:  
ruby code.rb 2 3  
ruby code.rb 2 3 4  
ruby code.rb 2 3 4 5  
I want to call these commands directly from a batch or shell script

Thanks,  
Antoine

> Hi,
> 
> Already ruby has framework named as Test::Unit.
> 
> So if you want to create a test-driven software then just analyse the  
> framework and design the software goal.
> 
> More about Test:Unit →   
> [http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html](http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html)
> 
> Thanks  
> P.Raveendran  
> [http://raveendran.wordpress.com](http://raveendran.wordpress.com)

---

<div class="post-metadata">

**Author:** ![twaynz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@twaynz](https://www.ruby-forum.com/u/twaynz)\
**Post date:** [February 11, 2010, 4:50pm UTC](https://www.ruby-forum.com/t/test-script/183285/5 "2010-02-11T16:50:32Z")

</div>

Richard C. wrote:

> What you are attempting to do here is an acceptance test, not a unit  
> test.  
> Test::Unit will facilitate both in this case.
> 
> What you want to do is call your program from the shell, with all of the  
> correct  
> arguments, and assert that your program gives the correct output.
> 
> THen do it again with random or incorrect arguments and verify that your  
> program gives the correct error response or exit code.
> 
> You will also need to verify the programs side effects. (Any files that  
> it  
> creates,  
> deletes or touches),

Yes I know, but I need to call my program with different arguments  
“automatically” i.e. I do not want to manually launch my program for  
each test. Can you tell me pls exactly what to enter in my .rb source  
file or a shell script / batch file so that it automatically makes the  
calls and outputs the results?  
Thank you

---

<div class="post-metadata">

**Author:** ![twaynz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@twaynz](https://www.ruby-forum.com/u/twaynz)\
**Post date:** [February 11, 2010, 4:43pm UTC](https://www.ruby-forum.com/t/test-script/183285/6 "2010-02-11T16:43:24Z")

</div>

What you are attempting to do here is an acceptance test, not a unit  
test.  
Test::Unit will facilitate both in this case.

What you want to do is call your program from the shell, with all of the  
correct  
arguments, and assert that your program gives the correct output.

THen do it again with random or incorrect arguments and verify that your  
program gives the correct error response or exit code.

You will also need to verify the programs side effects. (Any files that  
it  
creates,  
deletes or touches),

---

<div class="post-metadata">

**Author:** ![twaynz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@twaynz](https://www.ruby-forum.com/u/twaynz)\
**Post date:** [February 11, 2010, 6:59pm UTC](https://www.ruby-forum.com/t/test-script/183285/7 "2010-02-11T18:59:12Z")

</div>

Antoine Makhassak wrote:

> > THen do it again with random or incorrect arguments and verify that your  
> > Yes I know, but I need to call my program with different arguments  
> > “automatically” i.e. I do not want to manually launch my program for  
> > each test. Can you tell me pls exactly what to enter in my .rb source  
> > file or a shell script / batch file so that it automatically makes the  
> > calls and outputs the results?  
> > Thank you

You can use backticks (``) to call your program and return the output as  
a string. You can use $?.exitstatus to see if it exited with an error.  
You can also use string interpolation for arguments.

For example:

irb(main):001:0\> `echo hello`  
=\> “hello\n”  
irb(main):002:0\> $?.exitstatus  
=\> 0  
irb(main):003:0\> world = “world”  
=\> “world”  
irb(main):004:0\> `echo #{world}`  
=\> “world\n”

-Justin

---

<div class="post-metadata">

**Author:** ![twaynz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@twaynz](https://www.ruby-forum.com/u/twaynz)\
**Post date:** [February 11, 2010, 7:38pm UTC](https://www.ruby-forum.com/t/test-script/183285/8 "2010-02-11T19:38:25Z")

</div>

On Thu, Feb 11, 2010 at 3:50 PM, Antoine Makhassak  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > program gives the correct error response or exit code.  
> > each test. Can you tell me pls exactly what to enter in my .rb source  
> > file or a shell script / batch file so that it automatically makes the  
> > calls and outputs the results?  
> > Thank you

Well thats what I thought I said - you invoke the shell from within your  
ruby tests with the appropriate arguments. You might find the following  
articles useful:  
[http://tech.natemurray.com/2007/03/ruby-shell-commands.html](http://tech.natemurray.com/2007/03/ruby-shell-commands.html)  
[http://tech.natemurray.com/2007/03/ruby-shell-commands.html](http://tech.natemurray.com/2007/03/ruby-shell-commands.html)  
[http://www.gotripod.com/2007/10/07/tip-running-command-line-shell-commands-in-ruby-using-x/](http://www.gotripod.com/2007/10/07/tip-running-command-line-shell-commands-in-ruby-using-x/)  
[http://www.gotripod.com/2007/10/07/tip-running-command-line-shell-commands-in-ruby-using-x/](http://www.gotripod.com/2007/10/07/tip-running-command-line-shell-commands-in-ruby-using-x/)  
[http://groups.google.com/group/ruby-talk-google/browse\_thread/thread/a91dfc54f53195be](http://groups.google.com/group/ruby-talk-google/browse_thread/thread/a91dfc54f53195be)  
[http://groups.google.com/group/ruby-talk-google/browse\_thread/thread/a91dfc54f53195be](http://groups.google.com/group/ruby-talk-google/browse_thread/thread/a91dfc54f53195be)

> **[Elctech.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=elctech.com)**
>
> Get a new domain name for your startup. Quick and professional service. Seamless domain transfers.

This assumes that your CLI app executes and returns immediately without  
any  
further  
CLI interaction. For instance, it doesn’t prompt for further user input,  
or  
have a curses style  
interface. Both of which are also doable as automated tests, but they  
are  
harder.

If you are considering using Test::Unit to do the automation, you don’t  
need  
to start with a shell  
or batch file. Test::Unit allows you to define a battery of test  
methods,  
and this is the normal  
approach for dealing with inputs. If you have a very large amount of  
inputs  
you might want to  
consider using a small extension library for Test::Unit called ‘dust’ -  
it  
is very easy to create  
large volumes of tests, from arrays.

I wrote up some examples of using ‘dust’ a while ago here:

> **[Testing java code with Ruby and JtestR](http://richardconroy.blogspot.com/2008/11/testing-java-code-with-ruby-and-jtestr.html)**
>
> I am doing a lot of work lately with an old java code base. Its web based, so Watir is a good option at the acceptance test level. But ther...

Its not directly appropriate to your problem, but it should give you a  
few  
ideas of how you can  
structure a large volume of test inputs, in a way that plays nice with  
Test::Unit, and without  
having to manually write a new test method for each input.

regards,  
Richard.
