user1427661
user1427661

Reputation: 11774

Automating Commands in a CLI Program

I have a Ruby app that takes multiple steps of user input on the command line, i.e.:

Please select a client number: 1
Thank you for selecting 1. Please select a portfolio number: 3.
You are in portfolio 3. Please select a stock option: AAPL.
You have chosen AAPL. What would you like to do with it? b
How many shares would you like to buy? 5
You have purchased 5 shares of Apple stock. What would you like to do next? 

Not quite that ridiculous, but you get the idea. I would like to be able to test my rudimentary "UI" without having to go through it every single time for each corner case. Is there a tool, kind of a Selenium for the command line, that could achieve this?

Upvotes: 1

Views: 90

Answers (3)

davetron5000
davetron5000

Reputation: 24841

Depending on the barrage of questions, it might help to design your app around non-interactive input, and use interactivity as a fallback. For example, you might allow options like --client, --portfolio, and --stock-symbol. Your test/automation scripts would use these (and have the virtue of being eminently readable). You could then, by default, require interactive input from the user.

Now, to test said interactive input, expect would work, but if you are reading from standard input and the questions are deterministic, you can just make a text file and pipe it in. For example:

> cat input.txt
1
3
AAPL
b
5
> ./my_app < input.txt
> ./my_app --client 1 --portfolio 3 --stock-symbol AAPL --buy 5

Upvotes: 1

intellidiot
intellidiot

Reputation: 11228

You haven't mentioned your platform. In *nix platforms you can consider using expect command. Here are some examples to start with.

Otherwise you can modify your Ruby script to accept command line arguments. This is a better and less complicated way.

Upvotes: 0

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

Expect can do it. It's not a Ruby solution, but there may well be some "test wrappers" for it to make it easier.

Upvotes: 0

Related Questions