Creating a robot framework library from existing python package

坚强是说给别人听的谎言 提交于 2021-02-19 05:06:47

问题


My questions are:

  • How can I use the ConnectHandler in robotframework?
  • What is a good workflow to solve the problem of creating robot libraries from existing python packages?

I wish to use netmiko library in robotframework. I imported the module into my python env using pip and confirmed its available by using a robot file.

*** Settings ***
Library    netmiko

I now wish to instantiate a "ConnectHandler", I can see from the documentation that it takes a dictionary

https://pynet.twb-tech.com/blog/automation/netmiko.html at the python commandline:

>>> from netmiko import ConnectHandler

>>> cisco_881 = {
...   'device_type': 'cisco_ios',
...   'ip': '10.10.10.227',
...   'username': 'pyclass',
...   'password': 'password',
... } 

Source code is here: https://github.com/ktbyers/netmiko

So I edited the robot file to create a dictionary containing key:values , and then passed that as an argument to ConnectHandler.

*** Settings ***
Library    netmiko
Library    Collections


*** Test Cases ***
My Test
    ${device}=    Create Dictionary    device_type    cisco_ios
    ...    ip    10.10.10.227
    ...    username    pyclass
    ...    password    password
    Log Dictionary    ${device}

    ConnectHandler    ${device}

The result was

============================================================================== Testnetmiko

============================================================================== My Test
| FAIL | KeyError: u'device_type'

What am I doing wrong here?


回答1:


What is a good workflow to solve the problem of creating robot libraries from existing python packages?

The best way to create a library from an existing package is to do exactly that: create a library. Instead of trying to call the ConnectHandler method directly in your robot test case, create a keyword.

For example, create a file called netmikoKeywords.py, and place your code there. For example, you might have a keyword called Make Connection that might look something like this:

# netmikoKeywords.py
from netmiko import ConnectHandler

def make_connection(type, ip, username, password):
    device = {
       'device_type': type,
       'ip': ip,
       'username': username,
       'password': password,
    } 
    connection = ConnectHandler(device)
    return connection

If you want the connection to persist between keywords, you might want to set the connection as a global variable. Or, create your library as a class and make it an instance variable.

You can this use this in your robot file like so:

*** Settings ***
| Library | netmikoKeywords
*** Test cases ***
| Example
| | ${connection}= | Make connection
| | ... | cisco_ios | 10.10.10.227 | pyclass | password


来源:https://stackoverflow.com/questions/32952106/creating-a-robot-framework-library-from-existing-python-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!