Python中的类继承在Robot Framework中。

huangapple go评论87阅读模式
英文:

python class inheritence in robotframework

问题

我已经制作了一个Python类,将其用作robotframework中的库

Python文件名为:myclass.py

代码如下:

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.....")

之后,我创建了一个机器人测试用例文件

机器人文件名为:test.robot

代码如下:

*** Settings ***

Library  myclass.py  WITH NAME   my

*** Variables ***

*** Test Cases ***

TEST

    STEP1

*** Keywords ***

STEP1

    my.test0

当我执行上述代码的机器人测试用例时,它可以工作。

但是,当我更改测试用例如下时:

*** Settings ***

Library  myclass.MyTestClass1   WITH NAME   my

*** Variables ***

*** Test Cases ***

TEST

    STEP1

*** Keywords ***

STEP1

    my.test1

它不工作,显示"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

看起来"python_filename.classname"的方法在robotframework中无法以这种方式工作。

这里是在robotframework中使用多重继承的另一个答案

https://stackoverflow.com/questions/56947023/how-to-have-multiple-inheritance-in-robot-framework-library-file

假设它与我的解决方案相同,有人可以帮助我解决这个问题吗?

英文:

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

https://stackoverflow.com/questions/56947023/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?

答案1

得分: 1

你应该将类的名称与文件名称相同,并为每个类创建一个不同的文件。然后,你可以分别导入它们,它应该按预期工作。

或者,如果你不特别需要类,可以创建一个没有类只包含函数的文件。然后,你可以导入该文件并将所有函数用作关键字。

英文:

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.

huangapple
  • 本文由 发表于 2023年7月10日 10:10:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定