RobotFramework无法导入python文件中提到的模块

时间:2016-02-02 23:35:14

标签: python robotframework

我有一个导入.py文件的.robot文件(我的python文件有一些模块作为import语句) 当我试图运行机器人文件时,我得到以下错误。 如何确保导入python类文件中导入的模块?

[ ERROR ] Error in file 'C:\Users\Admin\Documents\PythonDemo\src\framework\tests\login_box.robot': Importing test library 'CreateCampaign.py' failed: ImportError: No module named framework.page_object_model.home_page
Traceback (most recent call last):
  File "C:\Users\Admin\Documents\PythonDemo\src\framework\tests\CreateCampaign.py", line 1, in <module>
from framework.page_object_model.home_page import HomePage
PYTHONPATH:
  C:\Users\Admin\Documents\PythonDemo\src\framework
  C:\Windows\system32\python27.zip
  C:\Python27\DLLs
  C:\Python27\lib
  C:\Python27\lib\plat-win
  C:\Python27\lib\lib-tk
  C:\Python27
  C:\Python27\lib\site-packages
==============================================================================
Login Box :: Tests Login to BOX.com
==============================================================================
Log into Box using valid credentials                                  | FAIL |
No keyword with name 'When I log into Box as my ${user} with ${userpass}' found.

.robot文件

*** Settings ***
Library  test_create_campaign.py

Documentation  Tests Login to BOX.com

*** Variables ***
${user}         abc@abc.com
${userpass}     testing

*** Test Cases ***
Log into Box using valid credentials
    When I log into Box as my ${user} with ${userpass}

.py文件

from framework.page_object_model.home_page import HomePage
from robot.api.deco import keyword

class CreateCampaign(LayarTestCase):
@keyword(name='I log into Box as my ${user} with ${userpass}')
    def test_create_campaign(self, user, userpass):
        print user
        print userpass

1 个答案:

答案 0 :(得分:1)

我修改了python文件以使其正常工作。

.py文件

class MyChildInline(admin.TabularInline):
    model = MyChildModel
    extra = 1
    def get_formset(self, request, parent=None, **kwargs):
        def formfield_callback(db_field):
            """
            Constructor of the formfield given the model field.
            """
            formfield = self.formfield_for_dbfield(db_field, request=request)
            if db_field.name == 'my_choice_field' and parent is not None:
                formfield.choices = parent.get_choices()
            return formfield
        return super(MyChildInline, self).get_formset(
            request, obj=obj, formfield_callback=formfield_callback, **kwargs)
        return result
  1. 更正了类定义的语法。
  2. 将类名重命名为与文件名相同。这很重要,因为Robot框架不会接受该关键字。你有没有理由使用课程?您可以在没有类的情况下定义方法(关键字),也可以使用。