Importing test library error for user defined meth

2019-06-14 10:15发布

I created Testclass.py as below:

class Testclass(object):
    def testmethod(self):
        print "Hi"

And I accessed it from .robot file as

Library   Testclass

and I called method testmethod.

But when I run through command line pybot login.robot I get import error

Error in file 'login.robot': Importing test library 'Testclass' failed: ImportError: No module named Testclass

If I don't define class and only define method it works.

1条回答
Fickle 薄情
2楼-- · 2019-06-14 10:51

The problem is simply that robot cannot find your library. It only looks in places in your PYTHONPATH. So, one solution is to add the path to your library to your PYTHONPATH environment variable.

You can also use the --pythonpath option to pybot if you don't want to alter your PYTHONPATH.

For example, assuming your file Testclass.py is in the folder ./robot/libraries, you can run your tests like this:

pybot --pythonpath ./robot/libraries my_test_case.robot

For more information about this option, see the section Configuring where to search libraries and other extensions in the robot framework user guide.

You can also specify the file by path, if you want to hard-code the path of the file into your test case. If you do this, the class name inside the file must match the filename (eg: class Testclass in Testclass.py).

For example:

*** Settings ***
| Library | robot/libraries/Testclass.py

This is covered in the robot framework user guide, in the section Specifying library to import.

查看更多
登录 后发表回答