Passing an object to another class

Hi,

I am testing a class that takes an object of another class (which communicates with the database) as an object. How do I pass it to the second class correctly?

The classes look roughly like this:

class DBConnections:
    def **init** (self, path, pwd):
           self.__connection

class Queries:
      def __init(self, dbConn):
             self.dbObject = dbConn #dbConn is an object of class DBConnections.

In my robot file I call the dbConnections class as follows:
Library …/path/to/lib/DBConnections.py ${db_path} ${pwd} WITH NAME dbw

Here’s what I tried:
Library Queries.py dbw WITH NAME dbquery

→ dbw is not an object of type DBConnections

Library Queries.py ${dbw} WITH NAME dbquery

→ dbquery doesn’t even know its own methods.

How do I pass dbw to Queries, when creating that object? Any help is greatly appreciated!

I also found this on stackoverflow, but can’t make heads or tails of it.

Do I still instantiate the Library at the top and then get an instance as described? Where do I call the get instance, so it’s recognized across all tests?

If possible, I would reimagine the general architecture from being two libraries to a single library which derives its keywords and functionality from two or more classes. Then handle the relationship between connections and queries internally exposing out what user controlled functionality through keywords.

Well to give a bit of insight into the project: I’m inheriting an application that connects to a database and retrieves or adds entries. The code is written fairly well, but one decision they have made 10 years ago is to separate the db driver (pyodbc) from the queries, of which there are several categories (entries, todos, etc) all in their own file.
I’m tasked with moving the application from access to SQLite, so some of the query strings and returns are different. I jsut want to use some unit tests to query old vs new and compare to make sure the functions return the same things.

I don’t think I’ll be able to rearchitect that.

Library Queries.py dbw WITH NAME dbquery

→ dbw is not an object of type DBConnections

Understand more the limitations. Reflecting back some of my understanding, I would guess that instead of importing both libraries within the Resource section one could import the connection library there, as a “static” action, and then using I am assuming some keyword to get the DBConnections object object use the Import Library keyword where you could them pass along the db object.

How were they doing this previously with the pyodbc db driver and queries in the test suite/settings?

To be honest, I think I might be stretching the use case of robot framework with this. What might suit me better is a unit test framework where I can write “normal” python code with an assert equal at the end.

With the constraints on how the libraries were designed, it does seem difficult or simply kludged to get both connection libraries together with both queries in the same suite. Maybe two separate suites (which then of course you need to either name them so they execute in the order you want or specify that order) is an option. But I tend to agree that in this case it may be just as easy to write a quick unit test to verify.

The way to go about this is to instantiate the DBConnections in a Python variables file and pass it to RF that way. That will mean it’ll be available as global variable when the Libraries section gets loaded.
See [Robot Framework User Guide -](Robot Framework User Guide). It should look something like this:

variables.py

from somewhere import DBConnections
from robot.libraries.BuiltIn import BuiltIn

def get_variables():
    db_path = BuiltIn().get_variable_value("${db_path}")
    pwd = BuiltIn().get_variable_value("${pwd}")
    db_connection = DBConnections(path=db_path, pwd=pwd)
    variables = {
        "dbw": db_connection,
    }
    return variables

Then in the suite / resource file:

*** Settings *** 
    Library    Queries.py    ${dbw}    WITH NAME    dbquery

I just wanted to take a moment to thank you for your input and for having an open mind! Often in forums, people get overly defensive of “their” tool, when in this case the truth is that plain old pytest was a better fit for what I wanted to achieve.

I hope I can get some projects in the future, where robot framework is a better fit as I really like the way it works.

1 Like

In class Person, MyClass is also used so that is imported. In method display() object of MyClass is created. Then the my_method method of class MyClass is called and object of Person class is passed as parameter. On executing this Python program you get output as following.

I know this post is old, but wanted to share my solution using Robot Framework 5.0 with python 3.7
You should be able to import your library and then create a reference to it like such:

Import Library  path/to/lib/DBConnections.py  ${db_path}  ${pwd}  WITH NAME  dbw
${dbwRef} =  BuiltIn.Get Library Instance  dbw

Import Library  Queries.py  ${dbwRef}  WITH NAME  dbquery

I hope this helps someone.

https://robotframework.org/robotframework/5.0/libraries/BuiltIn.html#Get%20Library%20Instance