Search code examples
pythonclassinheritancerobotframeworkkeyword

python class inheritence in robotframework


I have made a python class which will use as library in robotframework

python file name is : myclass.py

Code as follows:

class myclass:

        def test0(self):
        
            print("Test 0 start now.....")

class MyTestClass1(myclass):
    
        def test1(self):

            print("Test 1 start now.....")

class MyTestClass2(myclass):

    def test2(self):

            print("Test 2 start now.....")

after that, i have made a robot test case file

robot file name is : test.robot

code as follows:

*** Settings ***

Library  myclass.py  WITH NAME   my

*** Variables ***

*** Test Cases ***

TEST

    STEP1

*** Keywords ***

STEP1

    my.test0

It's work when i execute the robot test case with above code

But when i change the test case as below:

*** Settings ***

Library  myclass.MyTestClass1   WITH NAME   my

*** Variables ***

*** Test Cases ***

TEST

    STEP1

*** Keywords ***

STEP1

    my.test1

It does not work as "ModuleNotFoundError".

------------------------------------------------------------------------------------------------------------------------
Test                                                                                                            | FAIL |
1 test, 0 passed, 1 failed
========================================================================================================================
Output:  C:\Users\zhouvjie\oitefw\report\Test-Output-20230710-100003.xml
Log:     C:\Users\zhouvjie\oitefw\report\Test-Log-20230710-100003.html

[ ERROR ] Error in file 'C:\Users\zhouvjie\oran_ta_framework\TestData\CU_Plane\test.robot' on line 2: Importing library 'myclass.MyTestClass1' failed: ModuleNotFoundError: No module named 'myclass'
Traceback (most recent call last):
  None
PYTHONPATH:
  C:\Users\zhouvjie\AppData\Local\Programs\Python\Python310\Scripts\robot.exe
  C:\Users\zhouvjie\AppData\Local\Programs\Python\Python310\python310.zip
  C:\Users\zhouvjie\AppData\Local\Programs\Python\Python310\DLLs
  C:\Users\zhouvjie\AppData\Local\Programs\Python\Python310\lib
  C:\Users\zhouvjie\AppData\Local\Programs\Python\Python310
  C:\Users\zhouvjie\AppData\Local\Programs\Python\Python310\lib\site-packages
  C:\Users\zhouvjie\AppData\Local\Programs\Python\Python310\lib\site-packages\robotide\contrib\testrunner\../../lib
Report:  C:\Users\zhouvjie\oitefw\report\Test-Report-20230710-100003.html

Test finished 20230710 10:00:03

It's seem the method of "python_filename.classname" can not work this way in robotframework?

Here's another answer of multiple inheritance use in robotframework

How to Have Multiple inheritance in Robot Framework Library file?

suppose it's same way with my solution, can anyone help me how to fix this issue?


Solution

  • You should name your class the same as your file and make a different file for each class. You can then import them seperately and it should work as expected.

    Alternatively if you don't specifically need classes, you can just make a file without a class and only functions. You can then import the file and use all functions as keywords.