我正在使用python 2.7並鑽研TDD。我試圖測試一個簡單的函數,它使用csv模塊並返回一個csv.reader對象。我想測試使用assertIsInstance測試返回正確類型的對象,但是我無法弄清楚如何使這項工作。如何獲得python unittest來測試函數是否返回一個csv.reader對象?
#!/usr/bin/python
import os, csv
def importCSV(fileName):
'''importCSV brings in the CSV transaction file to be analyzed'''
try:
if not(os.path.exists("data")):
os.makedirs("data")
except(IOError):
return "Couldn't create data directory!"
try:
fileFullName = os.path.join("data", fileName)
return csv.reader(file(fileFullName))
except(IOError):
return "File not found!"
測試目前看起來是這樣的....
#!/usr/bin/python
from finaImport import finaImport
import unittest, os, csv
class testImport(unittest.TestCase):
'''Tests for importing a CSV file'''
def testImportCSV(self):
''' Test a good file and make sure importCSV returns a csv reader object '''
readerObject = finaImport.importCSV("toe")
self.assertTrue(str(type(readerObject))), "_csv.reader")
我真的不認爲在STR和類型的函數包裹「腳趾」是正確的。當我嘗試類似...
self.assertIsInstance(finaImport.importCSV("toe"), csv.reader)
它返回一個錯誤,如...
TypeError: isinstance() arg2 must be a class, type, or tuple of classes and types
幫助???