Reputation: 1679
I'm writing unit tests as I develop a program. I'm using SQLAlchemy for database processes, so I have a few functions like this:
def create_sqla_engine():
""" Create and return the SQLA engine """
mysql_uri = os.environ.get('MYSQL_CONNECTION_URI')
engine = sqlalchemy.create_engine(mysql_uri)
return engine
If I do print(type(engine))
I can see that the type is <class 'sqlalchemy.engine.base.Engine'>
...so I want to test if this function works by checking if the type is correct (assuming the type will be None
if the create_engine
call fails...)
This code is in unittests.py
:
class TestDatabase(unittest.TestCase):
def test_database_create_merkle_sqla_session(self):
assert type(myprogram_database.create_sqla_engine()) is # ?
How do I check the type of a variable that is a class
?
Upvotes: 5
Views: 4023
Reputation: 309919
You have a few options here... The easiest is just to check that the object you get back is not None
:
self.assertIsNotNone(myprogram_database.create_sqla_engine())
If you actually want to check that the return type is an instance of something else, you can use assertIsInstance
:
self.assertIsInstance(myprogram_database.create_sqla_engine(),
sqlalchemy.engine.base.Engine)
Upvotes: 8