Question

Is it possible to write Robot Framework tests in Python instead of the .txt format?

Behind the scenes it looks like the .txt test get converted into Python by pybot so I'm hoping that this is simply a matter of importing the right library and inheriting from the right class but I haven't been able to figure out how to do that.

(We already have a bunch of suites and have keywords written in both formats but sometimes the RF syntax makes it very difficult to do things that are simple in Python. I understand it would be possible to just write a Python keyword for each test plus 'wrap' setup and teardown functions the same way, but that seems cumbersome.)

Was it helpful?

Solution

Robot does not convert your test cases to python behind the scenes before running them. Instead, it parses the test cases, then iterates over each keyword, calling the code that implements the keyword. There isn't ever a stage where there's a completely pure python representation of a test case.

It is not possible to write tests in python, and have those tests run alongside traditional robot tests by the provided test runner. Like you said in your question, your only option is to put all of your logic for a single test case in a single keyword, and call that keyword from a test case.

It is possible to create and execute tests in python solely via the published API. This might not be what you're really asking for, because ultimately you're still creating keywords, you're just creating them via python.

from robot.api import TestSuite

suite = TestSuite('Activate Skynet')
suite.imports.library('OperatingSystem')
test = suite.tests.create('Should Activate Skynet', tags=['smoke'])
test.keywords.create('Set Environment Variable', args=['SKYNET', 'activated'], type='setup')
test.keywords.create('Environment Variable Should Be Set', args=['SKYNET'])

The above example was taken from here:

http://robot-framework.readthedocs.org/en/2.8.1/autodoc/robot.running.html

OTHER TIPS

Well, you should not care if your python code represents tests or keywords as long as you code the logic of the tests in python.

The best you can do is to keep some html tables in robot format. Each line would be a call for a keyword. The keyword could be implemented in python, and, logically, represents a whole test (although in robot terminology it is still a "keyword").

This post shows how you can have access to the robot context from your python code.

  • robot variables

    BuiltIn().get_variable_value("${USERNAME}")

  • java keywords

    from com.mycompany.myproject.testtools import LoginRobotKeyword LoginRobotKeywords().login(user, pwd)

  • robot keywords BuiltIn().run_keyword("check user connected", user)

You could try hytest developped by me.

See the user guide.

Hytest is similar to Robot Framework, except it let developer write test cases in Python.

It's easy to use, a pythoneer should get along with it very quickly.

Robotframework does not support writting test cases in python directly. I've submitted an enhancement PR, check it here

https://github.com/robotframework/robotframework/issues/3128

But I've tried to do that by moving all the test cases logic to python code, and make RF test cases just a entry point to them.

Here is an example.

We could create a python file to include all testing logic and setup/teardown logic, like this

# *** case0001.py *****
from SchoolClass import SchoolClass
schCla = SchoolClass()

class case0001:

    def steps(self):
        print('''\n\n***** step 1 ****  add school class \n''')
        self.ret1 = schCla.add_school_class('grade#1', 'class#1', 60)
        assert self.ret1['retcode'] == 0



        print('''\n\n***** step 2 ****  list school class to check\n''')

        ret = schCla.list_school_class(1)
        schCla.classlist_should_contain(ret['retlist'],
                                    'grade#1', 
                                    'class#1',
                                    60,
                                    self.ret1['id'])


    def setup(self):
        pass

    def teardown(self):
        schCla.delete_school_class(self.ret1['id'])


And then we creat a Robot file. In which all RF test cases are in the same form and just work as entry points to python test cases above.

like this

*** Settings ***
Library    cases/case0001.py   WITH NAME  C000001
Library    cases/case0002.py   WITH NAME  C000002


*** Test Cases ***

add class - tc000001
    [Setup]   C000001.setup
    C000001.steps
    [Teardown]    C000001.teardown

add class - tc000002
    [Setup]   C000002.setup
    C000002.steps
    [Teardown]    C000002.teardown

You could see, in this way, the RF testcases are similar. We could even create a tool to auto generate them by scanning Python testcases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top