Testing Comamnd-Line Applications With Aruba
In the last couple of weeks I have been working, with a little project of my own. I always love Command Line Tools, I don’t know what they have but using them make feel more like a Hackers or someone that actually know what he is doing.
So I decided to build one, with the help of a gem called Thor, which by the way is a great gem, that help you build your CLI really easy.
Codewars provided a great service by letting us the programmer improve our coding skills, I decide to build a CLI to interact with it.
Probably I will write a post in future about creating a CLI but for now I want to focus on test.
My project is called Codewars_Cli, any one interested, is open for suggestions and pull requests.
Wihch is focus on Behavior Driven Development.
Aruba is a great extension either for Rspec and Cucumber that makes testing command-lines tools meanigful, easy and fun. It makes easy to manipulate the file system and the process envarioment, automatically reset state of file system.
First inside the
features/support folder create a
env.rb to load
Now we are ready to start testing our application. By default
Aruba will create a folder
tmp/aruba where will perform its operations, you can change that in the
env.rb with some hooks that
1 2 3
For a list of all the available configuration README
I’m going to focus on testing my config commands. Normally this command will involve manipulating a config file store in your
If we don’t want our test to actually modify that file, aruba can mock our config file as well.
Feature test example from my small project.
1 2 3 4 5 6 7 8 9 10 11 12 13
As you can see is really easy with
aruba to achieve that.
Also I have declare some
1 2 3 4 5 6 7 8 9 10 11
To help me test. There are many more methods that
aruba bring us like
cd into folder, create files, delete them etc… Here is another great resource about it Getting Started
Stubbing External Services
In my case my application depends on an external service, so I don’t want my test to actually make any real request, that would result in a slower test suite.
Aruba execute the command under test in a new child process, that it makes them slower and complicated to mock components. But there is a way to make the test run in the same process.
First we have to wrap our application, and execute the wrapper instead.
This is my
bin file before introducing the wrapper class:
1 2 3 4 5
1 2 3 4 5 6 7
CodewarsCli::Runner the wrapper class has to respond to
Now we have to modify the the
env.rb inside our
1 2 3 4 5 6 7 8
We have added
aruba/in_process and pass some configuration to tell
aruba to run the test in the same process.
Now to create our wrapper CodewarsCli::Runner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
Now we can use
VCR to mock our external services.
We can do that with hooks example.
I have created
features/support/webmock.rb file where I will store all my hooks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
And inside the features test we can use this hook.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
This is all I have learn about
Aruba for testing your applications, I think is a great tool, that remove us from the pain of having to create to many
step_definitions and let us focus on testing our application. I know there is to much to learn about
Aruba and testing in general and if you have any questions or comments about the subject please do not hesitate.