Test script

A

Antoine Makhassak

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
 
J

Justin Collins

Antoine said:
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

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
 
R

Raveendran Perumalsamy

Antoine said:
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


Thanks
P.Raveendran
http://raveendran.wordpress.com
 
A

Antoine Makhassak

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
 
R

Richard Conroy

[Note: parts of this message were removed to make it a legal post.]

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),
 
A

Antoine Makhassak

Richard said:
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
 
J

Justin Collins

Antoine said:
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
 
R

Richard Conroy

[Note: parts of this message were removed to make it a legal post.]

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

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://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://www.elctech.com/snippets/executing-shell-commands-in-ruby

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:
http://richardconroy.blogspot.com/2008/11/testing-java-code-with-ruby-and-jtestr.html
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top