简体   繁体   中英

Get Unittest to share class instance with other tests

How do I get "getWorkSheet" to share the same unittest as "getFileData"? Unittest runs setup twice. I am new to TDD and can't seem to find and thing about sharing a class instance on 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

I figured it out. Needed to use

 def setUpClass(self):

instead of

 def setUp(self):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM