python - Using pyodbc in Unit Tests -
i'm struggling write unit tests python use pyodbc query local sql server database.
the following code runs...
import pyodbc import random import unittest emailsender import emailsender class testsequencefunctions(unittest.testcase): def setup(self): self.email_sender = emailsender() self.other_email_sender = emailsender() def test_for_initialisation_variables(self): """ tests check object created """ self.asserttrue(self.email_sender.server) self.asserttrue(self.email_sender.errorlogfile) self.asserttrue(self.email_sender.connection) self.asserttrue(self.email_sender.cursor) self.asserttrue(self.email_sender.siteauth) self.asserttrue(self.email_sender.apicallslimitperrun) def test_sql_server_connection(self): value in self.email_sender.cursor.execute("select * sys.tables"):. print value
however, when add second, identical test code...
import pyodbc import random import unittest emailsender import emailsender class testsequencefunctions(unittest.testcase): def setup(self): self.email_sender = emailsender() self.other_email_sender = emailsender() def test_for_initialisation_variables(self): """ tests check object created """ self.asserttrue(self.email_sender.server) self.asserttrue(self.email_sender.errorlogfile) self.asserttrue(self.email_sender.connection) self.asserttrue(self.email_sender.cursor) self.asserttrue(self.email_sender.siteauth) self.asserttrue(self.email_sender.apicallslimitperrun) def test_sql_server_connection(self): value in self.email_sender.cursor.execute("select * sys.tables"):. print value def test_getsites_method(self): value in self.other_email_sender.cursor.execute("select * sys.tables"):. print value
the third test fails following error message:
error: ('im001', '[im001] [unixodbc][driver manager]driver not support function (0) (sqlrowcount)')
p.s.
just note, started off 1 email_sender object being created couldn't run back queries changed query identical , when didn't work, added connection , cursor there no issues re: position cursor.
found solution - problem in (default) teardown method.
something going on cursor wasn't being reset - overrode method below , hey presto, worked:
def teardown(self): """ make sure reset cursor """ if self.email_sender.connection: self.email_sender.connection.close() del self.email_sender
Comments
Post a Comment