Search code examples
pythonpython-3.xpycharmrobotframework

Custom library doesn't exist


I am getting started with robot framework using python. I have following folder structure:

Project Structure

Tests have .robot files and Library has .py files.

This is what my robot file looks like

*** Settings ***
Library   /Library/CustomLib.py

*** Test Cases ***

Testing
    ${name}     generate random name    ${10}
    log to console ${name}

This is what my CustomLib.py file has:

import random
import string


__version__ = '1.0.0'

from robot.api.deco import keyword


class CustomLib:
  ROBOT_LIBRARY_VERSION = __version__
  ROBOT_LIBRARY_SCOPE = 'GLOBAL'

  @keyword('generate random name')
  def get_random_name(self, email_length):
      letters = string.ascii_lowercase[:12]
      return ''.join(random.choice(letters) for i in range(email_length))

At runtime it throws error that file doesn't exist and "No keyword with name 'Generate random name' found."

Though when I cmd+click on this keyword it takes me to the exact same method. I know it's some simple little thing but can't seem to figure out what.


Solution

  • The error is because of how you defined the path to the library during its import - you've prefixed it with /, which means absolute path, e.g. "look for the directory Library from the root of the file system".

    Just use a relative import, and you should be fine; if your tests are in the folder Tests, change the import to:

    *** Settings ***
    Library  ../Library/CustomLib.py
    

    If they are in Tests/FunctionalityX/Another, then it would be like this, and so on:

    *** Settings ***
    Library  ../../../Library/CustomLib.py