Custom Library - Keywords Usage - Unexpected argument error reported by a keyword of a Class but not of a module

Hello There,

Currently I’m exploring the different ways to use keywords and I’m getting some difficult to understand the reason why something works (and not works :smiley: ). Could you help me to learn a bit more about this topic please?

Scenario:
Given I have a testcase.robot
And the testcase.robot import the file KeywordsClass.py
And the testcase.robot import the file keywords_module.py
And both python files has a similar function
And the folder has only these tree files
When I execute the testcase.robot executing robot testcase.robot
Then the result below are verified
Figure 1

Question:

  1. Why I need to use .py when importing the keyword libraries in this scenario? I verified in many examples where it is not used, however, if it is removed from my code, the following error is reported.

  2. The second test case failed as you can see in the Figure 1. According to the message, the “Class Method” expected 1 argument, but it is not true since it has two and it is the same function used in the first test case but into a class (in my point of view). Do you have idea why this error are returned?

Files

"""
testcase.robot
"""
*** Settings ***
Library   keywords_module.py
Library   KeywordsClass.py

*** Test Cases ***
Example that calls a keyword from python module
    ${result}=  Module Function  hello  world
    Should be equal  ${result}  hello world

Example that calls a keyword from KeywordsClass
    ${result}=  Class Method  hello  world
    Should be equal  ${result}  hello world
"""
KeywordsClass.py
"""
class KeywordsClass:

    def __init__(self):
        pass

    def class_method(arg1, arg2):
        return arg1 + " " + arg2
"""
keywords_class.py
"""
def module_function(arg1, arg2):
    return arg1 + " " + arg2

Your class_method doesn’t have the self argument that methods in Python must have.

To import a library without the extension, it needs to in the Python module search path. That happens automatically if the library is installed but you can also set PYTHONPATH environment variable or use --pythonpath with the robot command.

1 Like

Well, I believe you figured out that I’m a newbie in python. :rofl: :rofl: :joy:

Thank you for the clarification! Based on it, I verified that:

  1. The pyYAML modules was not installed, but it is required to handle the robot.yaml (according to the documentation)
  2. The PYTHONPATH was not defined

So, I did the following:

  1. I added the PYTHONPATH to the system environment with the initial python installation path (since empty value is not accepted :smiley: and I suppose the robot will add automatically the script folder from robot.yml to PYTHONPATH ) as showed in the Figure 3 below:

  2. I added the file robot.yaml to the script folder (see the code below)

tasks:
condaConfigFile: conda.yaml
artifactsDir: output
PATH:
  - .
PYTHONPATH:
  - .
  
ignoreFiles:
  - .gitignore

f
3. I removed the .py from the testcase.robot
4. I executed the script again.

Actual Result:

  • The same problem was verified as showed below:
PYTHONPATH:
  c:\Users\araujoj\.vscode\extensions\robocorp.robotframework-lsp-0.39.0\src\robotframework_debug_adapter
  C:\Users\araujoj\Robots
  c:\Users\araujoj\.vscode\extensions\robocorp.robotframework-lsp-0.39.0\src
  c:\Users\araujoj\.vscode\extensions\robocorp.robotframework-lsp-0.39.0\src\robotframework_ls\vendored
  C:\Users\araujoj\AppData\Local\Programs\Python\Python310\python310.zip
  C:\Users\araujoj\AppData\Local\Programs\Python\Python310\DLLs
  C:\Users\araujoj\AppData\Local\Programs\Python\Python310\lib
  C:\Users\araujoj\AppData\Local\Programs\Python\Python310
  C:\Users\araujoj\AppData\Local\Programs\Python\Python310\lib\site-packages

NOTE: the testcase.robot is into the folder "C:\Users\araujoj\Robots\Creating test library class"

Expected Result:

  • The path “.” (current script directory) from robot.yaml file should considered
  • The .py file should be not required.

Please, help this newbie again :smiley: