我正在使用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
帮助???