3

I am hitting an issue that I do not understand when trying to create a Pandas data frame in my unittest file. The error happens well before the functions within the class gets called.

Here is a simple code to reproduce:

import unittest
import pandas as pd
import numpy as np

class simpleTest(unitest.TestCase):
    dates = pd.date_range('20160101', periods = 5)
    dataDf = pd.DataFrame({'date': dates,
            'count': np.array([3, 7, 4, 66, 9])})

    def doSomething(self):
        pass

if __name__ == '__main__':
    unittest.main()

The error I get is this:

Traceback (most recent call last):
  File "tsa_test.py", line 31, in <module>
    unittest.main()
  File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 94, in __init__
    self.parseArgs(argv)
  File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 149, in parseArgs
    self.createTests()
  File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 158, in createTests
    self.module)
  File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 130, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute '-'

1 Answer 1

4

Your unittest code is problematic. You did right in subclassing unittest.TestCase, so this line is OK:

class simpleTest(unitest.TestCase):

However, this class should now have methods that look like:

     def test_foo(self):
         ...

(note that they should start with test_, and should take self). The omission of any such method is confusing unittest.

Additionally, you have static class members, which you probably meant to be used as class fixtures. That's not how it's done in unittest. Your class should look like this:

class simpleTest(unitest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.dates = pd.date_range('20160101', periods = 5)
        cls.dataDf = pd.DataFrame({'date': cls.dates,
            'count': np.array([3, 7, 4, 66, 9])})

    def test_foo(self):
        # Note that here you access things like so:
        self.dataDF
        # even though you defined it as a class instance - that's how unittest works
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.