Python Unittest- class variables

4.7k views Asked by At

I was hoping someone wouldn't mind explaining what is going on here. I am attempting to run a python unittest that has been confirmed to be working using python 2.7. However, when attempting to run this same test on a machine running python 2.6 I am getting an error which I cannot figure out. Here is an example of what is happening

import re, string, os, subprocess, unittest
import MERCH_FUNCTIONS


class merchTests(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self._merchFileString=open("test_file.txt",'r').read()
        self._merchFileList=self._merchFileString.split("\n") #convert string to list

    def test_stuff(self):
         #print list
         print(self._merchFileList) 
if __name__ == '__main__':
    unittest.main()

For some reason if I run this code using python 2.7 it successfully runs the test, and the list self._merchFileList prints out.

However, when running this same code with python 2.6 I am getting the following error(s):

======================================================================
ERROR: test_stuff (__main__.merchTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "MERCH_Test_Case.py", line 14, in test_stuff
    print(self._merchFileList)
AttributeError: 'merchTests' object has no attribute '_merchFileList'

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

I cannot for the life of me figure out what is going on here. I have tried several different things with no success. If someone would be so kind as to explain what is going wrong here I would greatly appreciate it.

Thank you in advance.

1

There are 1 answers

4
asherbret On BEST ANSWER

setUpClass was introduced in python2.7. So it's not being called automatically when you run it with earlier versions (e.g., Python 2.6).