Nexus J. Xi
Nexus J. Xi

Reputation: 13

Pass arguments when robotframework test case create new instance of customized library class

There is a customized RF library written in Python:

class ALibrary:
    def __init__(self, name):
        self._name = name
    def get_name(self):
        print "*WARN*" + self._name

Import this library within settings

*** Settings ***
Library          lib/ALibrary.py LaoWang

by default, it creates new instances of test libraries for every test case.

My understanding is that __init__(self, name) gets invoked at the beginning of every test case, for example:

*** Test Cases ***
Test Case 1
    get name
Test Case 2
    get name

Robotframework should create one instance for Test Case 1 and another instance for Test Case 2, however, my __init__(self, name) required an argument, how do I pass this argument within the *** Test Cases ***?

Did a test:

$ python -m robot test.1.robot
==============================================================================
Test.1
==============================================================================
[ WARN ] LaoWang
Case 1                                                                | PASS |
------------------------------------------------------------------------------
[ WARN ] LaoWang
Case 2                                                                | PASS |
------------------------------------------------------------------------------
Test.1                                                                | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed

Both cases shown LaoWang, does that mean RF didn't create new instance in different test cases?

Upvotes: 1

Views: 1558

Answers (1)

A. Kootstra
A. Kootstra

Reputation: 6961

As this question is about Test Library Scope, I do want to link to the Robot Framework documentation on this topic. The way it should be read is that the library in the settings is re-initialised in the same way for each test case. Below is an example that illustrates that behaviour:

ALibrary.py

class ALibrary:

    ROBOT_LIBRARY_SCOPE = 'TEST CASE'

    def __init__(self, name):
        self._name = name

    def get_name(self):
        print "*WARN*" + self._name

    def set_name(self, name):
        self._name = name     

ALibrary.robot

*** Settings ***
Library          ALibrary   LaoWang

*** Test Cases ***
Test Case 1
    get name
    set name    New Name
    get name

Test Case 2
    get name
    set name    Another New Name
    get name

In the below result you can see that even after setting the new name in the first test case the default name from the settings initialisation returns in the second test case.

==============================================================================
TestProject                                                                   
==============================================================================
TestProject.ALibrary                                                          
==============================================================================
Test Case 1                                                           
[ WARN ] LaoWang
[ WARN ] New Name
| PASS |
------------------------------------------------------------------------------
Test Case 2                                                           
[ WARN ] LaoWang
[ WARN ] Another New Name
| PASS |
------------------------------------------------------------------------------
TestProject.ALibrary                                                  | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================   

Upvotes: 1

Related Questions