获取Unittest与其他测试共享类实例

时间:2018-02-19 18:19:16

标签: python unit-testing

我如何获得" getWorkSheet"和" getFileData"?分享相同的单位测试? Unittest运行两次设置。我是TDD的新手,似乎无法在Google上分享类实例。

import unittest
from workSq.convertToMonths import convertToMonths

class convertToMonthsUnittest(unittest.TestCase):

    def setUp(self):
        unittest.TestCase.setUp(self)
        print('In setUp()')
        self.c2m = convertToMonths()

    def test_getFileData(self):
        testDataRaw = self.c2m.getFileData("exampleFile/sq.xlsx")
        if str(testDataRaw)[:6]=="[Errno":
            testDataRaw = ""
        self.assertNotEqual(testDataRaw, "")

    def test_getWorkSheet(self):
        sheet = self.c2m.getWorkSheet('Schedule')
        print sheet


workSq.convertToMonths class

import openpyxl as pxl
from openpyxl.shared.exc import InvalidFileException
import sys
class convertToMonths():

    def __init__(self):
        self.workBook =""
    def getFileData(self,filename):
        try:
            self.workBook = pxl.load_workbook(filename)
        except InvalidFileException as e:
            return e
        return self.workBook

    def getWorkSheet(self,sheetName):
        print self.workBook
        sheet = self.workBook.get_sheet_by_name(sheetName)
        return sheet

1 个答案:

答案 0 :(得分:0)

我明白了。需要使用

 def setUpClass(self):

而不是

 def setUp(self):