Thursday, October 7, 2010

Hello World Robot Framework library

This is the way I did my first Robot Framework keyword library. It should show the basic steps to add your own python keywords.

Let's do it in a test-driven way!

Failing test case

Add a file called HelloWorld.txt. This is our robot test suite file.

Add following text to the file:

*** Test Cases ***
HelloWorld
Hello World



After this run pybot HelloWorld.txt - this will execute your Hello World test case.
Output should be something like:

==============================================================================
HelloWorld
==============================================================================
HelloWorld | FAIL |
No keyword with name 'Hello World' found.
------------------------------------------------------------------------------
HelloWorld | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================


Now we have a failing test case! So we can begin to implement our super cool Hello World keyword.

Keyword file

Add a file called HelloWorld.py to the same directory as our HelloWorld.txt test suite.

Add following text to the file:

def hello_world():
print "HELLO WORLD!"


Now we have implemented our fine keyword that prints "HELLO WORLD!". Although our test still fails..

Passing test case

We have to import our super cool library to our test suite. Add following lines to the HelloWorld.txt (before test cases):

*** Settings ***
Library HelloWorld.py



After this run pybot HelloWorld.txt - and watch it PASS:
==============================================================================
HelloWorld
==============================================================================
HelloWorld.HelloWorld
==============================================================================
HelloWorld | PASS |
------------------------------------------------------------------------------
HelloWorld.HelloWorld | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
HelloWorld | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================



Thats it.

11 comments:

xopa said...

Easy to understand and very useful

Mark said...

It didn't work for me at first because I had typed in my HelloWorld.txt file instead of cut-and-paste. The key thing I missed was that under the *** Settings *** section, there must be more than one space between "Library" and "HelloWorld.py" --this is because it is parsed as a "table" using the rules laid out elsewhere in the Robot Framework docs.

Sridevi said...

wow..it worked...extremely useful..i could learn the basics clearly

Anonymous said...

Great clearly worked fine :)

Haris said...

Where does it print to?

Pekka Klärck said...

Haris, the printed message goes to Robot's log file.

For anyone interested in creating test libraries, here's a simple demo project that illustrates it in practice:
https://bitbucket.org/robotframework/robotdemo/wiki/Home

Other Robot Framework demos, docs, etc. can be found via http://robotframework.org.

Abhishek said...

Thank you. Simplest of all, but extremely helpful. Cleared all basics.

D. said...

Hello, I'm a new in a Robot and will ask possible stupid questions.
I do not understand relation between file with python code and actual robot's file.
I'm assuming I should expect HELLO WORLD! string in output, but I got HELLO WORLD, no explanation mark "!"

Is this possible create example where in a python I have function open URL and then another function with LogIn and Password variable. And "robot" file should run the login test and verify if this step was successful?

I think this example will be more usable and helpful.

many thanks!

Pekka Klärck said...

D: When you say "output", do you mean logs, reports, or the console output? The message logged with `print "HELLO WORLD!"` goes only into the generated log file. What you see on the console are test suite and test case names. Error messages would be shown there too.

Examples opening browsers etc. are out of the scope of this very simple demo. Instead you can study the demo projects available here: http://robotframework.org/#documentation

dude_tj said...

Hi, If i have a .py file with multiple classes in it. How to add individual class to the .robot file. I tried using FileName.ClassName convention it is not working for me. Please help.

Anonymous said...

Thank you. Very good example. I'm a newbie and I got it! Thanks again.