简体   繁体   中英

Robot Framework class inheritance

I have a parent class like below.

class ABC (object):

   def __init__:
      //Do something

   def __del__():
     //Cleanup what you did

Test Suites are sub classes of ABC.

Class A(ABC):
    def __init__():

Class B(ABC):
    def __init__():

However, when I execute pybot -i A.robot B.robot . ABC get created and destroyed and then again created and destroyed. How can I make the ABC constructor be called once before any test case and ABC destructor called at the end?

I don't want to use init .robot , because I don't want to the get tied with the framework. One of the requirement is to be able to plug the code out of RBF and still use it.

Can I make ABC a singleton?

I'm not sure I understand what you want to do, it looks like you want to override things more than use a singleton.

If you instanciate 2 objects you'll initiate 2 parents.

a = A() # ABC 1
b = B() # ABC 2

But maybe what you want is something like this:

abc = ABC()
a = A()
a.ABC = abc
b = B()
b.ABC = abc

or changing your __init__() to __init__(abc)

abc = ABC()
a = A(abc)
b = B(abc)

As written you question doesn't make much sense,since you can't write test suites in python. I'm going to guess you are asking about keyword libraries.

If you want a keyword library to be instantiated once when a suite gets executed, and destroyed after all the tests have run, you need to set the library scope. From the user guide section on library scope :

Test libraries can control when new libraries are created with a class attribute ROBOT_LIBRARY_SCOPE . This attribute must be a string and it can have the following three values:

TEST CASE A new instance is created for every test case. A possible suite setup and suite teardown share yet another instance. This is the default.

TEST SUITE A new instance is created for every test suite. The lowest-level test suites, created from test case files and containing test cases, have instances of their own, and higher-level suites all get their own instances for their possible setups and teardowns.

GLOBAL Only one instance is created during the whole test execution and it is shared by all test cases and test suites. Libraries created from modules are always global.

Here's an example with a class-based keyword library:

class ExampleLibrary:

    ROBOT_LIBRARY_SCOPE = 'TEST SUITE'

    def __init__(self):
        // do something

    def __del__(self):
        // do something

Important : python doesn't guarantee that the __del__ method runs. If the example library is destroyed right before the interpreter exits, the object may be destroyed without calling __del__ (which I think might be true about robot libraries).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM